-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
122 lines (99 loc) · 2.79 KB
/
client.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/dollarkillerx/async_utils"
"github.com/dollarkillerx/light"
"github.com/dollarkillerx/light/cryptology"
"github.com/dollarkillerx/light_rpc_benchmark/grpc/proto"
"github.com/montanaflynn/stats"
"go.uber.org/atomic"
"google.golang.org/grpc"
)
var key = []byte("58a95a8f804b49e686f651a0d3f6e631")
func main() {
conn, e := grpc.Dial(":9001", grpc.WithInsecure()) // grpc.WithInsecure() 不安全的传输
if e != nil {
panic(e.Error())
}
client := proto.NewHelloClient(conn) // 注册上去
over := make(chan struct{})
poolFunc := async_utils.NewPoolFunc(100, func() {
close(over)
})
total := 1000000
summary := make([]int64, total, total)
suResp := atomic.Uint64{}
suOK := atomic.Uint64{}
stTime := time.Now().UnixNano()
for i := 0; i < total; i++ {
idx := i
poolFunc.Send(func() {
n := time.Now().UnixNano()
ctx := light.DefaultCtx()
ctx.SetTimeout(time.Second * 6)
// light rpc 不支持明文传输 所以加上
msg := "hello world"
resp, err := client.Say(context.TODO(), &proto.BenchmarkMessage{
Msg: coding([]byte(msg)),
})
if err == nil {
suResp.Add(1)
r := decoding(resp.Rp)
decoding(resp.Msg)
if string(r) == "ok" {
suOK.Add(1)
}
}
r := time.Now().UnixNano() - n
summary[idx] = r
})
}
poolFunc.Over()
fmt.Println("Send Over")
<-over
end := time.Now().UnixNano() - stTime
endTime := end / 1000000
fmt.Println("总耗时: ", endTime)
totalF6 := make([]float64, 0, total)
for _, k := range summary {
totalF6 = append(totalF6, float64(k))
}
mean, _ := stats.Mean(totalF6)
median, _ := stats.Median(totalF6)
max, _ := stats.Max(totalF6)
min, _ := stats.Min(totalF6)
p99, _ := stats.Percentile(totalF6, 99.9)
log.Printf("sent requests : %d\n", total)
log.Printf("received requests : %d\n", suResp.Load())
log.Printf("received requests_OK : %d\n", suOK.Load())
log.Printf("throughput (TPS) : %d\n", total*1000/int(endTime))
log.Printf("mean: %.f ns, median: %.f ns, max: %.f ns, min: %.f ns, p99: %.f ns\n", mean, median, max, min, p99)
log.Printf("mean: %d ms, median: %d ms, max: %d ms, min: %d ms, p99: %d ms\n", int64(mean/1000000), int64(median/1000000), int64(max/1000000), int64(min/1000000), int64(p99/1000000))
}
func coding(r []byte) []byte {
encrypt, err := cryptology.AESEncrypt(key, r)
if err != nil {
log.Fatalln(err)
}
//sn, _ := codes.CompressorManager.Get(codes.Snappy)
//zip, err := sn.Zip(encrypt)
//if err != nil {
// log.Fatalln(err)
//}
return encrypt
}
func decoding(r []byte) []byte {
//sn, _ := codes.CompressorManager.Get(codes.Snappy)
//rc, err := sn.Unzip(r)
//if err != nil {
// log.Fatalln(err)
//}
rb, err := cryptology.AESDecrypt(key, r)
if err != nil {
log.Fatalln(err)
}
return rb
}