diff --git a/tools/pd-api-bench/Makefile b/tools/pd-api-bench/Makefile new file mode 100644 index 000000000000..7e517ef92b9f --- /dev/null +++ b/tools/pd-api-bench/Makefile @@ -0,0 +1,34 @@ +# Copyright 2023 TiKV Project Authors. +# +# 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. + +ROOT_PATH := ../.. +GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin +PATH := $(GO_TOOLS_BIN_PATH):$(PATH) +SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash) + +install-tools: + cd $(ROOT_PATH) && $(MAKE) install-tools + +static: install-tools + @ echo "gofmt ..." + @ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }' + @ echo "golangci-lint ..." + @ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners + @ echo "revive ..." + @ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./... + +tidy: + @ go mod tidy + git diff go.mod go.sum | cat + git diff --quiet go.mod go.sum diff --git a/tools/pd-api-bench/cases/cases.go b/tools/pd-api-bench/cases/cases.go index 2b770805cd8e..d431b6f325c4 100644 --- a/tools/pd-api-bench/cases/cases.go +++ b/tools/pd-api-bench/cases/cases.go @@ -29,8 +29,10 @@ import ( ) var ( + // PDAddress is the address of PD server. PDAddress string - Debug bool + // Debug is the flag to print the output of api response for debug. + Debug bool ) var ( @@ -39,6 +41,7 @@ var ( storesID []uint64 ) +// InitCluster initializes the cluster. func InitCluster(ctx context.Context, cli pd.Client, httpClit *http.Client) error { req, _ := http.NewRequestWithContext(ctx, http.MethodGet, PDAddress+"/pd/api/v1/stats/region?start_key=&end_key=&count", nil) @@ -67,6 +70,7 @@ func InitCluster(ctx context.Context, cli pd.Client, httpClit *http.Client) erro return nil } +// Case is the interface for all cases. type Case interface { Name() string SetQPS(int64) @@ -101,11 +105,13 @@ func (c *baseCase) GetBurst() int64 { return c.burst } +// GRPCCase is the interface for all gRPC cases. type GRPCCase interface { Case Unary(context.Context, pd.Client) error } +// GRPCCaseMap is the map for all gRPC cases. var GRPCCaseMap = map[string]GRPCCase{ "GetRegion": newGetRegion(), "GetStore": newGetStore(), @@ -113,12 +119,14 @@ var GRPCCaseMap = map[string]GRPCCase{ "ScanRegions": newScanRegions(), } +// HTTPCase is the interface for all HTTP cases. type HTTPCase interface { Case Do(context.Context, *http.Client) error Params(string) } +// HTTPCaseMap is the map for all HTTP cases. var HTTPCaseMap = map[string]HTTPCase{ "GetRegionStatus": newRegionStats(), "GetMinResolvedTS": newMinResolvedTS(), @@ -319,6 +327,7 @@ func (c *getStores) Unary(ctx context.Context, cli pd.Client) error { return nil } +// nolint func generateKeyForSimulator(id int, keyLen int) []byte { k := make([]byte, keyLen) copy(k, fmt.Sprintf("%010d", id)) diff --git a/tools/pd-api-bench/go.sum b/tools/pd-api-bench/go.sum index 4651e7bcb6b1..431f657e9f99 100644 --- a/tools/pd-api-bench/go.sum +++ b/tools/pd-api-bench/go.sum @@ -309,8 +309,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= -github.com/rleungx/kvproto v0.0.0-20230906083326-5bca56048097 h1:luEk8MEptO8Fop2EfHqY0bsIBtGZh8Qb0eVEz3tjYG8= -github.com/rleungx/kvproto v0.0.0-20230906083326-5bca56048097/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc= +github.com/rleungx/kvproto v0.0.0-20230907032444-eba37dcb1a38 h1:8vBwN7w7keTO+sBESkGVHM0bWAvOtc5eEEDaRR7ufbE= +github.com/rleungx/kvproto v0.0.0-20230907032444-eba37dcb1a38/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= diff --git a/tools/pd-api-bench/main.go b/tools/pd-api-bench/main.go index a891f7d23182..3b413d4dd768 100644 --- a/tools/pd-api-bench/main.go +++ b/tools/pd-api-bench/main.go @@ -160,7 +160,7 @@ func main() { } httpClis := make([]*http.Client, 0) for i := 0; i < *client; i++ { - httpClis = append(httpClis, newHttpClient()) + httpClis = append(httpClis, newHTTPClient()) } err = cases.InitCluster(ctx, pdClis[0], httpClis[0]) if err != nil { @@ -248,8 +248,8 @@ func exit(code int) { os.Exit(code) } -// newHttpClient returns an HTTP(s) client. -func newHttpClient() *http.Client { +// newHTTPClient returns an HTTP(s) client. +func newHTTPClient() *http.Client { // defaultTimeout for non-context requests. const defaultTimeout = 30 * time.Second cli := &http.Client{Timeout: defaultTimeout} diff --git a/tools/pd-tso-bench/Makefile b/tools/pd-tso-bench/Makefile new file mode 100644 index 000000000000..7e517ef92b9f --- /dev/null +++ b/tools/pd-tso-bench/Makefile @@ -0,0 +1,34 @@ +# Copyright 2023 TiKV Project Authors. +# +# 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. + +ROOT_PATH := ../.. +GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin +PATH := $(GO_TOOLS_BIN_PATH):$(PATH) +SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash) + +install-tools: + cd $(ROOT_PATH) && $(MAKE) install-tools + +static: install-tools + @ echo "gofmt ..." + @ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }' + @ echo "golangci-lint ..." + @ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners + @ echo "revive ..." + @ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./... + +tidy: + @ go mod tidy + git diff go.mod go.sum | cat + git diff --quiet go.mod go.sum diff --git a/tools/pd-tso-bench/go.sum b/tools/pd-tso-bench/go.sum index 6becdd7df162..397ab438ad59 100644 --- a/tools/pd-tso-bench/go.sum +++ b/tools/pd-tso-bench/go.sum @@ -108,8 +108,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= -github.com/rleungx/kvproto v0.0.0-20230906083326-5bca56048097 h1:luEk8MEptO8Fop2EfHqY0bsIBtGZh8Qb0eVEz3tjYG8= -github.com/rleungx/kvproto v0.0.0-20230906083326-5bca56048097/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc= +github.com/rleungx/kvproto v0.0.0-20230907032444-eba37dcb1a38 h1:8vBwN7w7keTO+sBESkGVHM0bWAvOtc5eEEDaRR7ufbE= +github.com/rleungx/kvproto v0.0.0-20230907032444-eba37dcb1a38/go.mod h1:r0q/CFcwvyeRhKtoqzmWMBebrtpIziQQ9vR+JKh1knc= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=