-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
197 lines (171 loc) · 4.52 KB
/
stats.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package main
import (
"net/http"
// "strings"
"bytes"
"fmt"
"mime"
"runtime"
"time"
"github.com/GianlucaGuarini/go-observable"
"github.com/bitly/go-simplejson"
// "github.com/davecgh/go-spew/spew"
)
type Stats struct {
minGoRoutine int
maxGoRoutine int
minMem uint64
maxMem uint64
connCount int
connErrorCount int
connOKCount int
connMinTime time.Duration
connMaxTime time.Duration
connAvgTime time.Duration
// 真正的成功的网络连接数
connActiveCount int
chanActiveCount int
chanRealCount int
chan2RealCount int
closeReasons map[string]int
reqLen uint64
respLen uint64
reqNetLen uint64
respNetLen uint64
appmode string
selfOnline bool
peerOnline bool
selfOfflineCount int
peerOfflineCount int
}
func NewStates() *Stats {
crs := make(map[string]int, 0)
sts := new(Stats)
sts.closeReasons = crs
return sts
}
var sts = NewStates()
var appevt = observable.New()
func init() {
appevt.On("*", processEvent)
}
func processEvent(args ...interface{}) {
evt := args[0].(string)
switch evt {
case "newconn":
sts.connCount += 1
case "connerr":
sts.connErrorCount += 1
case "connok":
sts.connOKCount += 1
case "connact":
sts.connActiveCount += args[1].(int)
case "chanact":
sts.chanActiveCount += args[1].(int)
sts.chanRealCount = args[2].(int)
sts.chan2RealCount = args[3].(int)
case "closereason":
sts.closeReasons[args[1].(string)] += 1
case "reqbytes":
sts.reqLen += uint64(args[1].(int))
sts.reqNetLen += uint64(args[2].(int))
case "respbytes":
sts.respLen += uint64(args[1].(int))
sts.respNetLen += uint64(args[2].(int))
case "selfonline":
sts.selfOnline = args[1].(bool)
case "peeronline":
sts.peerOnline = args[1].(bool)
case "selfoffline":
sts.selfOfflineCount += 1
case "peeroffline":
sts.peerOfflineCount += 1
case "appmode":
sts.appmode = args[1].(string)
default:
}
}
func updateStats() {
rnum := runtime.NumGoroutine()
if sts.minGoRoutine == 0 {
sts.minGoRoutine = rnum
}
if rnum < sts.minGoRoutine {
sts.minGoRoutine = rnum
}
if rnum > sts.maxGoRoutine {
sts.maxGoRoutine = rnum
}
msts := runtime.MemStats{}
runtime.ReadMemStats(&msts)
if sts.minMem == 0 {
sts.minMem = msts.Alloc
}
if msts.Alloc < sts.minMem {
sts.minMem = msts.Alloc
}
if msts.Alloc > sts.maxMem {
sts.maxMem = msts.Alloc
}
}
func indexHandler(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", mime.TypeByExtension(".txt"))
w.WriteHeader(200)
rnum := runtime.NumGoroutine()
msts := runtime.MemStats{}
runtime.ReadMemStats(&msts)
wb := bytes.NewBufferString("")
str := fmt.Sprintf("toxtun stats: %v\n", sts.appmode)
str += fmt.Sprintf("Online Status: self: %v/%v, peer: %v/%v\n",
sts.selfOnline, sts.selfOfflineCount, sts.peerOnline, sts.peerOfflineCount)
str += fmt.Sprintf("Connections: total: %v, ok: %v, err: %v, active: %v, chans: %v, real1: %v, real2: %v\n",
sts.connCount, sts.connOKCount, sts.connErrorCount, sts.connActiveCount, sts.chanActiveCount,
sts.chanRealCount, sts.chan2RealCount)
str += fmt.Sprintf("Close Reasons: %v\n", sts.closeReasons)
dsrate := float64(0)
if sts.reqLen > 0 {
dsrate = float64(sts.respLen) / float64(sts.reqLen)
}
nsrate := float64(0)
if sts.respLen > 0 {
nsrate = float64(sts.respNetLen) / float64(sts.respLen)
}
str += fmt.Sprintf("DataStream: req: %v, resp: %v, r/s: 1/%.0f, reqnet: %v, respnet: %v, cost: %.2f\n",
sts.reqLen, sts.respLen, dsrate, sts.reqNetLen, sts.respNetLen, nsrate)
str += fmt.Sprintf("goroutines: cur: %v, max: %v, min: %v\n",
rnum, sts.maxGoRoutine, sts.minGoRoutine)
str += fmt.Sprintf("memory: cur: %v, max: %v, min: %v, total: %v\n",
msts.Alloc, sts.maxMem, sts.minMem, msts.TotalAlloc)
str += fmt.Sprintf("\nGo internals: %v\n", "")
str += fmt.Sprintf("goroutines: %v\n", rnum)
jso := simplejson.New()
jso.Set("MemStats", msts)
jbuf, err := jso.Encode()
jso, err = simplejson.NewJson(jbuf)
jso.Get("MemStats").Set("PauseNs", "--")
jso.Get("MemStats").Set("PauseEnd", "--")
jso.Get("MemStats").Set("BySize", "--")
jstr, err := jso.Get("MemStats").EncodePretty()
if err != nil {
}
str += "MemStats:\n"
str += string(jstr)
// str = str + spew.Sdump("mem stats: %v\n", msts)
wb.WriteString(str)
w.Write(wb.Bytes())
}
type StatServer struct {
}
func NewStatServer() *StatServer {
return &StatServer{}
}
func (this *StatServer) serve() {
go func() {
for {
updateStats()
time.Sleep(1234 * time.Millisecond)
}
}()
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":33444", nil)
}