-
Notifications
You must be signed in to change notification settings - Fork 13
/
handlerRegister_test.go
122 lines (106 loc) · 2.37 KB
/
handlerRegister_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package tgw
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"testing"
"time"
)
type Service struct {
}
type HelloArgs struct {
Who string
When int64
What string
}
const (
HOST = "localhost:2343"
HOST2 = "localhost:2344"
)
func (s *Service) Hello(args HelloArgs, env ReqEnv) (data map[string]interface{}) {
data = map[string]interface{}{}
data["who"] = args.Who
data["when"] = args.When
data["what"] = args.What
return
}
type HelloArgsRest struct {
Who string
When int64
What string
}
func (s *Service) Hellorest_(args HelloArgsRest, env ReqEnv) (data map[string]interface{}) {
data = map[string]interface{}{}
data["who"] = args.Who
data["when"] = args.When
data["what"] = args.What
return
}
// mock setup
func BenchmarkA(b *testing.B) {
go (func() {
svr := &Service{}
_tgw := NewTGW()
err := _tgw.RegisterREST(&svr).Run(HOST)
log.Fatalln(err)
})()
go (func() {
mux := http.NewServeMux()
mux.HandleFunc("/hello", func(rw http.ResponseWriter, req *http.Request) {
vals := req.URL.Query()
who := vals.Get("who")
when, _ := strconv.ParseInt(vals.Get("when"), 10, 64)
what := vals.Get("what")
data := map[string]interface{}{}
data["who"] = who
data["when"] = when
data["what"] = what
bs, _ := json.Marshal(data)
rw.Write(bs)
})
err := http.ListenAndServe(HOST2, mux)
log.Fatalln(err)
})()
time.Sleep(time.Second * 1)
}
func get(now int64, host string) {
resp, err := http.Get(fmt.Sprintf("http://%s/hello?who=icattlecoder&when=%d&what=hello", host, now))
if err != nil {
log.Fatalln("http.Get")
}
decoder := json.NewDecoder(resp.Body)
res := HelloArgs{}
err = decoder.Decode(&res)
if err != nil || res.When != now {
log.Fatalln("not equal")
}
resp.Body.Close()
}
func BenchmarkRegister(b *testing.B) {
for i := 0; i < b.N; i++ {
get(time.Now().Unix(), HOST)
}
}
func BenchmarkRegister2(b *testing.B) {
for i := 0; i < b.N; i++ {
get(time.Now().Unix(), HOST2)
}
}
func BenchmarkRegister3(b *testing.B) {
for i := 0; i < b.N; i++ {
now := time.Now().Unix()
resp, err := http.Get(fmt.Sprintf("http://%s/hellorest/who/icattlecoder/when/%d/what/hello", HOST, now))
if err != nil {
log.Fatalln("http.Get")
}
decoder := json.NewDecoder(resp.Body)
res := HelloArgsRest{}
err = decoder.Decode(&res)
if err != nil || res.When != now {
log.Fatalln("not equal")
}
resp.Body.Close()
}
}