Skip to content

Commit

Permalink
cmd: Add raw API bencher
Browse files Browse the repository at this point in the history
  • Loading branch information
ngaut committed Nov 28, 2016
1 parent a2da8fe commit 1f3f860
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LDFLAGS += -X "github.com/pingcap/tidb/util/printer.TiDBGitHash=$(shell git rev-

TARGET = ""

.PHONY: all build update parser clean todo test gotest interpreter server dev benchkv check
.PHONY: all build update parser clean todo test gotest interpreter server dev benchkv benchraw check

default: server buildsucc

Expand Down Expand Up @@ -110,6 +110,9 @@ endif
benchkv:
$(GOBUILD) -ldflags '$(LDFLAGS)' -o bin/benchkv cmd/benchkv/main.go

benchraw:
$(GOBUILD) -ldflags '$(LDFLAGS)' -o bin/benchraw cmd/benchraw/main.go

benchdb:
$(GOBUILD) -ldflags '$(LDFLAGS)' -o bin/benchdb cmd/benchdb/main.go

Expand Down
126 changes: 126 additions & 0 deletions cmd/benchraw/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// 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"
"github.com/juju/errors"
"io/ioutil"
"net/http"
_ "net/http/pprof"
"sync"
"time"

"github.com/ngaut/log"
"github.com/pingcap/tidb/store/tikv"
"github.com/prometheus/client_golang/prometheus"
)

var (
dataCnt = flag.Int("N", 1000000, "data num")
workerCnt = flag.Int("C", 100, "concurrent num")
pdAddr = flag.String("pd", "localhost:2379", "pd address:localhost:2379")
valueSize = flag.Int("V", 5, "value size in byte")

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 informations.
func Init() {
prometheus.MustRegister(txnCounter)
prometheus.MustRegister(txnRolledbackCounter)
prometheus.MustRegister(txnDurations)
http.Handle("/metrics", prometheus.Handler())

go http.ListenAndServe(":9191", nil)
}

// without conflict
func batchRawPut(value []byte) {
cli, err := tikv.NewRawKVClient([]string{"localhost:2379"})
if err != nil {
log.Fatal(err)
}

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
key := fmt.Sprintf("key_%d", k)
err = cli.Put([]byte(key), value)
if err != nil {
log.Fatal(errors.ErrorStack(err))
}

txnDurations.WithLabelValues("txn").Observe(time.Since(start).Seconds())
}
}(i)
}
wg.Wait()
}

func main() {
flag.Parse()
log.SetLevelByString("warn")
Init()

value := make([]byte, *valueSize)
t := time.Now()
batchRawPut(value)
resp, err := http.Get("http://localhost:9191/metrics")
if err != nil {
log.Fatal(err)
}

defer resp.Body.Close()
text, err1 := ioutil.ReadAll(resp.Body)
if err1 != nil {
log.Fatal(err)
}

fmt.Println(string(text))

fmt.Printf("\nelapse:%v, total %v\n", time.Since(t), *dataCnt)
}

0 comments on commit 1f3f860

Please sign in to comment.