From cb2e89b1ca406676bdbd9335095df83a58ee22ca Mon Sep 17 00:00:00 2001 From: mstmdev Date: Fri, 25 Mar 2022 13:23:11 +0800 Subject: [PATCH] Add benchmark tests for the AsciiJSON and JsonpJSON --- render/render_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/render/render_test.go b/render/render_test.go index 8b28dc3f..71e2f330 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -510,3 +510,33 @@ func TestRenderReaderNoContentLength(t *testing.T) { assert.Equal(t, headers["Content-Disposition"], w.Header().Get("Content-Disposition")) 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": "

你好 世界

", + "en": "

hello world

", + } + return data +}