Add benchmark tests for the AsciiJSON and JsonpJSON

This commit is contained in:
mstmdev 2022-03-25 13:23:11 +08:00
parent caf7809e58
commit cb2e89b1ca
1 changed files with 30 additions and 0 deletions

View File

@ -510,3 +510,33 @@ func TestRenderReaderNoContentLength(t *testing.T) {
assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition")) assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition"))
assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id")) assert.Equal(t, headers["x-request-id"], w.Header().Get("x-request-id"))
} }
func BenchmarkAsciiJSONRender(b *testing.B) {
data := getRenderData()
benchmarkRender(b, AsciiJSON{Data: data})
}
func BenchmarkJsonpJSONRender(b *testing.B) {
data := getRenderData()
benchmarkRender(b, JsonpJSON{Data: data})
}
func benchmarkRender(b *testing.B, r Render) {
w := httptest.NewRecorder()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := r.Render(w)
if err != nil {
b.Errorf("test json render error =>%v", err)
}
}
}
func getRenderData() map[string]any {
data := map[string]any{
"cn": "<h1>你好 世界</h1>",
"en": "<h1>hello world</h1>",
}
return data
}