-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
112 lines (98 loc) · 2.45 KB
/
main_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"bytes"
"context"
"io"
"io/ioutil"
"math/rand"
"strconv"
"testing"
"time"
)
var srand = rand.New(rand.NewSource(time.Now().UnixNano()))
func randString(wordLen int) string {
b := make([]byte, wordLen)
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for i := range b {
b[i] = charset[srand.Intn(len(charset))]
}
return string(b)
}
// This creates a stub of a work queue by returning a newline-delimited string
// reader of all of the files in /etc
func testWorkQueue(wordLen, queueLen int, delim rune) io.Reader {
buf := &bytes.Buffer{}
for i := 0; i < queueLen; i++ {
if i > 0 {
buf.WriteRune(delim)
}
buf.WriteString(randString(wordLen))
}
return buf
}
// This is my generalized benchmark function. It accepts a `*testing.B`, the
// concurrency I want to attempt to work at, the command to run for each item
// in the queue, as well as the args that would be supplied.
//
// This version uses a regular command rather than a template.
func benchmarkCmd(b *testing.B, concurrency, wordLen, queueLen int, cmd string, args ...string) {
for n := 0; n < b.N; n++ {
b.StopTimer()
pool, err := NewWorkerPool(
context.Background(),
ioutil.Discard,
ioutil.Discard,
testWorkQueue(wordLen, queueLen, '\n'),
byte('\n'),
cmd,
"",
[]string{},
concurrency,
)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
pool.run()
}
}
// This is my generalized benchmark function. It accepts a `*testing.B`, the
// concurrency I want to attempt to work at, the template to run for each item
// in the queue, as well as the args that would be supplied.
//
// This version uses a template rather than a command
func benchmarkTmpl(b *testing.B, concurrency, wordLen, queueLen int, tmpl string, args ...string) {
for n := 0; n < b.N; n++ {
b.StopTimer()
pool, err := NewWorkerPool(
context.Background(),
ioutil.Discard,
ioutil.Discard,
testWorkQueue(wordLen, queueLen, '\n'),
byte('\n'),
"",
tmpl,
[]string{},
concurrency,
)
if err != nil {
b.Fatal(err)
}
b.StartTimer()
pool.run()
}
}
func BenchmarkEchoEtcCmd(b *testing.B) {
for _, n := range []int{1, 2, 3, 4, 5, 6, 7, 8} {
b.Run(strconv.Itoa(n), func(b *testing.B) {
benchmarkCmd(b, n, 64, 100, "echo")
})
}
}
func BenchmarkEtcTmpl(b *testing.B) {
for _, n := range []int{1, 2, 3, 4, 5, 6, 7, 8} {
b.Run(strconv.Itoa(n), func(b *testing.B) {
benchmarkTmpl(b, n, 64, 100, "echo")
})
}
}