From 2c487ec040769c1ae5ab4d2f5c495bfcb3ad4a5c Mon Sep 17 00:00:00 2001 From: Skyenought Date: Sun, 19 Nov 2023 15:40:05 +0800 Subject: [PATCH 1/2] feat: add benchmark for sse --- sse_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/sse_test.go b/sse_test.go index e4e0cec..d84f5e4 100644 --- a/sse_test.go +++ b/sse_test.go @@ -106,6 +106,18 @@ func TestNewStream(t *testing.T) { assert.NotNil(t, s) } +func BenchmarkNewStream(b *testing.B) { + var c app.RequestContext + for i := 0; i < b.N; i++ { + s := NewStream(&c) + + assert.DeepEqual(b, ContentType, string(c.Response.Header.ContentType())) + assert.DeepEqual(b, noCache, c.Response.Header.Get(cacheControl)) + assert.NotNil(b, c.Response.GetHijackWriter()) + assert.NotNil(b, s) + } +} + type BufferExtWriter struct { buffer *bytes.Buffer } @@ -136,6 +148,20 @@ func TestStreamPublish(t *testing.T) { assert.DeepEqual(t, "data:hertz\n\n", buffer.String()) } +func BenchmarkStream_Publish(b *testing.B) { + var c app.RequestContext + s := NewStream(&c) + var buffer bytes.Buffer + s.w = &BufferExtWriter{ + buffer: &buffer, + } + for i := 0; i < b.N; i++ { + s.Publish(&Event{ + Data: []byte("hertz"), + }) + } +} + func TestLastEventID(t *testing.T) { var c app.RequestContext c.Request.Header.Set(LastEventID, "1") From 2bba471e62d94f8272611a0d0365e4da5608008e Mon Sep 17 00:00:00 2001 From: Skyenought Date: Sun, 19 Nov 2023 22:06:27 +0800 Subject: [PATCH 2/2] update benchmarks --- sse_test.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/sse_test.go b/sse_test.go index d84f5e4..9bc4b04 100644 --- a/sse_test.go +++ b/sse_test.go @@ -108,14 +108,16 @@ func TestNewStream(t *testing.T) { func BenchmarkNewStream(b *testing.B) { var c app.RequestContext + var s *Stream + b.ResetTimer() for i := 0; i < b.N; i++ { - s := NewStream(&c) - - assert.DeepEqual(b, ContentType, string(c.Response.Header.ContentType())) - assert.DeepEqual(b, noCache, c.Response.Header.Get(cacheControl)) - assert.NotNil(b, c.Response.GetHijackWriter()) - assert.NotNil(b, s) + s = NewStream(&c) } + + assert.DeepEqual(b, ContentType, string(c.Response.Header.ContentType())) + assert.DeepEqual(b, noCache, c.Response.Header.Get(cacheControl)) + assert.NotNil(b, c.Response.GetHijackWriter()) + assert.NotNil(b, s) } type BufferExtWriter struct { @@ -155,6 +157,7 @@ func BenchmarkStream_Publish(b *testing.B) { s.w = &BufferExtWriter{ buffer: &buffer, } + b.ResetTimer() for i := 0; i < b.N; i++ { s.Publish(&Event{ Data: []byte("hertz"),