-
Notifications
You must be signed in to change notification settings - Fork 12
/
http_test.go
145 lines (125 loc) · 3.15 KB
/
http_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"crypto/tls"
"crypto/x509"
"testing"
//"testing"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"sync"
//"golang.org/x/net/http2"
"github.com/Bimde/grpc-vs-rest/pb"
"golang.org/x/net/http2"
)
var client http.Client
func init() {
client = http.Client{}
}
// This code was taken from https://posener.github.io/http2/
func createTLSConfigWithCustomCert() *tls.Config {
// Create a pool with the server certificate since it is not signed
// by a known CA
caCert, err := ioutil.ReadFile("server/server.crt")
if err != nil {
log.Fatalf("Reading server certificate: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Create TLS configuration with the certificate of the server
return &tls.Config{
RootCAs: caCertPool,
}
}
// func BenchmarkHTTP2Get(b *testing.B) {
// client.Transport = &http2.Transport{
// TLSClientConfig: createTLSConfigWithCustomCert(),
// }
// var wg sync.WaitGroup
// wg.Add(b.N)
// for i := 0; i < b.N; i++ {
// go func() {
// get("https://bimde:8080", &pb.Random{})
// wg.Done()
// }()
// }
// wg.Wait()
// }
func get(path string, output interface{}) error {
req, err := http.NewRequest("GET", path, nil)
if err != nil {
log.Println("error creating request ", err)
return err
}
res, err := client.Do(req)
if err != nil {
log.Println("error executing request ", err)
return err
}
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println("error reading response body ", err)
return err
}
err = json.Unmarshal(bytes, output)
if err != nil {
log.Println("error unmarshalling response ", err)
return err
}
return nil
}
type Request struct {
Path string
Random *pb.Random
}
const stopRequestPath = "STOP"
const noWorkers = 4096
func BenchmarkHTTP2GetWithWokers(b *testing.B) {
client.Transport = &http2.Transport{
TLSClientConfig: createTLSConfigWithCustomCert(),
}
requestQueue := make(chan Request)
defer startWorkers(&requestQueue, noWorkers, startWorker)()
b.ResetTimer() // don't count worker initialization time
for i := 0; i < b.N; i++ {
requestQueue <- Request{Path: "https://bimde:8080", Random: &pb.Random{}}
}
}
func BenchmarkHTTP11Get(b *testing.B) {
client.Transport = &http.Transport{
TLSClientConfig: createTLSConfigWithCustomCert(),
}
requestQueue := make(chan Request)
defer startWorkers(&requestQueue, noWorkers, startWorker)()
b.ResetTimer() // don't count worker initialization time
for i := 0; i < b.N; i++ {
requestQueue <- Request{Path: "https://bimde:8080", Random: &pb.Random{}}
}
}
func startWorkers(requestQueue *chan Request, noWorkers int, startWorker func(*chan Request, *sync.WaitGroup)) func() {
var wg sync.WaitGroup
for i := 0; i < noWorkers; i++ {
startWorker(requestQueue, &wg)
}
return func() {
wg.Add(noWorkers)
stopRequest := Request{Path: stopRequestPath}
for i := 0; i < noWorkers; i++ {
*requestQueue <- stopRequest
}
wg.Wait()
}
}
func startWorker(requestQueue *chan Request, wg *sync.WaitGroup) {
go func() {
for {
request := <-*requestQueue
if request.Path == stopRequestPath {
wg.Done()
return
}
get(request.Path, request.Random)
}
}()
}