-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
cake_test.go
74 lines (64 loc) · 1.9 KB
/
cake_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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
}