Skip to content

Instantly share code, notes, and snippets.

@menghan
Last active December 23, 2015 20:09
Show Gist options
  • Save menghan/6687354 to your computer and use it in GitHub Desktop.
Save menghan/6687354 to your computer and use it in GitHub Desktop.
package main
import (
"testing"
)
func TestDummy(t *testing.T) {
}
func Benchmark1024Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 1024, 10)
}
func Benchmark512Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 512, 10)
}
func Benchmark256Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 256, 10)
}
func Benchmark128Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 128, 10)
}
func Benchmark64Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 64, 10)
}
func Benchmark32Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 32, 10)
}
func Benchmark16Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 16, 10)
}
func Benchmark8Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 8, 10)
}
func Benchmark4Concurrency(b *testing.B) {
benchConcurrency(b, 10240, 4, 10)
}
func benchConcurrency(b *testing.B, nReq int, nClient int, nServer int) {
var i, j int
finish := make(chan bool, nClient)
cRChans := make([]chan bool, 0, nClient)
for i = 0; i < nClient; i++ {
cRChans = append(cRChans, make(chan bool))
}
sRChans := make([]chan chan bool, 0, nClient)
for i = 0; i < nServer; i++ {
sRChans = append(sRChans, make(chan chan bool))
}
for i = 0; i < nServer; i++ {
go func(j int) {
for {
<-sRChans[j] <- true
}
}(i)
}
rpc := nReq / nClient
for i = 0; i < nClient; i++ {
go func(j int) {
for {
for k := 0; k < rpc; k++ {
<-cRChans[j]
}
finish <- true
}
}(i)
}
b.ResetTimer()
for i = 0; i < b.N; i++ {
for j = 0; j < nClient; j++ {
go func(p int) {
for k := 0; k < rpc; k++ {
sRChans[(k*nClient+p)%nServer] <- cRChans[p]
}
}(j)
}
for j = 0; j < nClient; j++ {
<-finish
}
}
}
bench:
go test benchmark_test.go --bench=. | tee result
PASS
Benchmark1024Concurrency 50 31726706 ns/op
Benchmark512Concurrency 50 29458105 ns/op
Benchmark256Concurrency 50 27518479 ns/op
Benchmark128Concurrency 50 25946690 ns/op
Benchmark64Concurrency 100 21016394 ns/op
Benchmark32Concurrency 100 15878670 ns/op
Benchmark16Concurrency 100 15632345 ns/op
Benchmark8Concurrency 100 15673540 ns/op
Benchmark4Concurrency 100 15439281 ns/op
ok command-line-arguments 14.628s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment