From a5f5d040f6441eb58287b057d856fb739d5c2b4e Mon Sep 17 00:00:00 2001 From: yedf2 <120050102@qq.com> Date: Mon, 24 Jan 2022 15:19:36 +0800 Subject: [PATCH] add flash-sales bench --- .gitignore | 2 +- bench/svr/http.go | 18 ++++++++++++++++++ bench/test-flash-sales.sh | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100755 bench/test-flash-sales.sh diff --git a/.gitignore b/.gitignore index 34e44f0..07212df 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ dist .vscode default.etcd */**/*.bolt - +bench/bench # Output file of unit test coverage coverage.* profile.* diff --git a/bench/svr/http.go b/bench/svr/http.go index 6e8d7bc..1f827d8 100644 --- a/bench/svr/http.go +++ b/bench/svr/http.go @@ -7,6 +7,7 @@ package svr import ( + "context" "database/sql" "fmt" "os" @@ -132,6 +133,8 @@ func qsAdjustBalance(uid int, amount int, c *gin.Context) error { // nolint: unp return nil } +var stockKey = "{a}--stock-1" + func benchAddRoute(app *gin.Engine) { app.POST(benchAPI+"/TransIn", dtmutil.WrapHandler2(func(c *gin.Context) interface{} { return qsAdjustBalance(dtmimp.MustAtoi(c.Query("uid")), 1, c) @@ -186,4 +189,19 @@ func benchAddRoute(app *gin.Engine) { saga.WaitResult = true return saga.Submit() })) + app.Any(benchAPI+"/benchFlashSalesReset", dtmutil.WrapHandler2(func(c *gin.Context) interface{} { + _, err := busi.RedisGet().FlushAll(context.Background()).Result() + logger.FatalIfError(err) + _, err = busi.RedisGet().Set(context.Background(), stockKey, "0", 86400*time.Second).Result() + logger.FatalIfError(err) + return nil + })) + app.Any(benchAPI+"/benchFlashSales", dtmutil.WrapHandler2(func(c *gin.Context) interface{} { + gid := "{a}-" + shortuuid.New() + msg := dtmcli.NewMsg(dtmutil.DefaultHTTPServer, gid). + Add("", nil) + return msg.DoAndSubmit("", func(bb *dtmcli.BranchBarrier) error { + return bb.RedisCheckAdjustAmount(busi.RedisGet(), stockKey, -1, 86400) + }) + })) } diff --git a/bench/test-flash-sales.sh b/bench/test-flash-sales.sh new file mode 100755 index 0000000..d47e174 --- /dev/null +++ b/bench/test-flash-sales.sh @@ -0,0 +1,14 @@ +# !/bin/bash + +set -x + +export LOG_LEVEL=fatal +export STORE_DRIVER=redis +export STORE_HOST=localhost +export STORE_PORT=6379 +go build +./bench redis & +echo 'sleeping 3s for dtm bench to run up.' && sleep 3 +curl "http://127.0.0.1:8083/api/busi_bench/benchFlashSalesReset" +ab -n 1000 -c 20 "http://127.0.0.1:8083/api/busi_bench/benchFlashSales" +pkill bench