-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
storage-put.go
150 lines (124 loc) · 4.08 KB
/
storage-put.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2015 Nippon Telegraph and Telephone Corporation.
//
// 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 cmd
import (
"crypto/rand"
"fmt"
"os"
"runtime/pprof"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/github.com/spf13/cobra"
"github.com/coreos/etcd/lease"
)
// storagePutCmd represents a storage put performance benchmarking tool
var storagePutCmd = &cobra.Command{
Use: "put",
Short: "Benchmark put performance of storage",
Run: storagePutFunc,
}
var (
totalNrKeys int
storageKeySize int
valueSize int
txn bool
)
func init() {
storageCmd.AddCommand(storagePutCmd)
storagePutCmd.Flags().IntVar(&totalNrKeys, "total", 100, "a total number of keys to put")
storagePutCmd.Flags().IntVar(&storageKeySize, "key-size", 64, "a size of key (Byte)")
storagePutCmd.Flags().IntVar(&valueSize, "value-size", 64, "a size of value (Byte)")
storagePutCmd.Flags().BoolVar(&txn, "txn", false, "put a key in transaction or not")
// TODO: after the PR https://github.com/spf13/cobra/pull/220 is merged, the below pprof related flags should be moved to RootCmd
storagePutCmd.Flags().StringVar(&cpuProfPath, "cpuprofile", "", "the path of file for storing cpu profile result")
storagePutCmd.Flags().StringVar(&memProfPath, "memprofile", "", "the path of file for storing heap profile result")
}
func createBytesSlice(bytesN, sliceN int) [][]byte {
rs := make([][]byte, sliceN)
for i := range rs {
rs[i] = make([]byte, bytesN)
if _, err := rand.Read(rs[i]); err != nil {
panic(err)
}
}
return rs
}
func storagePutFunc(cmd *cobra.Command, args []string) {
if cpuProfPath != "" {
f, err := os.Create(cpuProfPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create a file for storing cpu profile result: ", err)
os.Exit(1)
}
err = pprof.StartCPUProfile(f)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to start cpu profile: ", err)
os.Exit(1)
}
defer pprof.StopCPUProfile()
}
if memProfPath != "" {
f, err := os.Create(memProfPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to create a file for storing heap profile result: ", err)
os.Exit(1)
}
defer func() {
err := pprof.WriteHeapProfile(f)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to write heap profile result: ", err)
// can do nothing for handling the error
}
}()
}
keys := createBytesSlice(storageKeySize, totalNrKeys)
vals := createBytesSlice(valueSize, totalNrKeys)
latencies := make([]time.Duration, totalNrKeys)
minLat := time.Duration(1<<63 - 1)
maxLat := time.Duration(0)
for i := 0; i < totalNrKeys; i++ {
begin := time.Now()
if txn {
id := s.TxnBegin()
if _, err := s.TxnPut(id, keys[i], vals[i], lease.NoLease); err != nil {
fmt.Fprintln(os.Stderr, "txn put error:", err)
os.Exit(1)
}
s.TxnEnd(id)
} else {
s.Put(keys[i], vals[i], lease.NoLease)
}
end := time.Now()
lat := end.Sub(begin)
latencies[i] = lat
if maxLat < lat {
maxLat = lat
}
if lat < minLat {
minLat = lat
}
}
total := time.Duration(0)
for _, lat := range latencies {
total += lat
}
fmt.Printf("total: %v\n", total)
fmt.Printf("average: %v\n", total/time.Duration(totalNrKeys))
fmt.Printf("rate: %4.4f\n", float64(totalNrKeys)/total.Seconds())
fmt.Printf("minimum latency: %v\n", minLat)
fmt.Printf("maximum latency: %v\n", maxLat)
// TODO: Currently this benchmark doesn't use the common histogram infrastructure.
// This is because an accuracy of the infrastructure isn't suitable for measuring
// performance of kv storage:
// https://github.com/coreos/etcd/pull/4070#issuecomment-167954149
}