使用dbt5进行TPC-E测试

使用工具:dbt5、hgdb4.5.7

dbt5下载路径:https://github.com/osdldbt/dbt5

hgdb4.5.7自行安装

编译安装R语言

dbt5生成测试报告时需要用到Python及R语言,一般操作系统默认安装Python2.7版本,可以直接使用该版本,生成测试报告时,需要使用Python调用R语言的功能,不要使用最新的R语言版本。此处选择的是与Python2.7及dbt5差多时间的R语言版本

R语言下载路径:https://mirrors.tuna.tsinghua.edu.cn/CRAN/src/base/R-3/R-3.0.1.tar.gz

1
2
3
4
5
6
7
8
#R语言编译安装过程比较比较简单,没啥可说的
tar -zxf R-3.0.1.tar.gz
cd R-3.0.1
./configure --prefix=/usr/local/R3 --with-gnu-ld --with-cairo --with-x --enable-R-shlib
make && make install
#R语言的
ln -s /usr/local/R3/lib64/R/etc/ /usr/local/R3/etc
ln -s /usr/local/R3/lib64/R/lib /usr/local/R3/lib

编译安装Python插件rpy2

Python缺少时间序列分析的包,做分析很不方便,R语言有很多分析用的包,可以在使用Python时,将R语言作为一个附属部分使用。简单说就是使用rpy2可以直接用Python调用R的功能。

因为用的Python2.7,rpy2也需要选择早期版本,这里使用的是与R3.0.1同一时期的版本2.4.0

下载路径:https://github.com/rpy2/rpy2/releases/tag/RELEASE_2_4_0

编译安装过程

1
2
3
4
5
#RELEASE_2_4_0.tar.gz下载下来就叫这名字
tar -zxf RELEASE_2_4_0.tar.gz
cd rpy2-RELEASE_2_4_0
#此处不用参考网上教程,注释extern void Rf_PrintWarnings(void)等内容,那是rpy2 2.3及之前版本的问题,2.4版本已经修复,直接安装即可。
python setup.py build --r-home /usr/local/R3/lib64/R/ install

编译安装dbt5

编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#加载数据库环境变量
source ~/.bashrc
#dbt5使用的cmake,需要事先安装
yum install cmake
#下载dbt5源码
git clone https://github.com/osdldbt/dbt5 dbt5
#切换到dbt5目录
cd dbt5
#编译
cmake CMakeLists.txt -DDBMS=pgsql
#安装dbt5,DESTDIR后指定安装路径:/opt/dbt-5,此处可能会报错,报错解决方法见下面注意部分
make install DESTDIR=/opt/dbt-5
#编译EGen
cd dbt5/egen/prj
make -f Makefile.pgsql
#编译安装存储过程到数据库安装目录
cd dbt5/storedproc/pgsql/c
make install

报错处理

注意:如果make install时报如下错误

1
2
3
4
5
6
7
8
9
10
11
[root@k8s4 dbt5]# make install
[ 1%] Building CXX object CMakeFiles/bin/BrokerageHouseMain.dir/src/interfaces/TxnHarnessSendToMarket.o
In file included from src/include/TxnHarnessSendToMarket.h:17:0,
from /opt/dbt5/src/interfaces/TxnHarnessSendToMarket.cpp:11:
src/include/CSocket.h: In member function ‘void CSocket::closeListenerSocket()’:
src/include/CSocket.h:42:47: error: ‘close’ was not declared in this scope
void closeListenerSocket() { close(m_listenfd); }
^
make[2]: *** [CMakeFiles/bin/BrokerageHouseMain.dir/src/interfaces/TxnHarnessSendToMarket.o] Error 1
make[1]: *** [CMakeFiles/bin/BrokerageHouseMain.dir/all] Error 2
make: *** [all] Error 2

处理方式:修改文件dbt5/src/include/CSocket.h,添加“#include <unistd.h>”

1
2
3
4
5
6
7
8
9
10
11
vi src/include/CSocket.h
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include "CThreadErr.h"
#include "MiscConsts.h"
/*添加如下内容*/
#include <unistd.h>

功能说明

编译完成后,可执行文件在dbt5下的bin目录中,列表即部分文件功能如下,主要用到是dbt5-pgsql-build-db、dbt5-run-workload

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@k8s4 bin]# pwd
/opt/dbt-5/bin
[root@k8s4 bin]# ll
total 10684
-rwxr-xr-x 1 root root 1490512 Jun 14 16:13 BrokerageHouseMain #模拟券商
-rwxr-xr-x 1 root root 9923 Jun 9 14:44 dbt5-generate-report
-rwxr-xr-x 1 root root 416 Jun 9 14:44 dbt5-get-os-info
-rwxr-xr-x 1 root root 612 Jun 9 14:44 dbt5-pgsql-backup-db
-rwxr-xr-x 1 root root 3490 Jun 9 14:44 dbt5-pgsql-build-db #创建测试数据库,写入测试数据
-rwxr-xr-x 1 root root 2717 Jun 9 14:44 dbt5-pgsql-check-db
-rwxr-xr-x 1 root root 1374 Jun 9 14:44 dbt5-pgsql-create-db
-rwxr-xr-x 1 root root 30291 Jun 9 14:44 dbt5-pgsql-create-indexes
-rwxr-xr-x 1 root root 22206 Jun 9 14:44 dbt5-pgsql-create-tables
-rwxr-xr-x 1 root root 22590 Jun 9 14:44 dbt5-pgsql-db-plans
-rwxr-xr-x 1 root root 3824 Jun 9 14:44 dbt5-pgsql-db-stat
-rwxr-xr-x 1 root root 521 Jun 9 14:44 dbt5-pgsql-drop-db
-rwxr-xr-x 1 root root 2753 Jun 9 14:44 dbt5-pgsql-drop-tables
-rwxr-xr-x 1 root root 1736 Jun 9 14:44 dbt5-pgsql-load-stored-procs
-rwxr-xr-x 1 root root 618 Jun 9 14:44 dbt5-pgsql-restore-db
-rwxr-xr-x 1 root root 1061 Jun 9 14:44 dbt5-pgsql-start-db
-rwxr-xr-x 1 root root 470 Jun 9 14:44 dbt5-pgsql-stop-db
-rwxr-xr-x 1 root root 4024 Jun 9 14:44 dbt5-plot-transaction-rate
-rwxr-xr-x 1 root root 5094 Jun 9 14:44 dbt5-post-process #生成测试报告
-rwxr-xr-x 1 root root 19029 Jun 9 14:44 dbt5-run-workload #运行测试
-rwxr-xr-x 1 root root 2509 Jun 9 14:44 dbt5-sysstats
-rwxr-xr-x 1 root root 3115456 Jun 14 16:14 DriverMain #模拟客户端
-rwxr-xr-x 1 root root 1949824 Jun 14 16:15 MarketExchangeMain #模拟交易所
-rwxr-xr-x 1 root root 4196000 Jun 14 16:17 TestTxn

开始测试

设置环境变量

以下环境变量根据实际情况进行修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#数据库环境变量内容如下,如果加载过就不用加载了,要注意写上PGUSER
export HG_BASE=/opt/pg14
export HGDB_HOME=/opt/pg14
export PGPORT=5432
export PGDATABASE=postgres
export PGDATA=$HGDB_HOME/data
export PATH=$HGDB_HOME/bin:$PATH
export PGUSER=postgres
#dbt5环境变量
export EGENDIR=/opt/dbt5/egen
export DBT5DBNAME=dbt5
export DBT5PGDATA=/data/hgdb457
export DBT5TSDIR=/opt/dbt5
export DBT5_HOME=/opt/dbt5
export PATH=:$DBT5_HOME/bin:$PATH
#R语言环境变量
export R_HOME=/usr/local/R3/lib64/R
export PATH=:$R_HOME/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/R3/lib64/R/lib:$LD_LIBRARY_PATH
#把密码写到环境变量中,写到pgpass中,测试过程中要求输入密码
export PGPASSWORD=Qwert@1234

创建测试数据库

使用dbt5-pgsql-build-db命令创建测试数据库及生成测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#dbt5-pgsql-build-db帮助信息
[root@k8s4 bin]# dbt5-pgsql-build-db -h
Usage:
./dbt5-pgsql-build-db [option]
./dbt5-pgsql-build-db -h
Options:
-b <integer>
Beginning customer ordinal position
设置客户起始编号,默认从1开始
-c <integer>
Number of customers for this instance
生成的测试数据中有多少个客户账号,默认5000
-d <dbname>
PGDATABASE name
数据库名称
-h
this help
-i <path>
Path to EGen
EGen的路径,不指定使用环境变量中的设置
-l <port>
database port
数据库端口
-p <database_parameters>
PostgreSQL database parameters
指定数据库参数
-r
Drop existing database before building a new database
创建新库时,如果已经存在,则删掉已存在的数据库
-s <scale fact (customers per 1 trtps)
比例因子,每个客户操作通过是1trtps,具体怎么调节还不明白,默认值500
-t <integer>
Number of customers (total)
客户端数量
-u
Use tablespaces
表示使用表空间,好像没啥用
-w <days>
Initial trade days (business days) to populate
初始化多少天的交易数据,默认300天


#生成测试数据,环境变量设置好后,会自动初始化、创建数据库、写入测试数据,通过设置交易数据的天数,控制数据量,100天数据量约5G
[root@k8s4 bin]# dbt5-pgsql-build-db -l 5457 -c 1000 -t 1000 -w 100
Creating database...
=======================================
PGData directory /data/hgdb457 already exists
Skipping initdb
=======================================
Database is already started.
....

运行测试

使用命令dbt5-run-workload进行测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#dbt5-run-workload帮助选项
[root@k8s4 bin]# dbt5-run-workload -h
./dbt5-run-workload: option requires an argument -- h

error: specify which dbms system to test using -a <pgsql>

usage: run_workload.sh -c <number of customers> -d <duration of test> -u <number of users>
必选项:-c 客户数,跟加载数据时一样;-d 运行时间,单位秒;-u 同时跑多少用户,相当于数据库上的会话数
other options:
-a <pgsql> 指定数据库类型,这里只有一个选项pg
-b <database parameters> 设置数据库启动参数,忽略就好
-f <scale factor. (default 500)> 比例因子
-h <database host name. (default localhost)> 数据库地址,一定要写,本机也要写localhost,不然不连数据库,原因未知
-i <egen_path> egen的路径
-l <pacing delay. (default 0)> 延迟,可能是交易峰谷间的延迟,默认0
-n <database name. (default dbt5)> 数据库名称,不写默认dbt5
-o <result output dir> 结果存放路径,会自动生成一个目录
-p <database port number. (default 5432)> 数据库端口
-q <event1,[event2,...]> (Linux perf events) Linux性能事件,调用的是perf命令,感兴趣的自己研究吧
-r <random number seed, invalidates test> 随机数种子,无效测试。没明白是做什么的,需要的可也翻一下源码
-s <delay between starting threads in milliseconds (default 1000)> 启动线程间的延迟时间,默认1000ms
-t <customers total> 测试使用的客户数
-w <initial trade days. (default 300)> 初始交易天数,应该是小于等于生成数据时的天数
-x (oprofile) 是否使用oprofile进行性能分析,选择了还需要装oprofile,太麻烦了,没测试
-y (readprofile) 是否readprofile跟踪内核性能,同样需要安装,未测试
-z <comment describing this test run> 给本次测试写个注释吧

#一个简单测试
[root@k8s4 bin]# dbt5-run-workload -a pgsql -c 1000 -t 1000 -d 120 -u 5 -f 1000 -w 100 -p 5457 -n dbt5 -h localhost -o ./results
waiting for server to start.... done
server started

************************************************************************
* DBT-5 test started *
* *
************************************************************************
* *
* Test consists of 4 stages: *
* *
* 1. Start of the Brokerage House server *
* 2. Start of the Market Exchange server *
* 3. Test (Start of the Customer Emulator) *
* 4. Processing of results *
* *
************************************************************************
Results can be found in: ./results

1. Starting Brokerage House server
Sleeping 1 seconds

2. Starting Market Exchange server

3. Starting Customer driver: 5 user(s)
1 user starting every 1000 milliseconds...
Results will be written to: ./results
Sleeping 6 seconds
Sleeping 120 seconds

4. Run Post processing analyses
Killing Servers...
waiting for server to shut down........ done
server stopped
>> Test completed.
Results are in: ./results

Response Time
(seconds)
Transaction % Average: 90th % Total Rollbacks % Warning Invalid
----------------- ------- --------------- ------- -------------- ------- -------
Trade Result 8.309 nan: 0.018 3355 0 0.00% 0 0
Broker Volume 4.998 nan: 0.007 2018 0 0.00% 0 0
Customer Position 13.282 nan: 0.025 5363 0 0.00% 0 0
Market Feed 0.763 nan: 0.014 336 28 8.33% 0 0
Market Watch 0.166 nan: 0.006 7439 7372 99.10% 0 0
Security Detail 0.396 nan: 0.007 5777 5617 97.23% 0 160
Trade Lookup 8.141 nan: 0.111 3287 0 0.00% 0 0
Trade Order 10.211 nan: 0.015 4164 41 0.98% 0 0
Trade Status 19.338 nan: 0.040 7808 0 0.00% 0 0
Trade Update 2.051 nan: 0.141 828 0 0.00% 0 0
Data Maintenance N/A nan: 0.011 2 0 0.00% 0 0
----------------- ------- --------------- ------- -------------- ------- -------
27.96 trade-result transactions per second (trtps)
0.1 minute(s) to ramp up
2.0 minute steady state duration

结果查看,整体内容稍微有点复杂,可以只简单看倒数第三行,即:27.96 trade-result transactions per second (trtps),这里表示性能指标(tpsE, transactions per second E)和性价比(美元/tpsE),分值越大,性能越好。其他根据字面意思理解就好。

TPC-E及dbt5简单介绍

什么是TPC-E

TPC 测试基准 E (下文称 TPC-E) 是由事务处理性能委员会 (下称 TPC) 开发的一个新的用于测试 OLTP 负载的测试基准(基准于 2007 年被审核通过)。 TPC-E 通过模拟了证券交易公司的业务来测试数据库的 OLTP 性能。TPC-E 测试的重点是数据库,更多关注的是这个数据库处理来自证券公司和其客户账号的相关交易。 尽管在 TPC-E 测试基准之下的业务模型是一个证券公司的业务,但是数据库的表结构和数据分布,以及交易本身和实现规则都是尽最大程度的测试当下的 OLTP 数据库系统。

与TPCC的区别

关键的区别是,TPC-E 基准配置将更像一个真实客户会实际使用的配置。 这意味着基准测试中使用的软件和硬件配置应与客户端使用的软件相似或相同。 TPC-C 配置的绝对大小不能反映典型的客户端配置。

TPC-C模拟了批发分销商,拥有少量库存库存充足的仓库,为大量零售点提供服务,在这个场景下,通过衡量每分钟交易数(TPMC)表示性能情况。这种场景严重依赖磁盘IO。

TPC-E模拟了证券交易场景,使用由波动的股票价格驱动增加场景复杂度,同事模拟客户下单、限价交易、止损等场景。TPC-E场景更符合最新的OLTP场景,对磁盘IO性能比TPC-C低。

TPC-H用于测试OLAP场景,此处不介绍了。

什么是dbt5

简单来说dbt5就是TPC-E基准测试规范的一个实现。作为Google Summer of Code 2006中的一个项目,由Rilson Nascimento开发,由Mark Wong担任导师。

dbt5架构

业务架构

image

设计架构

image

dbt5中的表

类别 表名 注释
客户相关 ACCOUNT_PERMISSION 客户账目许可表
CUSTOMER 客户信息表
CUSTOMER_ACCOUNT 客户账目表
CUSTOMER_TAXRATE 客户税率表
HOLDING 客户股票持有表
HOLDING_HISTORY 客户股票持有历史表
HOLDING_SUMMARY 客户股票持有总表
WATCH_ITEM 客户观察证券列表
经纪人相关 BROKER 经纪人表
CASH_TRANSACTION 现金交易表
CHARGE 交易费用表
COMMISSION_RATE 佣金率表
SETTLEMENT 结算表
SETTLEMENT 结算表
TRADE_HISTORY 交易历史表
TRADE_REQUEST 交易请求表
TRADE_TYPE 交易类型表
交易所相关 COMPANY 公司表
COMPANY_COMPETITOR 公司竞争者表
DAILY_MARKET 日常市场统计表
EXCHANGE 交易所表
FINANCIAL 财政表
INDUSTRY 行业表
LAST_TRADE 最后交易表
NEWS_ITEM 新闻项表
NEWS_XREF 公司新闻参照表
SECTOR 公司领域表
SECURITY 证券表
其他类别 ADDRESS 地址表
STATUS_TYPE 交易状态表
TAXRATE 税率表
ZIP_CODE 邮政编码表

TPC-E标准中定义的事务有12种,每个事务对应数据库管理系统中的一个或多个带输入和输出参数的存储过程,单个存储过程叫做一个事务帧。事务的种类有如下几种:

  1. Broker-Volume:经纪人交易统计事务,包含1个事务帧;
  2. Customer-Position:客户价值统计事务,包含3个事务帧;
  3. Market-Watch:市场观察事务,包含1个事务帧;
  4. Security-Detail:证券信息事务,包含1个事务帧;
  5. Trade-Lookup:交易查询事务,包含4个事务帧;
  6. Trade-Order:交易执行事务,包含6个事务帧;
  7. Trade-Status:交易状态事务,包含1个事务帧;
  8. Trade-Update:交易修正事务,包含3个事务帧;
  9. Market-Feed:市场跟踪事务,包含1个事务帧,该事务由TradeOrder事务引起;
  10. Trade-Result:交易结果更新事务,包含6个事务帧,该事务由TradeOrder事务引起;
  11. Data-Maintenance:数据维护事务,包含1个事务帧,每60秒执行一次;
  12. Trade-Cleanup:交易清理事务,包含1个事务帧,测试开始时执行一次,不强制使用。

前8种事务由证券公司执行,第9-10号事务由交易所执行,最后两种事务属于数据库维护事务,与客户操作无关。

dbt5的优缺点

优点:

1、主程序使用C++编写,速度较快

2、提供了完整的测试数据

3、场景比benchmarkSQL更复杂,更贴近现实使用的业务系统

4、监控手段多样,信息详细,可以直接生产漂亮的报告格式

5、使用Libpq连接数据库,理论速度更快

6、GitHub上已经有了支持docker的更新,可以打包成docker镜像

7、复杂的场景可以用来作为pg优化的练手程序

缺点:

1、安装配置较复杂,使用的语言包括shell、Python、R、C++

2、没有服务端程序没有使用连接池,不能对数据库进行极限压测。如果要想跑出高分,修改程序增加连接池效果会好很多

3、有一定的学习成本