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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
package main
import (
"bufio"
"fmt"
"io"
"log"
"math"
"os"
"path/filepath"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"sync"
"time"
"unsafe"
)
// go run main.go [measurements_file]
// tune env vars for performance
//
// Environment variables:
// - NUM_PARSERS: number of parsers to run concurrently. if unset, defaults
// to runtime.NumCPU()
// - PARSE_CHUNK_SIZE_MB: size of each chunk to parse. if unset, defaults to
// defaultParseChunkSize
// - PROFILE: if "true", enables profiling
var (
// others: "heap", "threadcreate", "block", "mutex"
profileTypes = []string{"goroutine", "allocs"}
)
const (
defaultMeasurementsPath = "measurements.txt"
maxNameLen = 100
maxNameNum = 10000
// tuned for a 2023 Macbook M2 Pro
defaultParseChunkSizeMB = 64
mb = 1024 * 1024 // bytes
)
type Stats struct {
Min, Max, Sum float64
Count int
}
// rounding floats to 1 decimal place with 0.05 rounding up to 0.1
func round(x float64) float64 {
return math.Floor((x+0.05)*10) / 10
}
// parseFloatFast is a high performance float parser using the assumption that
// the byte slice will always have a single decimal digit.
func parseFloatFast(bs []byte) float64 {
var intStartIdx int // is negative?
if bs[0] == '-' {
intStartIdx = 1
}
v := float64(bs[len(bs)-1]-'0') / 10 // single decimal digit
place := 1.0
for i := len(bs) - 3; i >= intStartIdx; i-- { // integer part
v += float64(bs[i]-'0') * place
place *= 10
}
if intStartIdx == 1 {
v *= -1
}
return v
}
// size is the intended number of bytes to parse. buffer should be longer than size
// because we need to continue reading until the end of the line in order to
// properly segment the entire file and not miss any data.
func parseAt(f *os.File, buf []byte, offset int64, size int) map[string]*Stats {
stats := make(map[string]*Stats, maxNameNum)
n, err := f.ReadAt(buf, offset) // load the buffer
if err != nil && err != io.EOF {
log.Fatal(err)
}
lastName := make([]byte, maxNameLen) // last name parsed
var lastNameLen int
isScanningName := true // currently scanning name or value?
// if offset is non-zero, skip to the first new line
var idx, start int
if offset != 0 {
for idx < n {
if buf[idx] == '\n' {
idx++
start = idx
break
}
idx++
}
}
// tick tock between parsing names and values while accummulating stats
for {
if isScanningName {
for idx < n {
if buf[idx] == ';' {
nameBs := buf[start:idx]
lastNameLen = copy(lastName, nameBs)
idx++
start = idx
isScanningName = false
break
}
idx++
}
} else {
for idx < n {
if buf[idx] == '\n' {
valueBs := buf[start:idx]
value := parseFloatFast(valueBs)
nameUnsafe := unsafe.String(&lastName[0], lastNameLen)
if s, ok := stats[nameUnsafe]; !ok {
name := string(lastName[:lastNameLen]) // actually allocate string
stats[name] = &Stats{Min: value, Max: value, Sum: value, Count: 1}
} else {
if value < s.Min {
s.Min = value
}
if value > s.Max {
s.Max = value
}
s.Sum += value
s.Count++
}
idx++
start = idx
isScanningName = true
break
}
idx++
}
}
// terminate when we hit the first newline after the intended size OR
// when we hit the end of the file
if (isScanningName && idx >= size) || idx >= n {
break
}
}
return stats
}
func printResults(stats map[string]*Stats) { // doesn't help
// sorted alphabetically for output
names := make([]string, 0, len(stats))
for name := range stats {
names = append(names, name)
}
sort.Strings(names)
var builder strings.Builder
for i, name := range names {
s := stats[name]
// gotcha: first round the sum to to remove float precision errors!
avg := round(round(s.Sum) / float64(s.Count))
builder.WriteString(fmt.Sprintf("%s=%.1f/%.1f/%.1f", name, s.Min, avg, s.Max))
if i < len(names)-1 {
builder.WriteString(", ")
}
}
writer := bufio.NewWriter(os.Stdout)
fmt.Fprintf(writer, "{%s}\n", builder.String())
writer.Flush()
}
// Read file in chunks and parse concurrently. N parsers work off of a chunk
// offset chan and send results on an output chan. The results are merged into a
// single map of stats and printed.
func main() {
// parse env vars and inputs
shouldProfile := os.Getenv("PROFILE") == "true"
var err error
var numParsers int
{
if os.Getenv("NUM_PARSERS") != "" {
numParsers, err = strconv.Atoi(os.Getenv("NUM_PARSERS"))
if err != nil {
log.Fatal(fmt.Errorf("failed to parse NUM_PARSERS: %w", err))
}
} else {
numParsers = runtime.NumCPU()
}
}
var parseChunkSize int
{
if os.Getenv("PARSE_CHUNK_SIZE_MB") != "" {
parseChunkSizeMB, err := strconv.Atoi(os.Getenv("PARSE_CHUNK_SIZE_MB"))
if err != nil {
log.Fatal(fmt.Errorf("failed to parse PARSE_CHUNK_SIZE_MB: %w", err))
}
parseChunkSize = parseChunkSizeMB * mb
} else {
parseChunkSize = defaultParseChunkSizeMB * mb
}
}
measurementsPath := defaultMeasurementsPath
if len(os.Args) > 1 {
measurementsPath = os.Args[1]
}
// profile code
if shouldProfile {
nowUnix := time.Now().Unix()
os.MkdirAll(fmt.Sprintf("profiles/%d", nowUnix), 0755)
for _, profileType := range profileTypes {
file, _ := os.Create(fmt.Sprintf("profiles/%d/%s.%s.pprof",
nowUnix, filepath.Base(measurementsPath), profileType))
defer file.Close()
defer pprof.Lookup(profileType).WriteTo(file, 0)
}
file, _ := os.Create(fmt.Sprintf("profiles/%d/%s.cpu.pprof",
nowUnix, filepath.Base(measurementsPath)))
defer file.Close()
pprof.StartCPUProfile(file)
defer pprof.StopCPUProfile()
}
// read file
f, err := os.Open(measurementsPath)
if err != nil {
log.Fatal(fmt.Errorf("failed to open %s file: %w", measurementsPath, err))
}
defer f.Close()
info, err := f.Stat()
if err != nil {
log.Fatal(fmt.Errorf("failed to read %s file: %w", measurementsPath, err))
}
// kick off "parser" workers
wg := sync.WaitGroup{}
wg.Add(numParsers)
// buffered to not block on merging
chunkOffsetCh := make(chan int64, numParsers)
chunkStatsCh := make(chan map[string]*Stats, numParsers)
go func() {
i := 0
for i < int(info.Size()) {
chunkOffsetCh <- int64(i)
i += parseChunkSize
}
close(chunkOffsetCh)
}()
for i := 0; i < numParsers; i++ {
// WARN: w/ extra padding for line overflow. Each chunk should be read past
// the intended size to the next new line. 128 bytes should be enough for
// a max 100 byte name + the float value.
buf := make([]byte, parseChunkSize+128)
go func() {
for chunkOffset := range chunkOffsetCh {
chunkStatsCh <- parseAt(f, buf, chunkOffset, parseChunkSize)
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(chunkStatsCh)
}()
mergedStats := make(map[string]*Stats, maxNameNum)
for chunkStats := range chunkStatsCh {
for name, s := range chunkStats {
if ms, ok := mergedStats[name]; !ok {
mergedStats[name] = s
} else {
if s.Min < ms.Min {
ms.Min = s.Min
}
if s.Max > ms.Max {
ms.Max = s.Max
}
ms.Sum += s.Sum
ms.Count += s.Count
}
}
}
printResults(mergedStats)
}
|