/
main.go
62 lines (49 loc) · 1014 Bytes
/
main.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
package main
import (
"bytes"
"math/rand"
"os"
"runtime/trace"
"strconv"
"sync"
"time"
)
const (
maxConcurrent = 5
totalWork = 1000000
)
func main() {
// create file to hold the trace data
file, err := os.Create("trace.out")
if err != nil {
panic(err)
}
defer file.Close()
// start/stop the tracer
err = trace.Start(file)
if err != nil {
panic(err)
}
defer trace.Stop()
rand.Seed(time.Now().UnixNano())
wg := &sync.WaitGroup{}
for x := 0; x < maxConcurrent; x++ {
wg.Add(1)
go worker(wg, totalWork/maxConcurrent)
}
wg.Wait()
}
func worker(wg *sync.WaitGroup, total int) {
defer wg.Done()
buffer := &bytes.Buffer{}
for x := 0; x < total; x++ {
first := rand.Int63()
second := rand.Int63()
_, _ = buffer.WriteString(strconv.FormatInt(first, 10))
_, _ = buffer.WriteString("+")
_, _ = buffer.WriteString(strconv.FormatInt(second, 10))
_, _ = buffer.WriteString("=")
_, _ = buffer.WriteString(strconv.FormatInt(first+second, 10))
buffer.Reset()
}
}