| package benchmarks |
| |
| import ( |
| "testing" |
| ) |
| |
| func BenchmarkChannelBlockingOneGoroutine(b *testing.B) { |
| benchmarkBlocking(b, 1) |
| } |
| func BenchmarkChannelBlockingTwoGoroutines(b *testing.B) { |
| benchmarkBlocking(b, 1) |
| } |
| func BenchmarkChannelBlockingThreeGoroutinesWithContendedWrite(b *testing.B) { |
| benchmarkBlocking(b, 2) |
| } |
| func benchmarkBlocking(b *testing.B, writers int64) { |
| channel := make(chan int64, 1024*16) |
| iterations := int64(b.N) |
| |
| b.ReportAllocs() |
| b.ResetTimer() |
| |
| for x := int64(0); x < writers; x++ { |
| go func() { |
| for i := int64(0); i < iterations; i++ { |
| channel <- i |
| } |
| }() |
| } |
| |
| for i := int64(0); i < iterations*writers; i++ { |
| msg := <-channel |
| if writers == 1 && msg != i { |
| panic("out of sequence") |
| } |
| } |
| } |
| |
| func BenchmarkChannelNonBlockingOneGoroutine(b *testing.B) { |
| benchmarkNonBlocking(b, 1) |
| } |
| func BenchmarkChannelNonBlockingTwoGoroutines(b *testing.B) { |
| benchmarkNonBlocking(b, 1) |
| } |
| func BenchmarkChannelNonBlockingThreeGoroutinesWithContendedWrite(b *testing.B) { |
| benchmarkNonBlocking(b, 2) |
| } |
| func benchmarkNonBlocking(b *testing.B, writers int64) { |
| iterations := int64(b.N) |
| maxReads := iterations * writers |
| channel := make(chan int64, 1024*16) |
| |
| b.ReportAllocs() |
| b.ResetTimer() |
| |
| for x := int64(0); x < writers; x++ { |
| go func() { |
| for i := int64(0); i < iterations; { |
| select { |
| case channel <- i: |
| i++ |
| default: |
| continue |
| } |
| } |
| }() |
| } |
| |
| for i := int64(0); i < maxReads; i++ { |
| select { |
| case msg := <-channel: |
| if writers == 1 && msg != i { |
| // panic("Out of sequence") |
| } |
| default: |
| continue |
| } |
| } |
| } |