Skip to content

Commit

Permalink
Add benchmark for testing packet capture (and its quality)
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Apr 26, 2016
1 parent 0754ed7 commit 00feb08
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
9 changes: 9 additions & 0 deletions http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"runtime/debug"
"strings"
"time"
"strconv"
)

var defaultPorts = map[string]string{
Expand Down Expand Up @@ -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"
Expand Down
60 changes: 60 additions & 0 deletions input_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sync/atomic"
"testing"
"time"
"math/rand"
)

const testRawExpire = time.Millisecond * 200
Expand Down Expand Up @@ -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)
}

0 comments on commit 00feb08

Please sign in to comment.