From ae4a6259c2b45f04ad089870bb1e0681fe26601a Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Mon, 16 Sep 2019 18:20:49 +0300 Subject: [PATCH] Improve benchmark --- input_raw_test.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/input_raw_test.go b/input_raw_test.go index 517bdbd..f4dcf66 100644 --- a/input_raw_test.go +++ b/input_raw_test.go @@ -367,13 +367,19 @@ func TestInputRAWLargePayload(t *testing.T) { } func BenchmarkRAWInput(b *testing.B) { + var respCounter, reqCounter, replayCounter int64 + quit := make(chan int) origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})) defer origin.Close() originAddr := strings.Replace(origin.Listener.Addr().String(), "[::]", "127.0.0.1", -1) - var respCounter, reqCounter int64 + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + atomic.AddInt64(&replayCounter, 1) + })) + defer origin.Close() + upstreamAddr := strings.Replace(upstream.Listener.Addr().String(), "[::]", "127.0.0.1", -1) input := NewRAWInput(originAddr, EnginePcap, true, testRawExpire, "", "", "", 0) defer input.Close() @@ -384,27 +390,29 @@ func BenchmarkRAWInput(b *testing.B) { } else { atomic.AddInt64(&respCounter, 1) } - - // log.Println("Captured ", reqCounter, "requests and ", respCounter, " responses") }) + httpOutput := NewLimiter(NewHTTPOutput(upstreamAddr, &HTTPOutputConfig{}), "10%") + Plugins.Inputs = []io.Reader{input} - Plugins.Outputs = []io.Writer{output} + Plugins.Outputs = []io.Writer{output, httpOutput} go Start(quit) emitted := 0 fileContent, _ := ioutil.ReadFile("LICENSE.txt") + time.Sleep(400 * time.Millisecond) + for i := 0; i < b.N; i++ { wg := new(sync.WaitGroup) - wg.Add(10 * 100) - emitted += 10 * 100 + wg.Add(100 * 1000) + emitted += 100 * 1000 for w := 0; w < 100; w++ { go func() { client := NewHTTPClient(origin.URL, &HTTPClientConfig{}) - for i := 0; i < 10; i++ { - if rand.Int63n(2) == 0 { + for i := 0; i < 1000; i++ { + if i%2 == 0 { client.Post("/", fileContent) } else { client.Get("/") @@ -418,7 +426,7 @@ func BenchmarkRAWInput(b *testing.B) { } time.Sleep(400 * time.Millisecond) - log.Println("Emitted ", emitted, ", Captured ", reqCounter, "requests and ", respCounter, " responses") + log.Println("Emitted ", emitted, ", Captured ", reqCounter, "requests and ", respCounter, " responses", "and replayed", replayCounter) close(quit) }