Last active
December 15, 2015 08:19
-
-
Save nevir/5229850 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" | |
"runtime" | |
"time" | |
) | |
var NUM_THINGS = 100000 | |
var NUM_RUNS = 5 | |
var PARALLEL_WORKERS = 10 | |
var WORKER_DURATION = 100 * time.Microsecond | |
// Specific Type Signatures | |
// ======================== | |
func ConsumeWithGoroutineOnDemand(input []int, numInParallel int, worker func(int)) { | |
start := time.Now() | |
var startStats runtime.MemStats | |
runtime.ReadMemStats(&startStats) | |
sem := make(chan bool, numInParallel) | |
done := make(chan bool) | |
max := len(input) | |
count := 0 | |
init := time.Now() | |
for i := 0; i < numInParallel; i++ { | |
sem <- true | |
} | |
for _, item := range input { | |
<-sem | |
// A goroutine per item of work; up to `numInParallel`. | |
go func(item int) { | |
worker(item) | |
count++ | |
if count == max { | |
done <- true | |
} else { | |
sem <- true | |
} | |
}(item) | |
} | |
<-done | |
timings("ConsumeWithGoroutineOnDemand", &startStats, start, init) | |
} | |
func ConsumeWithPrespunWorkers(input []int, numInParallel int, worker func(int)) { | |
start := time.Now() | |
var startStats runtime.MemStats | |
runtime.ReadMemStats(&startStats) | |
workItems := make(chan int) | |
done := make(chan bool) | |
max := len(input) | |
count := 0 | |
init := time.Now() | |
for i := 0; i < numInParallel; i++ { | |
go func() { | |
for item := range workItems { | |
worker(item) | |
count++ | |
if count == max { | |
done <- true | |
} | |
} | |
}() | |
} | |
for _, item := range input { | |
workItems <- item | |
} | |
<-done | |
timings("ConsumeWithPrespunWorkers", &startStats, start, init) | |
} | |
// Benchmark Stuff | |
// =============== | |
func timings(name string, startStats *runtime.MemStats, start time.Time, init time.Time) { | |
finish := time.Now() | |
var finishStats runtime.MemStats | |
runtime.ReadMemStats(&finishStats) | |
pauseTotal := finishStats.PauseTotalNs - startStats.PauseTotalNs | |
allocs := finishStats.Alloc - startStats.Alloc | |
totalAllocs := finishStats.TotalAlloc - startStats.TotalAlloc | |
heapObjects := finishStats.HeapObjects - startStats.HeapObjects | |
fmt.Printf("%30v() init: %-5v work: %-9v | PauseTotalNs: %-3v Alloc: %-5v TotalAlloc: %-5v HeapObjects: %-3v\n", name, init.Sub(start), finish.Sub(init), pauseTotal, allocs, totalAllocs, heapObjects) | |
} | |
func spinnyWorker(thing int) { | |
stopAt := time.Now().Add(WORKER_DURATION).UnixNano() | |
for time.Now().UnixNano() < stopAt { | |
// spin spin spin! | |
} | |
} | |
func sleepyWorker(thing int) { | |
time.Sleep(WORKER_DURATION) | |
} | |
func main() { | |
things := make([]int, NUM_THINGS) | |
for i := 0; i < NUM_THINGS; i++ { | |
things[i] = i + 10 | |
} | |
for i := 1; i <= NUM_RUNS; i++ { | |
fmt.Println("=======================================") | |
fmt.Printf("Spin run %v:\n", i) | |
ConsumeWithPrespunWorkers(things, PARALLEL_WORKERS, spinnyWorker) | |
ConsumeWithGoroutineOnDemand(things, PARALLEL_WORKERS, spinnyWorker) | |
fmt.Printf("Sleep run %v:\n", i) | |
ConsumeWithPrespunWorkers(things, PARALLEL_WORKERS, sleepyWorker) | |
ConsumeWithGoroutineOnDemand(things, PARALLEL_WORKERS, sleepyWorker) | |
fmt.Println("") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interestingly, for the sleepy worker case, the ConsumeWithPrespunWorkers is consistently slower (even though it does a good deal fewer allocations):