mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-25 07:38:57 +00:00
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
package cake_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"gopl.io/ch8/cake"
|
|
)
|
|
|
|
var defaults = cake.Shop{
|
|
Verbose: testing.Verbose(),
|
|
Cakes: 20,
|
|
BakeTime: 10 * time.Millisecond,
|
|
NumIcers: 1,
|
|
IceTime: 10 * time.Millisecond,
|
|
InscribeTime: 10 * time.Millisecond,
|
|
}
|
|
|
|
func Benchmark(b *testing.B) {
|
|
// Baseline: one baker, one icer, one inscriber.
|
|
// Each step takes exactly 10ms. No buffers.
|
|
cakeshop := defaults
|
|
cakeshop.Work(b.N) // 224 ms
|
|
}
|
|
|
|
func BenchmarkBuffers(b *testing.B) {
|
|
// Adding buffers has no effect.
|
|
cakeshop := defaults
|
|
cakeshop.BakeBuf = 10
|
|
cakeshop.IceBuf = 10
|
|
cakeshop.Work(b.N) // 224 ms
|
|
}
|
|
|
|
func BenchmarkVariable(b *testing.B) {
|
|
// Adding variability to rate of each step
|
|
// increases total time due to channel delays.
|
|
cakeshop := defaults
|
|
cakeshop.BakeStdDev = cakeshop.BakeTime / 4
|
|
cakeshop.IceStdDev = cakeshop.IceTime / 4
|
|
cakeshop.InscribeStdDev = cakeshop.InscribeTime / 4
|
|
cakeshop.Work(b.N) // 259 ms
|
|
}
|
|
|
|
func BenchmarkVariableBuffers(b *testing.B) {
|
|
// Adding channel buffers reduces
|
|
// delays resulting from variability.
|
|
cakeshop := defaults
|
|
cakeshop.BakeStdDev = cakeshop.BakeTime / 4
|
|
cakeshop.IceStdDev = cakeshop.IceTime / 4
|
|
cakeshop.InscribeStdDev = cakeshop.InscribeTime / 4
|
|
cakeshop.BakeBuf = 10
|
|
cakeshop.IceBuf = 10
|
|
cakeshop.Work(b.N) // 244 ms
|
|
}
|
|
|
|
func BenchmarkSlowIcing(b *testing.B) {
|
|
// Making the middle stage slower
|
|
// adds directly to the critical path.
|
|
cakeshop := defaults
|
|
cakeshop.IceTime = 50 * time.Millisecond
|
|
cakeshop.Work(b.N) // 1.032 s
|
|
}
|
|
|
|
func BenchmarkSlowIcingManyIcers(b *testing.B) {
|
|
// Adding more icing cooks reduces the cost of icing
|
|
// to its sequential component, following Amdahl's Law.
|
|
cakeshop := defaults
|
|
cakeshop.IceTime = 50 * time.Millisecond
|
|
cakeshop.NumIcers = 5
|
|
cakeshop.Work(b.N) // 288ms
|
|
}
|