Skip to content

Commit

Permalink
add readme
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <jinhao.hu@pingcap.com>
  • Loading branch information
HuSharp committed Aug 4, 2023
1 parent a0a1986 commit 5c28b36
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
39 changes: 39 additions & 0 deletions tools/pd-api-bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pd-api-bench
========

pd-api-bench is a tool to test PD API.

## Build
1. [Go](https://golang.org/) Version 1.19 or later
2. In the root directory of the [PD project](https://github.com/tikv/pd), use the `make` command to compile and generate `bin/pd-api-bench`


## Usage

This section describes how to use the `pd-api-bench` tool.

### Flags description

```
-cacert string
path of file that contains list of trusted SSL CAs
-cert string
path of file that contains X509 certificate in PEM format
-concurrency int
the client number (default 1)
-pd string
pd address (default "127.0.0.1:2379")
-qps
the qps of request (default 1000)
```

### TLS

You can use the following command to generate a certificate for testing TLS:

```shell
cd cert
./generate_cert.sh
go run ../main.go --min-resolved-ts-http -cacert ca.pem -cert pd-server.pem -key pd-server-key.pem

```
2 changes: 1 addition & 1 deletion tools/pd-api-bench/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tools/pd-api-bench

go 1.19
go 1.20

require (
github.com/tikv/pd v1.1.0-beta.0.20230803041908-c8c5e1e215a1
Expand Down
43 changes: 23 additions & 20 deletions tools/pd-api-bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ import (
var (
pdAddr = flag.String("pd", "127.0.0.1:2379", "pd address")
// min-resolved-ts
minResolvedTSByGRPC = flag.Int("min-resolved-ts-grpc", 0, "min-resolved-ts by grpc qps")
minResolvedTSByHTTP = flag.Int("min-resolved-ts-http", 0, "min-resolved-ts by http qps")
minResolvedTSByGRPC = flag.Bool("min-resolved-ts-grpc", false, "min-resolved-ts by grpc qps")
minResolvedTSByHTTP = flag.Bool("min-resolved-ts-http", false, "min-resolved-ts by http qps")
// concurrency
concurrency = flag.Int("concurrency", 1, "client number")
client = flag.Int("client", 1, "client number")
// qps
qps = flag.Int("qps", 1000, "qps")
// tls
caPath = flag.String("cacert", "", "path of file that contains list of trusted SSL CAs")
certPath = flag.String("cert", "", "path of file that contains X509 certificate in PEM format")
keyPath = flag.String("key", "", "path of file that contains X509 key in PEM format")
)

var base = int(time.Second) / int(time.Millisecond)

func main() {
flag.Parse()
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -63,8 +67,12 @@ func main() {
cancel()
}()

go handleMinResolvedTSByGRPC(ctx)
go handleMinResolvedTSByHTTP(ctx)
if *minResolvedTSByGRPC {
go handleMinResolvedTSByGRPC(ctx)
}
if *minResolvedTSByHTTP {
go handleMinResolvedTSByHTTP(ctx)
}

<-ctx.Done()
switch sig {
Expand All @@ -76,14 +84,12 @@ func main() {
}

func handleMinResolvedTSByGRPC(ctx context.Context) {
if *minResolvedTSByGRPC == 0 {
log.Println("handleMinResolvedTSByGRPC qps = 0, exit")
return
}
pdCli := newPDClient()
for i := 0; i < *concurrency; i++ {
log.Println("handleMinResolvedTSByGRPC start...")
tt := base / *qps
for i := 0; i < *client; i++ {
pdCli := newPDClient()
go func() {
var ticker = time.NewTicker(time.Millisecond * 200)
var ticker = time.NewTicker(time.Millisecond * time.Duration(tt))
defer ticker.Stop()
for {
select {
Expand All @@ -103,22 +109,20 @@ func handleMinResolvedTSByGRPC(ctx context.Context) {
}

func handleMinResolvedTSByHTTP(ctx context.Context) {
if *minResolvedTSByHTTP == 0 {
log.Println("handleMinResolvedTSByHTTP qps = 0, exit")
return
}
log.Println("handleMinResolvedTSByHTTP start...")
// Judge whether with tls.
protocol := "http"
if len(*caPath) != 0 {
protocol = "https"
}
url := fmt.Sprintf("%s://%s/pd/api/v1/min-resolved-ts", protocol, *pdAddr)

httpsCli := newHttpClient()
for i := 0; i < *concurrency; i++ {
tt := base / *qps
for i := 0; i < *client; i++ {
httpsCli := newHttpClient()
go func() {
// Mock client-go's request frequency.
var ticker = time.NewTicker(time.Millisecond * 2000)
var ticker = time.NewTicker(time.Millisecond * time.Duration(tt))
defer ticker.Stop()
for {
select {
Expand All @@ -133,7 +137,6 @@ func handleMinResolvedTSByHTTP(ctx context.Context) {
listResp := &minResolvedTS{}
apiutil.ReadJSON(res.Body, listResp)
res.Body.Close()
httpsCli.CloseIdleConnections()
case <-ctx.Done():
log.Println("Got signal to exit handleMinResolvedTSByHTTP")
return
Expand Down

0 comments on commit 5c28b36

Please sign in to comment.