This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmarks_test.go
98 lines (73 loc) · 2.11 KB
/
benchmarks_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
package fastjsonrpc_test
import (
"testing"
. "github.com/serjvanilla/fastjsonrpc"
"github.com/valyala/fasthttp"
)
func BenchmarkEchoHandler(b *testing.B) {
r := NewRepository()
r.Register("echo", func(ctx *RequestCtx) {
ctx.SetResult(ctx.Params())
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"echo","params":"hello","id":1}`)
handler := r.RequestHandler()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
handler(ctx)
}
}
func BenchmarkSumHandler(b *testing.B) {
r := NewRepository()
r.Register("sum", func(ctx *RequestCtx) {
params := ctx.Params()
a := params.GetInt("a")
b := params.GetInt("b")
ctx.SetResult(ctx.Arena().NewNumberInt(a + b))
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"sum","params":{"a":7,"b":42},"id":1}`)
handler := r.RequestHandler()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
handler(ctx)
}
}
func BenchmarkBatchSumHandler(b *testing.B) {
r := NewRepository()
r.Register("sum", func(ctx *RequestCtx) {
params := ctx.Params()
a := params.GetInt("a")
b := params.GetInt("b")
ctx.SetResult(ctx.Arena().NewNumberInt(a + b))
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(
`[{"jsonrpc":"2.0","method":"sum","params":{"a":7,"b":42},"id":1},
{"jsonrpc":"2.0","method":"sum","params":{"a":42,"b":7},"id":2}]`)
handler := r.RequestHandler()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
handler(ctx)
}
}
func BenchmarkErrorHandler(b *testing.B) {
r := NewRepository()
r.Register("error", func(req *RequestCtx) {
req.SetError(ErrServerError(ErrorCode(-32001)))
})
ctx := new(fasthttp.RequestCtx)
ctx.Request.Header.SetMethod(fasthttp.MethodPost)
ctx.Request.SetBodyString(`{"jsonrpc":"2.0","method":"error","id":"err"}`)
handler := r.RequestHandler()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ctx.Response.ResetBody()
handler(ctx)
}
}