-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmarks_test.go
More file actions
114 lines (91 loc) · 2.62 KB
/
benchmarks_test.go
File metadata and controls
114 lines (91 loc) · 2.62 KB
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
package fastjsonrpc_test
import (
"testing"
"github.com/valyala/fasthttp"
. "github.com/zc310/fastjsonrpc"
)
func BenchmarkEchoHandler(b *testing.B) {
b.ReportAllocs()
s := new(ServerMap)
s.RegisterHandler("echo", func(c *RequestCtx) { c.Result = c.Params })
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"echo","params":"hello","id":3}`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
s.Handler(ctx)
}
}
func BenchmarkSumHandler(b *testing.B) {
b.ReportAllocs()
s := new(ServerMap)
s.RegisterHandler("sum", func(c *RequestCtx) {
c.Result = c.Params.GetInt("a") + c.Params.GetInt("b")
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"sum","params":{"a":3,"b":6},"id":9}`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
s.Handler(ctx)
}
}
func BenchmarkErrorHandler(b *testing.B) {
b.ReportAllocs()
s := new(ServerMap)
s.RegisterHandler("error", func(c *RequestCtx) {
c.Error = NewError(-32000, "Server error")
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc": "2.0", "method": "error", "id": "1"}`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
s.Handler(ctx)
}
}
func BenchmarkBatchSumHandler(b *testing.B) {
b.ReportAllocs()
s := new(ServerMap)
s.RegisterHandler("sum", func(c *RequestCtx) {
c.Result = c.Params.GetInt("a") + c.Params.GetInt("b")
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`k
[
{ "jsonrpc": "2.0", "method": "sum", "params": { "a": 3, "b": 3 }, "id": 3 },
{ "jsonrpc": "2.0", "method": "sum", "params": { "a": 6, "b": 6 }, "id": 6 },
{ "jsonrpc": "2.0", "method": "sum", "params": { "a": 9, "b": 9 }, "id": 9 }
]`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
s.Handler(ctx)
}
}
func BenchmarkParamsUnmarshalHandler(b *testing.B) {
type Args struct {
A int `json:"a,omitempty"`
B int `json:"b,omitempty"`
}
b.ReportAllocs()
s := new(ServerMap)
s.RegisterHandler("sum", func(c *RequestCtx) {
var a Args
if c.Error = c.ParamsUnmarshal(&a); c.Error == nil {
c.Result = a.A + a.B
}
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"sum","params":{"a":3,"b":6},"id":9}`)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
s.Handler(ctx)
}
}