-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
54 lines (48 loc) · 1.17 KB
/
bench_test.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
package main
import (
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"github.com/stretchr/testify/require"
"io"
"net/http"
"strings"
"testing"
"time"
)
var (
postURL *string
getURL *string
)
const (
contentType = "application/json"
)
func BenchmarkPut(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
cli := http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
}}}
for pb.Next() {
nanos := time.Now().UnixNano()
data := fmt.Sprintf("{\"key\": \"%d\", \"value\": \"val_%d\"}", nanos, nanos)
resp, err := cli.Post(*postURL, contentType, strings.NewReader(data))
require.NoError(b, err, b.N)
require.Equal(b, http.StatusCreated, resp.StatusCode)
}
})
b.StopTimer()
resp, err := http.Get(*getURL + "/state")
require.NoError(b, err, b.N)
data, err := io.ReadAll(resp.Body)
require.NoError(b, err, b.N)
var count int64
err = json.Unmarshal(data, &count)
require.NoError(b, err, b.N)
b.ReportMetric(float64(count)/b.Elapsed().Seconds(), "creates/s")
}
func init() {
postURL = flag.String("post-url", "", "POST URL to set values")
getURL = flag.String("get-url", "", "GET URL to read values")
}