|
| 1 | +package toolset |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "runtime" |
| 8 | + "sync" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/dustin/go-humanize" |
| 12 | + flag "github.com/spf13/pflag" |
| 13 | + |
| 14 | + "github.com/iotaledger/hive.go/app/configuration" |
| 15 | + "github.com/iotaledger/hive.go/db" |
| 16 | + "github.com/iotaledger/hive.go/kvstore" |
| 17 | + "github.com/iotaledger/iota-core/pkg/storage/database" |
| 18 | + iotago_tpkg "github.com/iotaledger/iota.go/v4/tpkg" |
| 19 | +) |
| 20 | + |
| 21 | +// estimateRemainingTime estimates the remaining time for a running operation and returns the finished percentage. |
| 22 | +func estimateRemainingTime(timeStart time.Time, current int64, total int64) (percentage float64, remaining time.Duration) { |
| 23 | + ratio := float64(current) / float64(total) |
| 24 | + totalTime := time.Duration(float64(time.Since(timeStart)) / ratio) |
| 25 | + remaining = time.Until(timeStart.Add(totalTime)) |
| 26 | + |
| 27 | + return ratio * 100.0, remaining |
| 28 | +} |
| 29 | + |
| 30 | +func benchmarkIO(args []string) error { |
| 31 | + fs := configuration.NewUnsortedFlagSet("", flag.ContinueOnError) |
| 32 | + objectsCountFlag := fs.Int(FlagToolBenchmarkCount, 500000, "objects count") |
| 33 | + objectsSizeFlag := fs.Int(FlagToolBenchmarkSize, 1000, "objects size in bytes") |
| 34 | + |
| 35 | + fs.Usage = func() { |
| 36 | + fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolBenchmarkIO) |
| 37 | + fs.PrintDefaults() |
| 38 | + println(fmt.Sprintf("\nexample: %s --%s %d --%s %d", |
| 39 | + ToolBenchmarkIO, |
| 40 | + FlagToolBenchmarkCount, |
| 41 | + 500000, |
| 42 | + FlagToolBenchmarkSize, |
| 43 | + 1000)) |
| 44 | + } |
| 45 | + |
| 46 | + if err := parseFlagSet(fs, args); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + objectCnt := *objectsCountFlag |
| 51 | + size := *objectsSizeFlag |
| 52 | + |
| 53 | + tempDir, err := os.MkdirTemp("", "benchmarkIO") |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("can't create temp dir: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + defer func() { _ = os.RemoveAll(tempDir) }() |
| 59 | + |
| 60 | + store, err := database.StoreWithDefaultSettings(tempDir, true, db.EngineRocksDB, db.EngineRocksDB) |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("database initialization failed: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + batchWriter := kvstore.NewBatchedWriter(store) |
| 66 | + writeDoneWaitGroup := &sync.WaitGroup{} |
| 67 | + writeDoneWaitGroup.Add(objectCnt) |
| 68 | + |
| 69 | + ts := time.Now() |
| 70 | + |
| 71 | + lastStatusTime := time.Now() |
| 72 | + for i := 0; i < objectCnt; i++ { |
| 73 | + // one read operation and one write operation per cycle |
| 74 | + batchWriter.Enqueue(newBenchmarkObject(store, writeDoneWaitGroup, iotago_tpkg.RandBytes(32), iotago_tpkg.RandBytes(size))) |
| 75 | + |
| 76 | + if time.Since(lastStatusTime) >= printStatusInterval { |
| 77 | + lastStatusTime = time.Now() |
| 78 | + |
| 79 | + duration := time.Since(ts) |
| 80 | + bytes := uint64(i * (32 + size)) |
| 81 | + totalBytes := uint64(objectCnt * (32 + size)) |
| 82 | + bytesPerSecond := uint64(float64(bytes) / duration.Seconds()) |
| 83 | + objectsPerSecond := uint64(float64(i) / duration.Seconds()) |
| 84 | + percentage, remaining := estimateRemainingTime(ts, int64(i), int64(objectCnt)) |
| 85 | + fmt.Printf("Average IO speed: %s/s (%dx 32+%d byte chunks, total %s/%s, %d objects/s, %0.2f%%. %v left ...)\n", humanize.Bytes(bytesPerSecond), i, size, humanize.Bytes(bytes), humanize.Bytes(totalBytes), objectsPerSecond, percentage, remaining.Truncate(time.Second)) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + writeDoneWaitGroup.Wait() |
| 90 | + |
| 91 | + if err := store.Flush(); err != nil { |
| 92 | + return fmt.Errorf("flush database failed: %w", err) |
| 93 | + } |
| 94 | + |
| 95 | + if err := store.Close(); err != nil { |
| 96 | + return fmt.Errorf("close database failed: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + te := time.Now() |
| 100 | + duration := te.Sub(ts) |
| 101 | + totalBytes := uint64(objectCnt * (32 + size)) |
| 102 | + bytesPerSecond := uint64(float64(totalBytes) / duration.Seconds()) |
| 103 | + objectsPerSecond := uint64(float64(objectCnt) / duration.Seconds()) |
| 104 | + |
| 105 | + fmt.Printf("Average IO speed: %s/s (%dx 32+%d byte chunks, total %s/%s, %d objects/s, took %v)\n", humanize.Bytes(bytesPerSecond), objectCnt, size, humanize.Bytes(totalBytes), humanize.Bytes(totalBytes), objectsPerSecond, duration.Truncate(time.Millisecond)) |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func benchmarkCPU(args []string) error { |
| 111 | + fs := configuration.NewUnsortedFlagSet("", flag.ContinueOnError) |
| 112 | + cpuThreadsFlag := fs.Int(FlagToolBenchmarkThreads, runtime.NumCPU(), "thread count") |
| 113 | + durationFlag := fs.Duration(FlagToolBenchmarkDuration, 1*time.Minute, "duration") |
| 114 | + |
| 115 | + fs.Usage = func() { |
| 116 | + fmt.Fprintf(os.Stderr, "Usage of %s:\n", ToolBenchmarkCPU) |
| 117 | + fs.PrintDefaults() |
| 118 | + println(fmt.Sprintf("\nexample: %s --%s %d --%s 1m0s", |
| 119 | + ToolBenchmarkCPU, |
| 120 | + FlagToolBenchmarkThreads, |
| 121 | + 2, |
| 122 | + FlagToolBenchmarkDuration)) |
| 123 | + } |
| 124 | + |
| 125 | + if err := parseFlagSet(fs, args); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + |
| 129 | + threads := *cpuThreadsFlag |
| 130 | + duration := *durationFlag |
| 131 | + |
| 132 | + benchmarkCtx, benchmarkCtxCancel := context.WithTimeout(context.Background(), duration) |
| 133 | + defer benchmarkCtxCancel() |
| 134 | + |
| 135 | + ts := time.Now() |
| 136 | + |
| 137 | + // doBenchmarkCPU mines with blake2b until the context has been canceled. |
| 138 | + // it returns the number of calculated hashes. |
| 139 | + doBenchmarkCPU := func(ctx context.Context, numWorkers int) uint64 { |
| 140 | + var counter uint64 |
| 141 | + var wg sync.WaitGroup |
| 142 | + |
| 143 | + // random digest |
| 144 | + powDigest := iotago_tpkg.RandBytes(32) |
| 145 | + |
| 146 | + go func() { |
| 147 | + for ctx.Err() == nil { |
| 148 | + time.Sleep(printStatusInterval) |
| 149 | + |
| 150 | + elapsed := time.Since(ts) |
| 151 | + percentage, remaining := estimateRemainingTime(ts, elapsed.Milliseconds(), duration.Milliseconds()) |
| 152 | + megahashesPerSecond := float64(counter) / (elapsed.Seconds() * 1000000) |
| 153 | + fmt.Printf("Average CPU speed: %0.2fMH/s (%d thread(s), %0.2f%%. %v left ...)\n", megahashesPerSecond, numWorkers, percentage, remaining.Truncate(time.Second)) |
| 154 | + } |
| 155 | + }() |
| 156 | + |
| 157 | + for i := 0; i < numWorkers; i++ { |
| 158 | + wg.Add(1) |
| 159 | + go func() { |
| 160 | + defer wg.Done() |
| 161 | + |
| 162 | + cpuBenchmarkWorker(ctx, powDigest, &counter) |
| 163 | + }() |
| 164 | + } |
| 165 | + wg.Wait() |
| 166 | + |
| 167 | + return counter |
| 168 | + } |
| 169 | + |
| 170 | + hashes := doBenchmarkCPU(benchmarkCtx, threads) |
| 171 | + megahashesPerSecond := float64(hashes) / (duration.Seconds() * 1000000) |
| 172 | + fmt.Printf("Average CPU speed: %0.2fMH/s (%d thread(s), took %v)\n", megahashesPerSecond, threads, duration.Truncate(time.Millisecond)) |
| 173 | + |
| 174 | + return nil |
| 175 | +} |
0 commit comments