Last active
October 1, 2016 18:00
-
-
Save toefel18/96edac8f57f9ad8a4f9a86d83e726aec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestConcurrency(t *testing.T) { | |
Benchmrk(10, 20000) | |
Benchmrk(100, 20000) | |
Benchmrk(1000, 20000) | |
Benchmrk(10, 200000) | |
Benchmrk(100, 200000) | |
} | |
func Benchmrk(threads int64, itemsPerThread int64) { | |
millisStart := currentTimeMillis() | |
wg := sync.WaitGroup{} | |
subject := lockbased.NewFacade(lockbased.NewStore()) | |
for i := int64(0); i < threads; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
sw := StartStopwatch() | |
defer subject.RecordElapsedTime("goroutine.duration", sw) | |
for i := int64(0); i < itemsPerThread; i++ { | |
subject.IncrementCounter("concurrency.counter") | |
subject.AddSample("concurrency.sample", i) | |
} | |
}() | |
} | |
wg.Wait() | |
snapshot := subject.Snapshot() | |
expectedItems := threads * itemsPerThread | |
if snapshot.Counters()["concurrency.counter"] != expectedItems { | |
panic(fmt.Sprint(expectedItems, "counters expected, but got", snapshot.Counters()["concurrency.counter"])) | |
} | |
if snapshot.Durations()["goroutine.duration"].SampleCount() != threads { | |
panic(fmt.Sprint("There should be",threads, "durations registered but got", snapshot.Durations()["goroutine.duration"].SampleCount())) | |
} | |
if snapshot.Samples()["concurrency.sample"].SampleCount() != threads * itemsPerThread{ | |
panic(fmt.Sprint(expectedItems, "samples expected but got", snapshot.Samples()["concurrency.sample"].SampleCount())) | |
} | |
millisEnd := currentTimeMillis() | |
fmt.Println(threads, "threads with", itemsPerThread, "items took", (millisEnd - millisStart)) | |
} | |
func currentTimeMillis() int64 { | |
return time.Now().UnixNano() / time.Millisecond.Nanoseconds() | |
} | |
// Results | |
// 10 threads with 20000 items took 133 | |
// 100 threads with 20000 items took 1809 | |
// 1000 threads with 20000 items took 17576 | |
// 10 threads with 200000 items took 1228 | |
// 100 threads with 200000 items took 17900 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment