-
Notifications
You must be signed in to change notification settings - Fork 2
/
performance_test.go
103 lines (83 loc) · 2.08 KB
/
performance_test.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
// Copyright 2020 Ye Zi Jie. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
//
// Author: FishGoddess
// Email: [email protected]
// Created at 2020/10/01 16:31:41
package main
import (
"net/http"
"strconv"
"strings"
"testing"
"time"
"github.com/avino-plan/kafo/servers"
)
const (
// keySize is the key size of test.
keySize = 10000
)
// testTask is a wrapper wraps task to testTask.
func testTask(task func(no int)) string {
beginTime := time.Now()
for i := 0; i < keySize; i++ {
task(i)
}
return time.Now().Sub(beginTime).String()
}
// go test -v -count=1 performance_test.go -run=^TestHttpServer$
func TestHttpServer(t *testing.T) {
writeTime := testTask(func(no int) {
data := strconv.Itoa(no)
request, err := http.NewRequest("PUT", "http://localhost:5837/v1/cache/"+data, strings.NewReader(data))
if err != nil {
t.Fatal(err)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
response.Body.Close()
})
t.Logf("写入消耗时间为 %s!", writeTime)
time.Sleep(3 * time.Second)
readTime := testTask(func(no int) {
data := strconv.Itoa(no)
request, err := http.NewRequest("GET", "http://localhost:5837/v1/cache/"+data, nil)
if err != nil {
t.Fatal(err)
}
response, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatal(err)
}
response.Body.Close()
})
t.Logf("读取消耗时间为 %s!", readTime)
}
// go test -v -count=1 performance_test.go -run=^TestTcpServer$
func TestTcpServer(t *testing.T) {
client, err := servers.NewTCPClient("127.0.0.1:5837")
if err != nil {
t.Fatal(err)
}
defer client.Close()
writeTime := testTask(func(no int) {
data := strconv.Itoa(no)
err := client.Set(data, []byte(data), 0)
if err != nil {
t.Fatal(err)
}
})
t.Logf("写入消耗时间为 %s!", writeTime)
time.Sleep(3 * time.Second)
readTime := testTask(func(no int) {
data := strconv.Itoa(no)
_, err := client.Get(data)
if err != nil {
t.Fatal(err)
}
})
t.Logf("读取消耗时间为 %s!", readTime)
}