forked from leesper/tao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
75 lines (63 loc) · 1.42 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package stw
import (
"expvar"
"fmt"
"net/http"
"strconv"
"github.com/leesper/holmes"
)
var (
handleExported *expvar.Int
connExported *expvar.Int
timeExported *expvar.Float
qpsExported *expvar.Float
)
func init() {
handleExported = expvar.NewInt("TotalHandle")
connExported = expvar.NewInt("TotalConn")
timeExported = expvar.NewFloat("TotalTime")
qpsExported = expvar.NewFloat("QPS")
}
// MonitorOn starts up an HTTP monitor on port.
func MonitorOn(port int) {
go func() {
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
holmes.Errorln(err)
return
}
}()
}
func addTotalConn(delta int64) {
connExported.Add(delta)
calculateQPS()
}
func addTotalHandle() {
handleExported.Add(1)
calculateQPS()
}
func addTotalTime(seconds float64) {
timeExported.Add(seconds)
calculateQPS()
}
func calculateQPS() {
totalConn, err := strconv.ParseInt(connExported.String(), 10, 64)
if err != nil {
holmes.Errorln(err)
return
}
totalTime, err := strconv.ParseFloat(timeExported.String(), 64)
if err != nil {
holmes.Errorln(err)
return
}
totalHandle, err := strconv.ParseInt(handleExported.String(), 10, 64)
if err != nil {
holmes.Errorln(err)
return
}
if float64(totalConn)*totalTime != 0 {
// take the average time per worker go-routine
qps := float64(totalHandle) / (float64(totalConn) * (totalTime / float64(WorkerPoolInstance().Size())))
qpsExported.Set(qps)
}
}