-
Notifications
You must be signed in to change notification settings - Fork 151
/
benchmark.go
196 lines (156 loc) · 5.88 KB
/
benchmark.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package toolset
import (
"context"
"fmt"
"math"
"os"
"runtime"
"sync"
"sync/atomic"
"time"
"github.com/dustin/go-humanize"
flag "github.com/spf13/pflag"
"github.com/iotaledger/hive.go/kvstore"
"github.com/iotaledger/hornet/pkg/database"
"github.com/iotaledger/hornet/pkg/utils"
)
func benchmarkIO(args []string) error {
fs := flag.NewFlagSet("", flag.ContinueOnError)
objectsCountFlag := fs.Int(FlagToolBenchmarkCount, 500000, "objects count")
objectsSizeFlag := fs.Int(FlagToolBenchmarkSize, 1000, "objects size in bytes")
databaseEngineFlag := fs.String(FlagToolDatabaseEngine, string(DefaultValueDatabaseEngine), "database engine (optional, values: pebble, rocksdb)")
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolBenchmarkIO)
fs.PrintDefaults()
println(fmt.Sprintf("\nexample: %s --%s %d --%s %d --%s %s",
ToolBenchmarkIO,
FlagToolBenchmarkCount,
500000,
FlagToolBenchmarkSize,
1000,
FlagToolDatabaseEngine,
DefaultValueDatabaseEngine))
}
if err := parseFlagSet(fs, args); err != nil {
return err
}
objectCnt := *objectsCountFlag
size := *objectsSizeFlag
dbEngine, err := database.DatabaseEngineFromStringAllowed(*databaseEngineFlag, database.EnginePebble, database.EngineRocksDB)
if err != nil {
return err
}
tempDir, err := os.MkdirTemp("", "benchmarkIO")
if err != nil {
return fmt.Errorf("can't create temp dir: %w", err)
}
defer func() { _ = os.RemoveAll(tempDir) }()
store, err := database.StoreWithDefaultSettings(tempDir, true, dbEngine)
if err != nil {
return fmt.Errorf("database initialization failed: %w", err)
}
batchWriter := kvstore.NewBatchedWriter(store)
writeDoneWaitGroup := &sync.WaitGroup{}
writeDoneWaitGroup.Add(objectCnt)
ts := time.Now()
lastStatusTime := time.Now()
for i := 0; i < objectCnt; i++ {
// one read operation and one write operation per cycle
batchWriter.Enqueue(newBenchmarkObject(store, writeDoneWaitGroup, randBytes(32), randBytes(size)))
if time.Since(lastStatusTime) >= printStatusInterval {
lastStatusTime = time.Now()
duration := time.Since(ts)
bytes := uint64(i * (32 + size))
totalBytes := uint64(objectCnt * (32 + size))
bytesPerSecond := uint64(float64(bytes) / duration.Seconds())
objectsPerSecond := uint64(float64(i) / duration.Seconds())
percentage, remaining := utils.EstimateRemainingTime(ts, int64(i), int64(objectCnt))
fmt.Printf("Average IO speed: %s/s (%dx 32+%d byte chunks with %s database, total %s/%s, %d objects/s, %0.2f%%. %v left...)\n", humanize.Bytes(bytesPerSecond), i, size, dbEngine, humanize.Bytes(bytes), humanize.Bytes(totalBytes), objectsPerSecond, percentage, remaining.Truncate(time.Second))
}
}
writeDoneWaitGroup.Wait()
if err := store.Flush(); err != nil {
return fmt.Errorf("flush database failed: %w", err)
}
if err := store.Close(); err != nil {
return fmt.Errorf("close database failed: %w", err)
}
te := time.Now()
duration := te.Sub(ts)
totalBytes := uint64(objectCnt * (32 + size))
bytesPerSecond := uint64(float64(totalBytes) / duration.Seconds())
objectsPerSecond := uint64(float64(objectCnt) / duration.Seconds())
fmt.Printf("Average IO speed: %s/s (%dx 32+%d byte chunks with %s database, total %s/%s, %d objects/s, took %v)\n", humanize.Bytes(bytesPerSecond), objectCnt, size, dbEngine, humanize.Bytes(totalBytes), humanize.Bytes(totalBytes), objectsPerSecond, duration.Truncate(time.Millisecond))
return nil
}
func benchmarkCPU(args []string) error {
fs := flag.NewFlagSet("", flag.ContinueOnError)
cpuThreadsFlag := fs.Int(FlagToolBenchmarkThreads, runtime.NumCPU(), "thread count")
durationFlag := fs.Duration(FlagToolBenchmarkDuration, 1*time.Minute, "duration")
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolBenchmarkCPU)
fs.PrintDefaults()
println(fmt.Sprintf("\nexample: %s --%s %d --%s 1m0s",
ToolBenchmarkCPU,
FlagToolBenchmarkThreads,
2,
FlagToolBenchmarkDuration))
}
if err := parseFlagSet(fs, args); err != nil {
return err
}
threads := *cpuThreadsFlag
duration := *durationFlag
benchmarkCtx, benchmarkCtxCancel := context.WithTimeout(context.Background(), duration)
defer benchmarkCtxCancel()
ts := time.Now()
// doBenchmarkCPU mines with CURL until the context has been canceled.
// it returns the number of calculated hashes.
doBenchmarkCPU := func(ctx context.Context, numWorkers int) uint64 {
var (
done uint32
counter uint64
wg sync.WaitGroup
closingCtx, closingCtxCancel = context.WithCancel(context.Background())
)
// random digest
powDigest := randBytes(32)
// stop when the context has been canceled
go func() {
select {
case <-ctx.Done():
atomic.StoreUint32(&done, 1)
case <-closingCtx.Done():
return
}
}()
go func() {
for atomic.LoadUint32(&done) == 0 {
time.Sleep(printStatusInterval)
elapsed := time.Since(ts)
percentage, remaining := utils.EstimateRemainingTime(ts, int64(elapsed.Milliseconds()), int64(duration.Milliseconds()))
megahashesPerSecond := float64(counter) / (elapsed.Seconds() * 1000000)
fmt.Printf("Average CPU speed: %0.2fMH/s (%d thread(s), %0.2f%%. %v left...)\n", megahashesPerSecond, threads, percentage, remaining.Truncate(time.Second))
}
}()
workerWidth := math.MaxUint64 / uint64(numWorkers)
for i := 0; i < numWorkers; i++ {
startNonce := uint64(i) * workerWidth
wg.Add(1)
go func() {
defer wg.Done()
if err := cpuBenchmarkWorker(powDigest, startNonce, &done, &counter); err != nil {
return
}
atomic.StoreUint32(&done, 1)
}()
}
wg.Wait()
closingCtxCancel()
return counter
}
hashes := doBenchmarkCPU(benchmarkCtx, threads)
megahashesPerSecond := float64(hashes) / (duration.Seconds() * 1000000)
fmt.Printf("Average CPU speed: %0.2fMH/s (%d thread(s), took %v)\n", megahashesPerSecond, threads, duration.Truncate(time.Millisecond))
return nil
}