Skip to content

Commit

Permalink
feat: add benchmark test for sse (#7)
Browse files Browse the repository at this point in the history
* feat: add benchmark for sse

* update benchmarks
  • Loading branch information
Skyenought authored Nov 19, 2023
1 parent 457fbba commit ccfd7f3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func TestNewStream(t *testing.T) {
assert.NotNil(t, s)
}

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)
}

type BufferExtWriter struct {
buffer *bytes.Buffer
}
Expand Down Expand Up @@ -136,6 +150,21 @@ 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,
}
b.ResetTimer()
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")
Expand Down

0 comments on commit ccfd7f3

Please sign in to comment.