-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-bench-datasink.go
128 lines (117 loc) · 3.06 KB
/
go-bench-datasink.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
package main
import (
"bufio"
"flag"
"fmt"
redistimeseries "github.com/RedisTimeSeries/redistimeseries-go"
"github.com/gomodule/redigo/redis"
"golang.org/x/tools/benchmark/parse"
"log"
"os"
"runtime"
"strings"
"time"
)
var (
tagsFlag = flag.String(
"tag", "",
"comma-separated list of key=value pairs to add to each document",
)
redistimeseriesEndpoint = flag.String(
"redistimeseries-endpoint", "localhost:6379",
"RedisTimeSeries URL into which the benchmark data should be inserted",
)
redistimeseriesAuth = flag.String(
"redistimeseries-auth", "",
"RedisTimeSeries auth",
)
gitRef = flag.String(
"git-ref", "",
"git ref (branch is a good one)",
)
keySuffix = flag.String(
"key-suffix", "go-bench-datasink:",
"RedisTimeSeries key suffix",
)
verboseFlag = flag.Bool("v", false, "Be verbose")
)
const (
fieldName = "name"
fieldPkg = "pkg"
fieldGoVersion = "go_version"
fieldGOOS = "goos"
fieldGOARCH = "goarch"
fieldNSPerOp = "ns_per_op"
fieldGitRef = "git_ref"
fieldMeasurement = "measurement"
)
func main() {
flag.Parse()
if len(*gitRef) == 0 {
fmt.Errorf("git-ref is required.")
os.Exit(1)
}
pool := &redis.Pool{Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", *redistimeseriesEndpoint, redis.DialPassword(*redistimeseriesAuth))
}}
client := redistimeseries.NewClientFromPool(pool, "ts-client-1")
tags := make(map[string]string)
var pkg, goos, goarch string
timestamp := time.Now().UTC()
scanner := bufio.NewScanner(os.Stdin)
datapointTs := timestamp.UTC().Unix() * 1000.0
for scanner.Scan() {
line := scanner.Text()
switch {
case strings.HasPrefix(line, "pkg:"):
pkg = strings.TrimSpace(line[len("pkg:"):])
case strings.HasPrefix(line, "goos:"):
goos = strings.TrimSpace(line[len("goos:"):])
case strings.HasPrefix(line, "goarch:"):
goarch = strings.TrimSpace(line[len("goarch:"):])
default:
if b, err := parse.ParseLine(line); err == nil {
encodeIndexOp(
client, b,
pkg, goos, goarch,
tags, datapointTs,
*gitRef,
*keySuffix,
)
}
}
}
fmt.Println(pkg, goos, goarch)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func encodeIndexOp(
client *redistimeseries.Client,
b *parse.Benchmark,
pkg, goos, goarch string,
tags map[string]string,
timestamp int64,
gitRef string,
keySuffix string,
) {
keyName := fmt.Sprintf("%s%s:%s:%s:%s:%s:%s:%s", keySuffix, goos, goarch, runtime.Version(), pkg, b.Name, gitRef, fieldNSPerOp)
benchmarksSetName := fmt.Sprintf("%s%s:benchmarks", keySuffix, pkg)
labels := map[string]string{
fieldName: b.Name,
fieldPkg: pkg,
fieldGoVersion: runtime.Version(),
fieldGOOS: goos,
fieldGOARCH: goarch,
fieldGitRef: gitRef,
fieldMeasurement: fieldNSPerOp,
}
if b.Measured&parse.NsPerOp == 0 {
return
}
metric := b.NsPerOp
fmt.Println(timestamp, labels)
client.CreateKeyWithOptions(keyName, redistimeseries.CreateOptions{Labels: labels})
client.Add(keyName, timestamp, metric)
client.Pool.Get().Do("SADD", benchmarksSetName, b.Name)
}