forked from tidwall/lotsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlotsaa.go
174 lines (161 loc) · 3.67 KB
/
lotsaa.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package lotsa
import (
"fmt"
"io"
"math/rand"
"runtime"
"sync"
"sync/atomic"
"time"
)
// Output is used to print elapsed time and ops/sec
var Output io.Writer
// MemUsage is used to output the memory usage
var MemUsage bool
// Ops executed a number of operations over a multiple goroutines.
// count is the number of operations.
// threads is the number goroutines.
// op is the operation function
func Ops(count, threads int, op func(i, thread int)) {
var start time.Time
var wg sync.WaitGroup
wg.Add(threads)
var ms1 runtime.MemStats
output := Output
if output != nil {
if MemUsage {
runtime.GC()
runtime.ReadMemStats(&ms1)
}
start = time.Now()
}
for i := 0; i < threads; i++ {
s, e := count/threads*i, count/threads*(i+1)
if i == threads-1 {
e = count
}
go func(i, s, e int) {
defer wg.Done()
for j := s; j < e; j++ {
op(j, i)
}
}(i, s, e)
}
wg.Wait()
if output != nil {
dur := time.Since(start)
var alloc uint64
if MemUsage {
runtime.GC()
var ms2 runtime.MemStats
runtime.ReadMemStats(&ms2)
if ms1.HeapAlloc > ms2.HeapAlloc {
alloc = 0
} else {
alloc = ms2.HeapAlloc - ms1.HeapAlloc
}
}
WriteOutput(output, int64(count), threads, dur, alloc)
}
}
// Time executed operations over multiple goroutines for a fixed duration.
// duration is the duration for continuously running the Operation.
// threads is the number goroutines.
// op is the operation function
func Time(duration time.Duration, threads int, op func(threadRand *rand.Rand, thread int)) {
var start time.Time
var wg sync.WaitGroup
wg.Add(threads)
var ms1 runtime.MemStats
output := Output
if output != nil {
if MemUsage {
runtime.GC()
runtime.ReadMemStats(&ms1)
}
start = time.Now()
}
var count int64
for i := 0; i < threads; i++ {
go func(i int) {
threadRand := rand.New(rand.NewSource(time.Now().UnixNano()))
done := time.After(duration)
var localCount int64 = 0
for {
select {
case <-done:
wg.Done()
atomic.AddInt64(&count, localCount)
return
default:
{
op(threadRand, i)
localCount++
}
}
}
}(i)
}
wg.Wait()
if output != nil {
dur := time.Since(start)
var alloc uint64
if MemUsage {
runtime.GC()
var ms2 runtime.MemStats
runtime.ReadMemStats(&ms2)
if ms1.HeapAlloc > ms2.HeapAlloc {
alloc = 0
} else {
alloc = ms2.HeapAlloc - ms1.HeapAlloc
}
}
WriteOutput(output, count, threads, dur, alloc)
}
}
func commaize(n int64) string {
s1, s2 := fmt.Sprintf("%d", n), ""
for i, j := len(s1)-1, 0; i >= 0; i, j = i-1, j+1 {
if j%3 == 0 && j != 0 {
s2 = "," + s2
}
s2 = string(s1[i]) + s2
}
return s2
}
func memstr(alloc uint64) string {
switch {
case alloc <= 1024:
return fmt.Sprintf("%d bytes", alloc)
case alloc <= 1024*1024:
return fmt.Sprintf("%.1f KB", float64(alloc)/1024)
case alloc <= 1024*1024*1024:
return fmt.Sprintf("%.1f MB", float64(alloc)/1024/1024)
default:
return fmt.Sprintf("%.1f GB", float64(alloc)/1024/1024/1024)
}
}
// WriteOutput writes an output line to the specified writer
func WriteOutput(w io.Writer, count int64, threads int, elapsed time.Duration, alloc uint64) {
var ss string
if threads != 1 {
ss = fmt.Sprintf("over %d threads ", threads)
}
var nsop int
if count > 0 {
nsop = int(elapsed / time.Duration(count))
}
var allocstr string
if alloc > 0 {
var bops float64
if count > 0 {
bops = float64(alloc) / float64(count)
}
allocstr = fmt.Sprintf(", %s, %.1f bytes/op", memstr(alloc), bops)
}
fmt.Fprintf(w, "%s ops %sin %.0fms, %s/sec, %d ns/op%s\n",
commaize(count), ss, elapsed.Seconds()*1000,
commaize(int64(float64(count)/elapsed.Seconds())),
nsop, allocstr,
)
}