summaryrefslogtreecommitdiffhomepage
path: root/test/benchmarks/tools/hey.go
blob: b8cb938fedd82671da2cf3485b06ad933eea4285 (plain)
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
// Copyright 2020 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tools

import (
	"fmt"
	"net"
	"regexp"
	"strconv"
	"strings"
	"testing"
)

// Hey is for the client application 'hey'.
type Hey struct {
	Requests    int // Note: requests cannot be less than concurrency.
	Concurrency int
	Doc         string
}

// MakeCmd returns a 'hey' command.
func (h *Hey) MakeCmd(ip net.IP, port int) []string {
	return strings.Split(fmt.Sprintf("hey -n %d -c %d http://%s:%d/%s",
		h.Requests, h.Concurrency, ip, port, h.Doc), " ")
}

// Report parses output from 'hey' and reports metrics.
func (h *Hey) Report(b *testing.B, output string) {
	b.Helper()
	requests, err := h.parseRequestsPerSecond(output)
	if err != nil {
		b.Fatalf("failed to parse requests per second: %v", err)
	}
	ReportCustomMetric(b, requests, "requests_per_second" /*metric name*/, "QPS" /*unit*/)

	ave, err := h.parseAverageLatency(output)
	if err != nil {
		b.Fatalf("failed to parse average latency: %v", err)
	}
	ReportCustomMetric(b, ave, "average_latency" /*metric name*/, "s" /*unit*/)
}

var heyReqPerSecondRE = regexp.MustCompile(`Requests/sec:\s*(\d+\.?\d+?)\s+`)

// parseRequestsPerSecond finds requests per second from 'hey' output.
func (h *Hey) parseRequestsPerSecond(data string) (float64, error) {
	match := heyReqPerSecondRE.FindStringSubmatch(data)
	if len(match) < 2 {
		return 0, fmt.Errorf("failed get bandwidth: %s", data)
	}
	return strconv.ParseFloat(match[1], 64)
}

var heyAverageLatencyRE = regexp.MustCompile(`Average:\s*(\d+\.?\d+?)\s+secs`)

// parseHeyAverageLatency finds Average Latency in seconds form 'hey' output.
func (h *Hey) parseAverageLatency(data string) (float64, error) {
	match := heyAverageLatencyRE.FindStringSubmatch(data)
	if len(match) < 2 {
		return 0, fmt.Errorf("failed get average latency match%d : %s", len(match), data)
	}
	return strconv.ParseFloat(match[1], 64)
}