OLD | NEW |
(Empty) | |
| 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 sync.Once fast-path scalability under contention. |
| 6 // A set of goroutines repeatedly execute sync.Once.Do() on a single object. |
| 7 // Expected to scale almost linearly. |
| 8 |
| 9 package main |
| 10 |
| 11 import ( |
| 12 "container/vector" |
| 13 "runtime" |
| 14 "sync" |
| 15 ) |
| 16 |
| 17 func benchmarkOnce() { |
| 18 procs := runtime.GOMAXPROCS(-1) |
| 19 compl := make(chan uint64, procs) |
| 20 once := &sync.Once{} |
| 21 for p := 0; p != procs; p += 1 { |
| 22 go func() { |
| 23 f := func() {} |
| 24 work := uint64(0) |
| 25 for benchmarking { |
| 26 work += 1 |
| 27 if (work % 1000) == 0 { |
| 28 runtime.Gosched() |
| 29 } |
| 30 once.Do(f) |
| 31 } |
| 32 compl <- work |
| 33 }() |
| 34 } |
| 35 for p := 0; p != procs; p += 1 { |
| 36 totalWork += <-compl |
| 37 } |
| 38 } |
| 39 |
| 40 func SuiteOnce(benchmarks *vector.Vector) { |
| 41 benchmarks.Push(&Benchmark{"once", benchmarkOnce}) |
| 42 } |
OLD | NEW |