LEFT | RIGHT |
(no file at all) | |
| 1 // Copyright 2011 The Go Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 |
| 5 // Tests goroutine creation/completion performance/scalability. |
| 6 // Spawns a lot of goroutines concurrently. goroutine-centr test uses single ato
mic |
| 7 // variable to track when all the goroutines are created and executed (this sing
le |
| 8 // var can hinder scalability). goroutine-distr test uses distributed variables |
| 9 // to track completion (it does not create scalability bottleneck, but puts addi
tional |
| 10 // pressure onto GC). |
| 11 // Expected to scale almost linearly. |
| 12 |
| 13 package main |
| 14 |
| 15 import ( |
| 16 "container/vector" |
| 17 "runtime" |
| 18 "sync/atomic" |
| 19 ) |
| 20 |
| 21 const ( |
| 22 LEVEL = 16 |
| 23 COUNT = uint64(1<<uint(LEVEL+1) - 1) |
| 24 ) |
| 25 |
| 26 type Compl struct { |
| 27 sync1 bool |
| 28 sync2 bool |
| 29 } |
| 30 |
| 31 func workerDistributed(level int, sync *bool) { |
| 32 if level > 0 { |
| 33 var compl Compl |
| 34 go workerDistributed(level-1, &compl.sync1) |
| 35 go workerDistributed(level-1, &compl.sync2) |
| 36 for compl.sync1 == false || compl.sync2 == false { |
| 37 runtime.Gosched() |
| 38 } |
| 39 } |
| 40 *sync = true |
| 41 } |
| 42 |
| 43 func benchmarkGorotuineDistributed() { |
| 44 for benchmarking { |
| 45 sync := false |
| 46 workerDistributed(LEVEL, &sync) |
| 47 totalWork += COUNT |
| 48 } |
| 49 } |
| 50 |
| 51 func workerCentralized(level int, sync *uint64) { |
| 52 if level > 0 { |
| 53 go workerCentralized(level-1, sync) |
| 54 go workerCentralized(level-1, sync) |
| 55 } |
| 56 atomic.AddUint64(sync, 1) |
| 57 } |
| 58 |
| 59 func benchmarkGorotuineCentralized() { |
| 60 for benchmarking { |
| 61 sync := uint64(0) |
| 62 workerCentralized(LEVEL, &sync) |
| 63 for sync != COUNT { |
| 64 runtime.Gosched() |
| 65 } |
| 66 totalWork += COUNT |
| 67 } |
| 68 } |
| 69 |
| 70 func SuiteGoroutine(benchmarks *vector.Vector) { |
| 71 benchmarks.Push(&Benchmark{"goroutine-distr", benchmarkGorotuineDistribu
ted}) |
| 72 benchmarks.Push(&Benchmark{"goroutine-centr", benchmarkGorotuineCentrali
zed}) |
| 73 } |
LEFT | RIGHT |