Repository navigation
Expand file tree
/
Copy pathdflybench.go
More file actions
128 lines (118 loc) · 3.91 KB
/
Copy pathdflybench.go
File metadata and controls
128 lines (118 loc) · 3.91 KB
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
package replication
import (
"bufio"
"context"
"fmt"
"io"
"os"
"regexp"
"strconv"
"time"
"github.com/dragonflydb/benchmarking/remote"
)
// rpsPattern and p99Pattern match dfly_bench's periodic stdout lines, taken
// verbatim from playground/replication-bench/benchmark.py. Only the
// instantaneous ("now") RPS value is captured, not the aggregate.
var (
rpsPattern = regexp.MustCompile(`RPS\(now/agg\):\s*(\d+)/\d+`)
p99Pattern = regexp.MustCompile(`p99_lat\(us\):\s*(\d+)`)
)
// BuildDflyBenchArgs renders the dfly_bench command string for one case.
// Mode is either "preload" (populate the keyspace) or "load" (sustained
// traffic for cfg.TestTime). Flag choices mirror
// playground/replication-bench/benchmark.py so results are comparable with
// the Python prototype. PreloadKeys is the keyspace range (--key_maximum),
// not the number of keys written.
func BuildDflyBenchArgs(masterHost string, masterPort int, mode string, cfg *ReplicationConfig, c Case) string {
switch mode {
case "preload":
// PRELOAD_BASE from benchmark.py: 4 threads * 15 conns writing
// random keys over [0, PreloadKeys) for a fixed 30s.
return fmt.Sprintf(
"dfly_bench --h=%s --p=%d -d %d --ratio=1:0 --proactor_threads=4 -c=15 --pipeline=10 --key_maximum=%d --test_time=30 --qps=0",
masterHost, masterPort, cfg.ValueSize, cfg.PreloadKeys,
)
case "load":
// LOAD_BASE from benchmark.py. --qps is a per-connection cap and
// there are 2 connections, so qps=100000 schedules 200k total ops/s.
ratio := cfg.Ratio
if ratio == "" {
ratio = DefaultRatio
}
// --qps is always emitted: 0 means unbounded (maxload), whereas
// omitting the flag would use dfly_bench's built-in default of 20/sec.
return fmt.Sprintf(
"dfly_bench --h=%s --p=%d -d %d --ratio=%s --proactor_threads=1 -c=2 --pipeline=5 --test_time=%d --key_maximum=%d --qps=%d",
masterHost, masterPort, cfg.ValueSize, ratio, int(cfg.TestTime.Seconds()), cfg.PreloadKeys, c.QPS,
)
}
return ""
}
// RunDflyBenchStream runs dfly_bench on the client instance and emits one
// RepSample per (RPS, p99) pair parsed from its stdout. The samples channel
// is closed when dfly_bench exits; the error channel receives at most one
// value.
func RunDflyBenchStream(ctx context.Context, ssh *remote.SSHClient, cmd string) (<-chan RepSample, <-chan error) {
samples := make(chan RepSample, 128)
errs := make(chan error, 1)
pr, pw := io.Pipe()
started := time.Now()
go func() {
defer close(samples)
scanner := bufio.NewScanner(pr)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
var curRPS, curP99 int
var haveRPS, haveP99 bool
for scanner.Scan() {
line := scanner.Text()
if m := rpsPattern.FindStringSubmatch(line); m != nil {
if n, err := strconv.Atoi(m[1]); err == nil {
curRPS = n
haveRPS = true
}
}
if m := p99Pattern.FindStringSubmatch(line); m != nil {
if n, err := strconv.Atoi(m[1]); err == nil {
curP99 = n
haveP99 = true
}
}
if haveRPS && haveP99 {
samples <- RepSample{
TSec: int(time.Since(started).Seconds()),
RPS: curRPS,
P99MicroSec: curP99,
}
haveRPS, haveP99 = false, false
}
}
}()
go func() {
// Tee stdout to both the parser and the operator's terminal.
stderr, err := ssh.ExecuteStream(ctx, cmd, io.MultiWriter(pw, os.Stderr))
pw.Close()
if err != nil {
errs <- fmt.Errorf("dfly_bench: %w\n%s", err, stderr)
return
}
errs <- nil
}()
return samples, errs
}
// ParseDflyBenchLine extracts the RPS and p99 values, if present, from one
// line of dfly_bench stdout.
func ParseDflyBenchLine(line string) (rps int, p99us int, matchedRPS, matchedP99 bool) {
if m := rpsPattern.FindStringSubmatch(line); m != nil {
if n, err := strconv.Atoi(m[1]); err == nil {
rps = n
matchedRPS = true
}
}
if m := p99Pattern.FindStringSubmatch(line); m != nil {
if n, err := strconv.Atoi(m[1]); err == nil {
p99us = n
matchedP99 = true
}
}
return
}