Created
November 11, 2014 16:52
-
-
Save tylertreat/111b752eb1e3e5c2bb3f 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
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
const maxThread = 16 | |
var ( | |
channel chan interface{} | |
startTime []time.Time | |
endTime []time.Time | |
wg sync.WaitGroup | |
) | |
func server(id int) { | |
startTime[id] = time.Now() | |
for i := 0; i < 1000000; i++ { | |
<-channel | |
} | |
} | |
func client(id int) { | |
for i := 0; i < 1000000; i++ { | |
channel <- id | |
} | |
endTime[id] = time.Now() | |
wg.Done() | |
} | |
func measure(numThreads int) { | |
channel = make(chan interface{}, 10000) | |
startTime = make([]time.Time, numThreads) | |
endTime = make([]time.Time, numThreads) | |
for i := 0; i < numThreads; i++ { | |
wg.Add(1) | |
go server(i) | |
go client(i) | |
} | |
wg.Wait() | |
for i := 1; i < numThreads; i++ { | |
if startTime[0].After(startTime[i]) { | |
startTime[0] = startTime[i] | |
} | |
} | |
for i := 1; i < numThreads; i++ { | |
if endTime[0].Before(endTime[i]) { | |
endTime[0] = endTime[i] | |
} | |
} | |
ms := endTime[0].Sub(startTime[0]).Seconds() * 1000 | |
fmt.Printf("chan: %d*1000000 send/recv time in ms: %f (%f nr_of_msg/msec)\n", | |
numThreads, ms, float64(numThreads*1000000)/ms) | |
} | |
func main() { | |
for numThreads := 1; numThreads <= maxThread; numThreads *= 2 { | |
measure(numThreads) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment