|
@@ -2,16 +2,17 @@ package utils
|
|
|
|
|
|
import (
|
|
import (
|
|
"fmt"
|
|
"fmt"
|
|
- "git.wecise.com/wecise/common/logger"
|
|
|
|
- "io/ioutil"
|
|
|
|
|
|
+ "github.com/wecisecode/util/logger
|
|
"os"
|
|
"os"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
"time"
|
|
"time"
|
|
|
|
+
|
|
|
|
+ "github.com/wecisecode/util/logger"
|
|
)
|
|
)
|
|
|
|
|
|
var (
|
|
var (
|
|
- dataStats = make(map[string]*DataStat) // <title-name>:DataStat
|
|
|
|
|
|
+ dataStats = make(map[string]*DataStat) // <title-name>:DataStat
|
|
dataStatsMu sync.Mutex
|
|
dataStatsMu sync.Mutex
|
|
)
|
|
)
|
|
|
|
|
|
@@ -56,7 +57,7 @@ type DataStat struct {
|
|
func NewDataStat(title, name string, interval int, chanSize int) *DataStat {
|
|
func NewDataStat(title, name string, interval int, chanSize int) *DataStat {
|
|
dataStatsMu.Lock()
|
|
dataStatsMu.Lock()
|
|
defer dataStatsMu.Unlock()
|
|
defer dataStatsMu.Unlock()
|
|
- key := title+"-"+name
|
|
|
|
|
|
+ key := title + "-" + name
|
|
if ds, ok := dataStats[key]; ok {
|
|
if ds, ok := dataStats[key]; ok {
|
|
if ds.statChan != nil {
|
|
if ds.statChan != nil {
|
|
close(ds.statChan)
|
|
close(ds.statChan)
|
|
@@ -103,9 +104,9 @@ func (ds *DataStat) Close() {
|
|
}
|
|
}
|
|
|
|
|
|
func (ds *DataStat) Start() {
|
|
func (ds *DataStat) Start() {
|
|
- tc := time.NewTicker(time.Duration(ds.Interval)*time.Second)
|
|
|
|
|
|
+ tc := time.NewTicker(time.Duration(ds.Interval) * time.Second)
|
|
startTime := time.Now()
|
|
startTime := time.Now()
|
|
- ds.StartTime = startTime.UnixNano()/int64(time.Millisecond)
|
|
|
|
|
|
+ ds.StartTime = startTime.UnixNano() / int64(time.Millisecond)
|
|
ds.running = true
|
|
ds.running = true
|
|
defer func() {
|
|
defer func() {
|
|
ds.running = false
|
|
ds.running = false
|
|
@@ -113,30 +114,30 @@ func (ds *DataStat) Start() {
|
|
L:
|
|
L:
|
|
for {
|
|
for {
|
|
select {
|
|
select {
|
|
- case <- tc.C:
|
|
|
|
|
|
+ case <-tc.C:
|
|
now := time.Now()
|
|
now := time.Now()
|
|
- speed := float64(ds.Success + ds.Fail + ds.Ignore) / now.Sub(startTime).Seconds()
|
|
|
|
|
|
+ speed := float64(ds.Success+ds.Fail+ds.Ignore) / now.Sub(startTime).Seconds()
|
|
startTime = now
|
|
startTime = now
|
|
logger.Infof("Stat %s %s: success:%d fail:%d ignore:%d within %ds, total success:%d total fail:%d total ignore:%d speed:%.1f/s.\n", ds.title, ds.name, ds.Success, ds.Fail, ds.Ignore, ds.Interval, ds.TotalSuccess, ds.TotalFail, ds.TotalIgnore, speed)
|
|
logger.Infof("Stat %s %s: success:%d fail:%d ignore:%d within %ds, total success:%d total fail:%d total ignore:%d speed:%.1f/s.\n", ds.title, ds.name, ds.Success, ds.Fail, ds.Ignore, ds.Interval, ds.TotalSuccess, ds.TotalFail, ds.TotalIgnore, speed)
|
|
- ds.StatTime = now.UnixNano()/int64(time.Millisecond)
|
|
|
|
|
|
+ ds.StatTime = now.UnixNano() / int64(time.Millisecond)
|
|
ds.Success = 0
|
|
ds.Success = 0
|
|
ds.Fail = 0
|
|
ds.Fail = 0
|
|
ds.Ignore = 0
|
|
ds.Ignore = 0
|
|
- case n, ok := <- ds.statChan:
|
|
|
|
|
|
+ case n, ok := <-ds.statChan:
|
|
if !ok {
|
|
if !ok {
|
|
break L
|
|
break L
|
|
}
|
|
}
|
|
- ds.LastTime = time.Now().UnixNano()/int64(time.Millisecond)
|
|
|
|
|
|
+ ds.LastTime = time.Now().UnixNano() / int64(time.Millisecond)
|
|
switch n {
|
|
switch n {
|
|
case 0:
|
|
case 0:
|
|
- ds.Success ++
|
|
|
|
- ds.TotalSuccess ++
|
|
|
|
|
|
+ ds.Success++
|
|
|
|
+ ds.TotalSuccess++
|
|
case 1:
|
|
case 1:
|
|
- ds.Fail ++
|
|
|
|
- ds.TotalFail ++
|
|
|
|
|
|
+ ds.Fail++
|
|
|
|
+ ds.TotalFail++
|
|
case 2:
|
|
case 2:
|
|
- ds.Ignore ++
|
|
|
|
- ds.TotalIgnore ++
|
|
|
|
|
|
+ ds.Ignore++
|
|
|
|
+ ds.TotalIgnore++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -188,4 +189,4 @@ func (ds *DataStat) GetName() string {
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
-//}
|
|
|
|
|
|
+//}
|