-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
200 lines (173 loc) · 4.43 KB
/
main.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
198
199
200
package main
import (
"encoding/json"
"os"
"runtime"
"log"
"time"
"sync"
"github.com/lzjluzijie/gobench/bench"
"github.com/urfave/cli"
)
var SpeedTests = []*bench.SpeedTest{
bench.NewSpeedTest("北京联通", "http://www2.unicomtest.com:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("上海联通", "http://211.95.17.50:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("北京电信", "http://st1.bjtelecom.net:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("广州电信", "http://gzspeedtest.com:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("深圳移动", "http://speedtest3.gd.chinamobile.com:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("北京移动", "http://speedtest.bmcc.com.cn:8080/download?size=10485760", 10485760),
bench.NewSpeedTest("东京Linode", "http://speedtest.tokyo.linode.com/100MB-tokyo.bin", 104857600),
bench.NewSpeedTest("洛杉矶Psychz", "http://lg.lax.psychz.net/200MB.test", 209715200),
bench.NewSpeedTest("CacheFly", "http://cachefly.cachefly.net/100mb.test", 104857600),
}
var TraceRoutes = []*bench.TraceRoute{
bench.NewTraceRoute("北京联通", "www2.unicomtest.com"),
bench.NewTraceRoute("上海联通", "211.95.17.50"),
bench.NewTraceRoute("北京电信", "st1.bjtelecom.net"),
bench.NewTraceRoute("广州电信", "gzspeedtest.com"),
bench.NewTraceRoute("北京移动", "speedtest.bmcc.com.cn"),
bench.NewTraceRoute("东京Linode", "speedtest.tokyo.linode.com"),
bench.NewTraceRoute("洛杉矶Psychz", "lg.lax.psychz.net"),
}
func info(c *cli.Context) (err error) {
info, err := bench.GetInfo()
if err != nil {
log.Println(err.Error())
}
j, err := json.Marshal(info)
if err != nil {
return
}
log.Println(string(j))
return
}
func cpu(c *cli.Context) (err error) {
threads := runtime.NumCPU()
b := bench.NewSHA3Bench(threads, 1*1048576, 10*time.Second)
log.Printf(b.Result())
return
}
func memory(c *cli.Context) (err error) {
b := bench.NewMemoryBench(1024, 10000)
log.Printf(b.Result())
b = bench.NewMemoryBench(1024*1024, 500)
log.Printf(b.Result())
return
}
func disk(c *cli.Context) (err error) {
b := bench.NewDiskBench(1024, 10*time.Second)
log.Println(b.Result())
b = bench.NewDiskBench(1024*1024, 10*time.Second)
log.Println(b.Result())
return
}
func speedtest(c *cli.Context) (err error) {
for _, st := range SpeedTests {
log.Printf(st.Result())
}
return
}
func traceroute(c *cli.Context) (err error) {
wg := sync.WaitGroup{}
for _, tr := range TraceRoutes {
wg.Add(1)
t := tr
go func() {
defer wg.Done()
err = t.Do()
if err != nil {
log.Println(err.Error())
return
}
log.Println(t.Result)
}()
}
wg.Wait()
return
}
func main() {
app := cli.NewApp()
app.Name = "GoBench"
app.Version = "0.3.0"
app.Usage = "A simple benchmark tool"
app.Description = "See https://github.com/lzjluzijie/gobench"
app.Author = "Halulu"
app.Email = "[email protected]"
app.Action = func(c *cli.Context) (err error) {
info, err := bench.GetInfo()
if err != nil {
log.Println(err.Error())
}
j, err := json.Marshal(info)
if err != nil {
return
}
log.Println(string(j))
threads := runtime.NumCPU()
cb := bench.NewSHA3Bench(threads, 1*1048576, 10*time.Second)
log.Printf(cb.Result())
mb := bench.NewMemoryBench(1024, 10000)
log.Printf(mb.Result())
mb = bench.NewMemoryBench(1024*1024, 500)
log.Printf(mb.Result())
db := bench.NewDiskBench(1024, 10*time.Second)
log.Println(db.Result())
db = bench.NewDiskBench(1024*1024, 10*time.Second)
log.Println(db.Result())
wg := sync.WaitGroup{}
for _, tr := range TraceRoutes {
wg.Add(1)
t := tr
go func() {
defer wg.Done()
err = t.Do()
if err != nil {
log.Println(err.Error())
return
}
log.Println(t.Result)
}()
}
for _, st := range SpeedTests {
log.Printf(st.Result())
}
wg.Wait()
return
}
app.Commands = []cli.Command{
{
Name: "info",
Usage: "Print system info",
Action: info,
},
{
Name: "cpu",
Usage: "Run cpu benchmark",
Action: cpu,
},
{
Name: "memory",
Usage: "Run memory benchmark",
Action: memory,
},
{
Name: "disk",
Usage: "Run disk benchmark",
Action: disk,
},
{
Name: "speedtest",
Usage: "Run speedtest",
Action: speedtest,
},
{
Name: "traceroute",
Usage: "Run traceroute",
Action: traceroute,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatalf(err.Error())
}
}