generated from amitshekhariitbhu/go-backend-clean-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpserver_test.go
83 lines (72 loc) · 1.71 KB
/
httpserver_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
package dudu_com
import (
"context"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"github.com/valyala/fasthttp"
"log"
"net/http"
"testing"
)
func BenchmarkHertzServer(b *testing.B) {
pid := prepareHertzServer(b)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
c := &http.Client{}
for pb.Next() {
_, err := c.Get("http://localhost:8080/v1/test")
if err != nil {
b.Fatalf("Error: %v", err)
}
}
})
b.StopTimer()
stopChildProcess(b, pid)
}
func BenchmarkHertzServerWithHertzClient(b *testing.B) {
pid := prepareHertzServer(b)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
c, err := client.NewClient()
if err != nil {
b.Fatalf("Error:%v", err)
}
req := &protocol.Request{}
req.SetMethod(consts.MethodGet)
req.SetRequestURI("http://localhost:8080/v1/test")
for pb.Next() {
res := getResponse()
err = c.Do(context.Background(), req, res)
if err != nil {
b.Fatalf("Error: %v", err)
}
putResponse(res)
}
})
b.StopTimer()
stopChildProcess(b, pid)
}
func BenchmarkHertzServerWithFasthttpClient(b *testing.B) {
pid := prepareHertzServer(b)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
c := &fasthttp.HostClient{
Addr: "localhost:8080",
}
for pb.Next() {
statusCode, _, err := c.Get(nil, "http://localhost:8080/v1/test")
if err != nil {
log.Fatalf("Error when request through local proxy: %v", err)
}
if statusCode != fasthttp.StatusOK {
log.Fatalf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
if err != nil {
b.Fatalf("Error: %v", err)
}
}
})
b.StopTimer()
stopChildProcess(b, pid)
}