Debian Codegraph @

server/raft_benchmark_test.go

raw
1 // Copyright 2026 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package server
15
16 import (
17 "encoding/binary"
18 "fmt"
19 "os"
20 "path/filepath"
21 "sync"
22 "sync/atomic"
23 "testing"
24 "time"
25 )
26
27 func benchmarkWriteTermVote(b *testing.B, workers int) {
28 root := b.TempDir()
29 dirs := make([]string, workers)
30 for i := range dirs {
31 dir := filepath.Join(root, fmt.Sprintf("raft-%d", i))
32 if err := os.MkdirAll(dir, defaultDirPerms); err != nil {
33 b.Fatal(err)
34 }
35 dirs[i] = dir
36 }
37
38 wtvs := make([][]byte, workers)
39 for i := range wtvs {
40 wtv := make([]byte, termVoteLen)
41 copy(wtv[termLen:], fmt.Sprintf("%08d", i))
42 wtvs[i] = wtv
43 }
44
45 dios := defaultDiskIOSemaphore()
46 var next atomic.Uint64
47 var wg sync.WaitGroup
48 type latencyStats struct {
49 count uint64
50 total time.Duration
51 min time.Duration
52 max time.Duration
53 }
54 stats := make([]latencyStats, workers)
55 wg.Add(workers)
56
57 b.ResetTimer()
58 for i := range workers {
59 go func(worker int) {
60 defer wg.Done()
61 dir := dirs[worker]
62 wtv := wtvs[worker]
63 stat := &stats[worker]
64 for {
65 n := next.Add(1)
66 if n > uint64(b.N) {
67 return
68 }
69 binary.LittleEndian.PutUint64(wtv[:termLen], n)
70 start := time.Now()
71 if err := writeTermVote(dios, dir, wtv); err != nil {
72 b.Errorf("writeTermVote failed: %v", err)
73 return
74 }
75 elapsed := time.Since(start)
76 stat.count++
77 stat.total += elapsed
78 if stat.min == 0 || elapsed < stat.min {
79 stat.min = elapsed
80 }
81 if elapsed > stat.max {
82 stat.max = elapsed
83 }
84 }
85 }(i)
86 }
87 wg.Wait()
88 b.StopTimer()
89
90 var count uint64
91 var total, minLatency, maxLatency time.Duration
92 for i := range stats {
93 stat := stats[i]
94 if stat.count == 0 {
95 continue
96 }
97 count += stat.count
98 total += stat.total
99 if minLatency == 0 || stat.min < minLatency {
100 minLatency = stat.min
101 }
102 if stat.max > maxLatency {
103 maxLatency = stat.max
104 }
105 }
106 if count > 0 {
107 avgLatency := time.Duration(total.Nanoseconds() / int64(count))
108 b.ReportMetric(float64(minLatency.Microseconds()), "min-latency-us")
109 b.ReportMetric(float64(avgLatency.Microseconds()), "avg-latency-us")
110 b.ReportMetric(float64(maxLatency.Microseconds()), "max-latency-us")
111 b.ReportMetric(0, "ns/op")
112 }
113 if elapsed := b.Elapsed(); elapsed > 0 {
114 b.ReportMetric(float64(b.N)/elapsed.Seconds(), "writes/s")
115 }
116 }
117
118 func BenchmarkWriteTermVote(b *testing.B) {
119 for _, workers := range []int{1, 16, 128, 512} {
120 b.Run(fmt.Sprintf("workers=%d", workers), func(b *testing.B) {
121 benchmarkWriteTermVote(b, workers)
122 })
123 }
124 }