-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
119 lines (89 loc) · 2.38 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
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"benchmark-client/pkg"
"github.com/thinhdanggroup/executor"
"google.golang.org/grpc"
)
func main() {
executor, err := executor.New(executor.DefaultConfig())
checkErr(err)
noReq := int64(100000)
callGrpc(executor, "localhost:6790", noReq)
time.Sleep(2 * time.Second)
callHttp(executor, "http:https://localhost:6789", noReq)
}
func callGrpc(executor *executor.Executor, target string, noReq int64) {
// Prepare
request := &pkg.PingRequest{}
conn, err := grpc.Dial(target, grpc.WithInsecure())
checkErr(err)
defer conn.Close()
server := pkg.NewCoreServiceClient(conn)
// init connection
_, err = server.Ping(context.Background(), request)
checkErr(err)
// Run test
start := makeTimestamp()
for i := int64(0); i < noReq; i++ {
executor.Publish(
func() {
_, err := server.Ping(context.Background(), request)
if err != nil {
fmt.Errorf("Error request: %s", err)
}
})
}
executor.Wait()
execTime := (makeTimestamp() - start)
report("gRPC", noReq, execTime)
}
func callHttp(executor *executor.Executor, target string, noReq int64) {
// Prepare
url := target + "/api/ping"
method := "POST"
// Run
start := makeTimestamp()
for i := int64(0); i < noReq; i++ {
executor.Publish(postRequest, method, url)
}
executor.Wait()
execTime := (makeTimestamp() - start)
report("HTTP", noReq, execTime)
}
func postRequest(method string, url string) {
payload := strings.NewReader("{\n \"timestamp\": 20\n}")
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
checkErr(err)
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
checkErr(err)
defer res.Body.Close()
_, err = ioutil.ReadAll(res.Body)
if err != nil {
fmt.Errorf("Error request: %s", err)
}
// fmt.Println(string(body))
}
func report(testName string, total int64, execTime int64) {
fmt.Println("======================================")
fmt.Println("TestName: " + testName)
fmt.Println("Benchmark result for ", total, " request in ", execTime, " Microseconds")
fmt.Println("Throughtput :", total*1000000/execTime, "req/s")
fmt.Println("Latency :", execTime/total, "Microseconds")
fmt.Println("======================================")
}
func makeTimestamp() int64 {
return time.Now().UnixNano() / 1000
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}