Skip to content

Commit

Permalink
test: handle empty newlines in BenchmarkPublisher
Browse files Browse the repository at this point in the history
  • Loading branch information
kruskall committed Jun 24, 2024
1 parent 477584b commit 75d478b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions internal/publish/pub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/stretchr/testify/require"

"go.elastic.co/apm/v2/apmtest"
"go.elastic.co/fastjson"

"github.com/elastic/beats/v7/libbeat/beat"
"github.com/elastic/beats/v7/libbeat/idxmgmt"
Expand Down Expand Up @@ -97,6 +98,8 @@ func TestPublisherStopShutdownInactive(t *testing.T) {
}

func BenchmarkPublisher(b *testing.B) {
require.NoError(b, logp.DevelopmentSetup(logp.ToObserverOutput()))

mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Elastic-Product", "Elasticsearch")
Expand All @@ -109,14 +112,30 @@ func BenchmarkPublisher(b *testing.B) {
assert.NoError(b, err)
defer gzr.Close()

var jsonw fastjson.Writer
jsonw.RawString(`{"items":[`)
first := true

scanner := bufio.NewScanner(gzr)
var n int64
for scanner.Scan() { // index
if scanner.Scan() { // actual event
n++

// stop if there's no more data or we bump into an empty line
// Prevent an issue with clients appending newlines to
// valid requests
for scanner.Scan() && len(scanner.Bytes()) != 0 { // index
require.True(b, scanner.Scan())

if first {
first = false
} else {
jsonw.RawByte(',')
}
jsonw.RawString(`{"create":{"status":201}}`)
n++
}
assert.NoError(b, scanner.Err())
jsonw.RawString(`]}`)
w.Write(jsonw.Bytes())
indexed.Add(n)
})
srv := httptest.NewServer(mux)
Expand All @@ -137,9 +156,12 @@ func BenchmarkPublisher(b *testing.B) {
namespace := config.Namespace{}
err = conf.Unpack(&namespace)
require.NoError(b, err)

pipeline, err := pipeline.New(
beat.Info{},
pipeline.Monitors{},
pipeline.Monitors{
Logger: logp.NewLogger("monitor"),
},
namespace,
outputGroup,
pipeline.Settings{
Expand Down

0 comments on commit 75d478b

Please sign in to comment.