-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
main.go
140 lines (122 loc) · 3.69 KB
/
main.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
// Copyright 2016 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"sync"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/juju/errors"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/terror"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
)
var (
store kv.Storage
dataCnt = flag.Int("N", 1000000, "data num")
workerCnt = flag.Int("C", 400, "concurrent num")
pdAddr = flag.String("pd", "localhost:2379", "pd address:localhost:2379")
valueSize = flag.Int("V", 5, "value size in byte")
sslCA = flag.String("cacert", "", "path of file that contains list of trusted SSL CAs.")
sslCert = flag.String("cert", "", "path of file that contains X509 certificate in PEM format.")
sslKey = flag.String("key", "", "path of file that contains X509 key in PEM format.")
txnCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tikv",
Subsystem: "txn",
Name: "total",
Help: "Counter of txns.",
}, []string{"type"})
txnRolledbackCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tikv",
Subsystem: "txn",
Name: "failed_total",
Help: "Counter of rolled back txns.",
}, []string{"type"})
txnDurations = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "tikv",
Subsystem: "txn",
Name: "durations_histogram_seconds",
Help: "Txn latency distributions.",
Buckets: prometheus.ExponentialBuckets(0.0005, 2, 13),
}, []string{"type"})
)
// Init initializes information.
func Init() {
driver := tikv.Driver{}
var err error
store, err = driver.Open(fmt.Sprintf("tikv://%s?cluster=1", *pdAddr))
terror.MustNil(err)
prometheus.MustRegister(txnCounter)
prometheus.MustRegister(txnRolledbackCounter)
prometheus.MustRegister(txnDurations)
http.Handle("/metrics", prometheus.Handler())
go func() {
err1 := http.ListenAndServe(":9191", nil)
terror.Log(errors.Trace(err1))
}()
}
// batchRW makes sure conflict free.
func batchRW(value []byte) {
wg := sync.WaitGroup{}
base := *dataCnt / *workerCnt
wg.Add(*workerCnt)
for i := 0; i < *workerCnt; i++ {
go func(i int) {
defer wg.Done()
for j := 0; j < base; j++ {
txnCounter.WithLabelValues("txn").Inc()
start := time.Now()
k := base*i + j
txn, err := store.Begin()
if err != nil {
log.Fatal(err)
}
key := fmt.Sprintf("key_%d", k)
err = txn.Set([]byte(key), value)
terror.Log(errors.Trace(err))
err = txn.Commit(context.Background())
if err != nil {
txnRolledbackCounter.WithLabelValues("txn").Inc()
terror.Call(txn.Rollback)
}
txnDurations.WithLabelValues("txn").Observe(time.Since(start).Seconds())
}
}(i)
}
wg.Wait()
}
func main() {
flag.Parse()
log.SetLevel(log.ErrorLevel)
Init()
value := make([]byte, *valueSize)
t := time.Now()
batchRW(value)
resp, err := http.Get("http://localhost:9191/metrics")
terror.MustNil(err)
defer terror.Call(resp.Body.Close)
text, err1 := ioutil.ReadAll(resp.Body)
terror.Log(errors.Trace(err1))
fmt.Println(string(text))
fmt.Printf("\nelapse:%v, total %v\n", time.Since(t), *dataCnt)
}