-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhttp_json_test.go
52 lines (42 loc) · 1.05 KB
/
http_json_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
package benchmarks
import (
"bytes"
"encoding/json"
"net/http"
"testing"
"time"
"github.com/plutov/benchmark-grpc-protobuf-vs-http-json/http-json"
)
func init() {
go httpjson.Start()
time.Sleep(time.Second)
}
func BenchmarkHTTPJSON(b *testing.B) {
client := &http.Client{}
for n := 0; n < b.N; n++ {
doPost(client, b)
}
}
func doPost(client *http.Client, b *testing.B) {
u := &httpjson.User{
Email: "[email protected]",
Name: "Bench",
Password: "bench",
}
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(u)
resp, err := client.Post("http://127.0.0.1:60001/", "application/json", buf)
if err != nil {
b.Fatalf("http request failed: %v", err)
}
defer resp.Body.Close()
// We need to parse response to have a fair comparison as gRPC does it
var target httpjson.Response
decodeErr := json.NewDecoder(resp.Body).Decode(&target)
if decodeErr != nil {
b.Fatalf("unable to decode json: %v", decodeErr)
}
if target.Code != 200 || target.User == nil || target.User.ID != "1000000" {
b.Fatalf("http response is wrong: %v", resp)
}
}