Skip to content

Commit

Permalink
💄 Reorganize tests and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bigomby committed Aug 23, 2016
1 parent aac2432 commit 97619dc
Show file tree
Hide file tree
Showing 5 changed files with 501 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ errcheck:
errcheck -ignoretests -verbose ./...

vet:
@printf "$(MKL_YELLOW)Runing go vet$(MKL_CLR_RESET)\n"
@printf "$(MKL_YELLOW)Running go vet$(MKL_CLR_RESET)\n"
go vet ./...

test:
@printf "$(MKL_YELLOW)Runing tests$(MKL_CLR_RESET)\n"
go test -race ./...
@printf "$(MKL_YELLOW)Running tests$(MKL_CLR_RESET)\n"
go test -race ./... -tags=integration
@printf "$(MKL_GREEN)Test passed$(MKL_CLR_RESET)\n"

coverage:
Expand Down
178 changes: 178 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package rbforwarder

import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"

"github.com/redBorder/rbforwarder/components/batch"
"github.com/redBorder/rbforwarder/components/httpsender"
)

func NewTestClient(code int, cb func(*http.Request)) *http.Client {
server := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
cb(r)
}))

transport := &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(server.URL)
},
}

return &http.Client{Transport: transport}
}

func BenchmarkNoBatch(b *testing.B) {
var components []interface{}
var workers []int

f := NewRBForwarder(Config{
Retries: 3,
Backoff: 5,
QueueSize: 1,
})

batch := &batcher.Batcher{
Config: batcher.Config{
TimeoutMillis: 1000,
Limit: 10000,
},
}
components = append(components, batch)
workers = append(workers, 1)

sender := &httpsender.HTTPSender{
URL: "http://localhost:8888",
Client: NewTestClient(200, func(r *http.Request) {}),
}
components = append(components, sender)
workers = append(workers, 1)

f.PushComponents(components, workers)
f.Run()

opts := map[string]interface{}{
"http_endpoint": "librb-http",
"batch_group": "librb-http",
}

for i := 0; i < b.N; i++ {
data := fmt.Sprintf("{\"message\": %d}", i)
f.Produce([]byte(data), opts, i)
}

for report := range f.GetReports() {
r := report.(Report)
if r.Code > 0 {
b.FailNow()
}
if r.Opaque.(int) == b.N-1 {
break
}
}
}

func BenchmarkLittleBatch(b *testing.B) {
var components []interface{}
var workers []int

f := NewRBForwarder(Config{
Retries: 3,
Backoff: 5,
QueueSize: b.N / 100,
})

batch := &batcher.Batcher{
Config: batcher.Config{
TimeoutMillis: 1000,
Limit: 10000,
},
}
components = append(components, batch)
workers = append(workers, 1)

sender := &httpsender.HTTPSender{
URL: "http://localhost:8888",
Client: NewTestClient(200, func(r *http.Request) {}),
}
components = append(components, sender)
workers = append(workers, 1)

f.PushComponents(components, workers)
f.Run()

opts := map[string]interface{}{
"http_endpoint": "librb-http",
"batch_group": "librb-http",
}

for i := 0; i < b.N; i++ {
data := fmt.Sprintf("{\"message\": %d}", i)
f.Produce([]byte(data), opts, i)
}

for report := range f.GetReports() {
r := report.(Report)
if r.Code > 0 {
b.FailNow()
}
if r.Opaque.(int) == b.N-1 {
break
}
}
}

func BenchmarkBigBatch(b *testing.B) {
var components []interface{}
var workers []int

f := NewRBForwarder(Config{
Retries: 3,
Backoff: 5,
QueueSize: b.N / 10,
})

batch := &batcher.Batcher{
Config: batcher.Config{
TimeoutMillis: 1000,
Limit: 10000,
},
}
components = append(components, batch)
workers = append(workers, 1)

sender := &httpsender.HTTPSender{
URL: "http://localhost:8888",
Client: NewTestClient(200, func(r *http.Request) {}),
}
components = append(components, sender)
workers = append(workers, 1)

f.PushComponents(components, workers)
f.Run()

opts := map[string]interface{}{
"http_endpoint": "librb-http",
"batch_group": "librb-http",
}

for i := 0; i < b.N; i++ {
data := fmt.Sprintf("{\"message\": %d}", i)
f.Produce([]byte(data), opts, i)
}

for report := range f.GetReports() {
r := report.(Report)
if r.Code > 0 {
b.FailNow()
}
if r.Opaque.(int) == b.N-1 {
break
}
}
}

0 comments on commit 97619dc

Please sign in to comment.