Skip to content

Commit

Permalink
Add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwilkie committed May 4, 2016
1 parent b4f3ae6 commit 0bacaaa
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/merger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app_test

import (
"fmt"
"math/rand"
"testing"

"github.com/weaveworks/scope/app"
Expand Down Expand Up @@ -65,3 +67,45 @@ func TestSmartMerger(t *testing.T) {
t.Errorf("Bad merge: %s", test.Diff(have, want))
}
}

func BenchmarkSmartMerger(b *testing.B) {
benchmarMerger(b, app.NewSmartMerger())
}

func BenchmarkDumbMerger(b *testing.B) {
benchmarMerger(b, app.MakeDumbMerger())
}

const NUM_HOSTS = 15

func benchmarMerger(b *testing.B, merger app.Merger) {
makeReport := func() report.Report {
rpt := report.MakeReport()
for i := 0; i < 100; i++ {
rpt.Endpoint.AddNode(report.MakeNode(fmt.Sprintf("%x", rand.Int63())))
}
return rpt
}

reports := []report.Report{}
for i := 0; i < NUM_HOSTS*5; i++ {
reports = append(reports, makeReport())
}
merger.Merge(reports) // prime the cache

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
b.StartTimer()

// replace 1/3 of hosts work of reports & merge them all
for i := 0; i < NUM_HOSTS/3; i++ {
reports[rand.Intn(len(reports))] = makeReport()
}

merger.Merge(reports)

b.StopTimer()
}
}

0 comments on commit 0bacaaa

Please sign in to comment.