From 00feb08165f709c64c77a7e3213ed7870e53703c Mon Sep 17 00:00:00 2001 From: Leonid Bugaev Date: Tue, 26 Apr 2016 17:18:09 +0500 Subject: [PATCH] Add benchmark for testing packet capture (and its quality) --- http_client.go | 9 +++++++ input_raw_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/http_client.go b/http_client.go index 5ce8125..70ad56e 100644 --- a/http_client.go +++ b/http_client.go @@ -10,6 +10,7 @@ import ( "runtime/debug" "strings" "time" + "strconv" ) var defaultPorts = map[string]string{ @@ -202,6 +203,14 @@ func (c *HTTPClient) Get(path string) (response []byte, err error) { return c.Send([]byte(payload)) } +func (c *HTTPClient) Post(path string, body []byte) (response []byte, err error) { + payload := "POST " + path + " HTTP/1.1\r\n" + payload += "Content-Length: " + strconv.Itoa(len(body)) + "\r\n\r\n" + payload += string(body) + + return c.Send([]byte(payload)) +} + const ( // https://support.cloudflare.com/hc/en-us/articles/200171936-Error-520-Web-server-is-returning-an-unknown-error HTTP_UNKNOWN_ERROR = "520" diff --git a/input_raw_test.go b/input_raw_test.go index f381493..aada58a 100644 --- a/input_raw_test.go +++ b/input_raw_test.go @@ -15,6 +15,7 @@ import ( "sync/atomic" "testing" "time" + "math/rand" ) const testRawExpire = time.Millisecond * 200 @@ -241,3 +242,62 @@ func TestInputRAWLargePayload(t *testing.T) { wg.Wait() close(quit) } + +func BenchmarkRAWInput(b *testing.B) { + 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 + + input := NewRAWInput(originAddr, testRawExpire) + defer input.Close() + + output := NewTestOutput(func(data []byte) { + if data[0] == '1' { + atomic.AddInt64(&reqCounter, 1) + } else { + atomic.AddInt64(&respCounter, 1) + } + + // log.Println("Captured ", reqCounter, "requests and ", respCounter, " responses") + }) + + Plugins.Inputs = []io.Reader{input} + Plugins.Outputs = []io.Writer{output} + + time.Sleep(time.Millisecond) + + go Start(quit) + + emitted := 0 + fileContent, _ := ioutil.ReadFile("LICENSE.txt") + + for i := 0; i < b.N; i++ { + wg := new(sync.WaitGroup) + wg.Add(10 * 100) + emitted += 10*100 + for w := 0; w < 100; w++ { + go func(){ + client := NewHTTPClient(origin.URL, &HTTPClientConfig{}) + for i := 0; i < 10; i++ { + if rand.Int63n(2) == 0 { + client.Post("/", fileContent) + } else { + client.Get("/") + } + time.Sleep(time.Duration(rand.Int63n(50)) * time.Millisecond) + wg.Done() + } + }() + } + wg.Wait() + } + + time.Sleep(201 * time.Millisecond) + log.Println("Emitted ", emitted, ", Captured ", reqCounter, "requests and ", respCounter, " responses") + + close(quit) +}