Created
July 2, 2020 12:33
-
-
Save nicewook/f1af9a5ef3d180952bb418f959c2e477 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 ( | |
"sync" | |
"testing" | |
) | |
func BenchmarkContextSwitch(b *testing.B) { | |
var wg sync.WaitGroup | |
begin := make(chan struct{}) | |
c := make(chan struct{}) | |
var token struct{} | |
sender := func() { | |
defer wg.Done() | |
<-begin // block | |
for i := 0; i < b.N; i++ { | |
c <- token // send via channel | |
} | |
} | |
receiver := func() { | |
defer wg.Done() | |
<-begin // block | |
for i := 0; i < b.N; i++ { | |
<-c // send via channel | |
} | |
} | |
wg.Add(2) | |
go receiver() | |
go sender() | |
b.StartTimer() // manually start benchmark timer here | |
close(begin) // trigger | |
wg.Wait() | |
} | |
/* | |
go test context_test.go -bench . | |
goos: windows | |
goarch: amd64 | |
BenchmarkContextSwitch-12 5999942 204 ns/op | |
PASS | |
ok command-line-arguments 1.591s | |
오히려 여러개의 CPU 를 돌아다니며 context switching 하는것 보다 이게 더 빠르네 하하 | |
go test context_test.go -bench . -cpu=1 | |
goos: windows | |
goarch: amd64 | |
BenchmarkContextSwitch 9370021 126 ns/op | |
PASS | |
ok command-line-arguments 1.464s | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment