AWS DynamoDB作为公有云的NoSQL 数据库,在很多业务环境和系统都被大量使用。 作为托管服务,传统DBA解脱了调优的压力,但是实际使用过程中,也需要遵守DynamoDB的游戏规则,才能更好的发挥性能。
而很多业务场景,在上线前,要评估生产环境的资源分配情况,如DynamoDB WCU/RCU等。那就需要对数据库做相应的压力测试,并根据测试情况,预留相应的资源。
这个时候,一个有效的,被广泛认可的压力测试软件,才是压力测试的核心。
这里我选择YCSB,全称为Yahoo! Cloud Serving Benchmark ,是Yahoo公司的一个用来对云服务进行基础测试的工具。
百度一下,YCSB的文章很多,但是针对DynamoDB的,竟然只有一篇,而且还是日文的。
无奈,自己研究Github文档吧。
下面开始流水账式一步步记录我的按照和测试过程:
1. YCSB下载和安装
wget https://github.com/brianfrankcooper/YCSB/releases/download/0.15.0/ycsb-0.15.0.tar.gz tar xfvz ycsb-0.15.0.tar.gz cd ycsb-0.15.0
2.测试是否可以运行
bin/ycsb.sh load basic -P workloads/workloada bin/ycsb.sh run basic -P workloads/workloada
3.查看关于dynamoDB部分的测试方法和具体配置
cd ycsb-0.15.0 cd dynamodb-binding/ cat README.md
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Benchmark $YCSB_HOME/bin/ycsb load dynamodb -P workloads/workloada -P dynamodb.properties $YCSB_HOME/bin/ycsb run dynamodb -P workloads/workloada -P dynamodb.properties # Properties $DYNAMODB_HOME/conf/dynamodb.properties $DYNAMODB_HOME/conf/AWSCredentials.properties ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4. 准备dynamoDB的连接信息,
查找终端节点链接:
https://docs.amazonaws.cn/aws/latest/userguide/endpoints-Ningxia.html
$ cat dynamodb/conf/dynamodb.properties dynamodb.awsCredentialsFile = dynamodb/conf/AWSCredentials.properties dynamodb.primaryKey = ppktest dynamodb.endpoint = http://dynamodb.cn-northwest-1.amazonaws.com.cn
5. 准备AWS AK/SK 信息
$ cat dynamodb/conf/AWSCredentials.properties accessKey = XXXXXXXXXXXXXXXXXXXX secretKey = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
6. 创建dynamoDB表:
很简单,但是也耗费我很长时间的一个小坑。dynamoDB的表名,YCSB只能是默认表名usertable,不可以自己取名字(应该是能改的,尝试去找配置文件,但是没有找到)。这也就能理解,为什么第4步的dynamodb.properties中,只需要提供的是主键名dynamodb.primaryKey ,而不是表名了。另外,只能设置主键,不能设置sort key,不然也会报错:
~~~~~~~~~~~~~~~~表名如果不是usertable,会遇到下面报错~~~~~~~~~~~~~ 0 [Thread-1] INFO com.yahoo.ycsb.db.DynamoDBClient -dynamodb connection created with http://dynamodb.cn-northwest-1.amazonaws.com.cn DBWrapper: report latency for each error is false and specific error codes to track for latency are: [] 185 [Thread-1] ERROR com.yahoo.ycsb.db.DynamoDBClient -com.amazonaws.AmazonServiceException: The provided key element does not match the schema (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 2J7JAI7ME2OCD2DR7VCQQFG6HJVV4KQNSO5AEMVJF66Q9ASUAAJG) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~设置sort key,会遇到下面错误~~~~~~~~~~~~~~~~~~~~~~ Starting test. 0 [Thread-1] INFO com.yahoo.ycsb.db.DynamoDBClient -dynamodb connection created with dynamodb.cn-northwest-1.amazonaws.com.cn DBWrapper: report latency for each error is false and specific error codes to track for latency are: [] 412 [Thread-1] ERROR com.yahoo.ycsb.db.DynamoDBClient -com.amazonaws.AmazonServiceException: One or more parameter values were invalid: Missing the key sortkeytest in the item (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: GH5CTDSTDPG99AL0Q1PF962DPJVV4KQNSO5AEMVJF66Q9ASUAAJG) Error inserting, not retrying any more. number of attempts: 1Insertion Retry Limit: 0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7. 准备压力测试参数文件,YCSB已经准备默认的workloada,workloadb,workloadc.....等常规业务模拟场景,可以直接使用,也可以根据需求更改具体参数
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # Read/update ratio: 95/5 # Default data size: 1 KB records (10 fields, 100 bytes each, plus key) # Request distribution: zipfian recordcount=10000000 operationcount=100000 workload=com.yahoo.ycsb.workloads.CoreWorkload readallfields=true readproportion=0.95 updateproportion=0.05 scanproportion=0 insertproportion=0 requestdistribution=zipfian ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8. 加载数据到DynamoDB
cd ycsb-0.15.0 ./bin/ycsb.sh load dynamodb -P workloads/ddbworkload -P dynamodb_home/conf/dynamodb.properties
9. 加载完数据后,表信息
10. 进行压力测试
./bin/ycsb.sh run dynamodb -P workloads/ddbworkload -P dynamodb_homeonf/dynamodb.properties -threads 100 -target 100
输出结果如下:
[OVERALL], RunTime(ms), 432143## 总共运行时间432秒 [OVERALL], Throughput(ops/sec), 231.40488218020423## 平均吞吐量 [TOTAL_GCS_PS_Scavenge], Count, 180 [TOTAL_GC_TIME_PS_Scavenge], Time(ms), 200 [TOTAL_GC_TIME_%_PS_Scavenge], Time(%), 0.046280976436040844 [TOTAL_GCS_PS_MarkSweep], Count, 0 [TOTAL_GC_TIME_PS_MarkSweep], Time(ms), 0 [TOTAL_GC_TIME_%_PS_MarkSweep], Time(%), 0.0 [TOTAL_GCs], Count, 180 [TOTAL_GC_TIME], Time(ms), 200 [TOTAL_GC_TIME_%], Time(%), 0.046280976436040844 [READ], Operations, 95111## 有95111个更新操作 [READ], AverageLatency(us), 4202.653026463816## 平均响应时间(毫秒) [READ], MinLatency(us), 2478## 最小响应时间(毫秒) [READ], MaxLatency(us), 635391 ## 最大响应时间(毫秒) [READ], 95thPercentileLatency(us), 4239 [READ], 99thPercentileLatency(us), 4863 [READ], Return=OK, 95111 ## 有95111个更新操作返回0(0=成功) [CLEANUP], Operations, 1 [CLEANUP], AverageLatency(us), 4.0 [CLEANUP], MinLatency(us), 4 [CLEANUP], MaxLatency(us), 4 [CLEANUP], 95thPercentileLatency(us), 4 [CLEANUP], 99thPercentileLatency(us), 4 [UPDATE], Operations, 4889 [UPDATE], AverageLatency(us), 6465.101656780527 [UPDATE], MinLatency(us), 4448 [UPDATE], MaxLatency(us), 1715199 [UPDATE], 95thPercentileLatency(us), 6099 [UPDATE], 99thPercentileLatency(us), 6739 [UPDATE], Return=OK, 4889
11. 在YCSB压测方式之外,如果只是想测试一下RCU/auto scaling,也可以通过scan的方式,在多个窗口同时执行,
(查询usertbale 行数)
aws dynamodb scan --table-name usertable --select "COUNT" { "Count": 5529465, "ScannedCount": 5529465, "ConsumedCapacity": null }
瞬间就达到我这个账号 DynamoDB的8000 limit了.
至此,本文结束,YCSB作为简单易用的NoSQL压测工具,整体感觉还不错,还有很多参数和细节,需要有时间深入研究一下。
参考文档:
Github YCSB https://github.com/brianfrankcooper/YCSB
YCSB dynamodb-autoscaling検証(日文) https://qiita.com/lawliteqed/items/d5d726e73803b891bb57