测试方法
<p>购买RDS-PostgreSQL实例后,您可以参照本文对实例性能进行性能测试。</p>
<p><strong><span style="font-size:18px">测试环境</span></strong></p>
<ul>
<li>地域:华北1,PostgreSQL实例和ECS实例在同一可用区</li>
<li>ECS的实例规格:g5.2xlarge.4(8核32GB)</li>
<li>ECS存储规格:普通云磁盘 200GB</li>
<li>网络类型:专有网络</li>
<li>操作系统:CentOS7.7-64Bit</li>
<li>RDS-PostgreSQL版本:PostgreSQL9.6 版本</li>
</ul>
<p><span style="font-size:18px"><strong>前提条件</strong></span></p>
<ul>
<li>您已成功创建RDS-PostgreSQL实例,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Instance_Management.Create_Instance.Create_Common_Instance" target="_blank">创建常规实例</a>。</li>
<li>您已成功创建ECS,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/Getting_Started.RDS_PostgreSQL.Create_ECS" target="_blank">创建ECS</a>。</li>
<li>您已成功开通白名单,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Security_Management.Create_Whitelist" target="_blank">开通白名单</a>。</li>
<li>您可以通过ECS连接RDS-PostgreSQL实例,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/Getting_Started.RDS_PostgreSQL.Connect_RDS_PostgreSQL.Linux" target="_blank">Linux系统连接</a>。</li>
</ul>
<p><span style="font-size:18px"><strong>测试工具</strong></span></p>
<p>pgbench是PostgreSQL自带的一款轻量级压力测试工具,是一种在PostgreSQL上运行基准测试的简单程序。可以在并发的数据库会话中重复运行相同的SQL语句。</p>
<p>安装方法:登录ECS,执行如下命令安装PostgreSQL客户端。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">yum install -y postgresql* </span></p>
</td>
</tr>
</tbody>
</table>
<p><span style="font-size:18px"><strong>测试步骤</strong></span></p>
<p>1. 在进行测试前,请提交工单修改RDS-PostgreSQL的实例参数。</p>
<p><img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20202907102834-1484b3ed9e73.png" style="height:26px; margin:0px; width:65px" />:需要同时修改主备实例上的参数(即postgresql.auto.conf文件)。</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">synchronous_commit = off</span></p>
<p><span style="font-family:Consolas">max_wal_size = 64GB # 1/2 当前RDS-PostgreSQL实例的规格内存</span></p>
<p><span style="font-family:Consolas">min_wal_size = 16GB # 1/8 当前RDS-PostgreSQL实例的规格内存</span><span style="font-family:Consolas"> </span></p>
</td>
</tr>
</tbody>
</table>
<p>2. 修改参数后,重启RDS-PostgreSQL实例使配置生效。</p>
<p>3. 登录ECS。</p>
<p>4. 根据目标库的大小,初始化测试数据:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p>//初始化数据50亿:</p>
<p>pgbench -i -s 50000</p>
<p> </p>
<p>//初始化数据10亿:</p>
<p>pgbench -i -s 10000</p>
<p> </p>
<p>//初始化数据5亿:</p>
<p>pgbench -i -s 5000</p>
<p> </p>
<p>//初始化数据1亿:</p>
<p>pgbench -i -s 1000</p>
<p> </p>
<p>//初始化数据5000万:</p>
<p>pgbench -i -s 500<span style="font-family:Consolas"> </span></p>
</td>
</tr>
</tbody>
</table>
<p>5. 执行以下命令,配置环境变量:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">export PGHOST=<<em>Domain-Name</em>></span></p>
<p><span style="font-family:Consolas">export PGPORT=<<em>Port</em>></span></p>
<p><span style="font-family:Consolas">export PGDATABASE=postgres</span></p>
<p><span style="font-family:Consolas">export PGUSER=<<em>Account</em>></span></p>
<p><span style="font-family:Consolas">export PGPASSWORD=<<em>Password</em>> </span></p>
</td>
</tr>
</tbody>
</table>
<p> <img src="https://pcp-portal-sca.obs-cn-shenzhen.pinganyun.com/pcp-portal-sca/20202907102834-1484b3ed9e73.png" style="height:26px; margin:0px; width:65px" />:</p>
<p> • <em>Domain-Name</em>:是实例的访问域名,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Instance_Management.View_Instance.View_Common_Instance" target="_blank">查看实例基本信息</a>。</p>
<p> • <em>Port</em>:实例的连接端口,可参考<a href="https://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Instance_Management.View_Instance.View_Common_Instance" target="_blank">查看实例基本信息</a>。</p>
<p> • <em>Account</em>:实例管理员名称,可参考<a href="http://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Instance_Management.Create_Instance.Create_Common_Instance" target="_blank">创建常规实例</a>。</p>
<p> • postgres:需要连接的数据库名称,默认为postgres数据库。</p>
<p> • <em>Password</em>:创建管理员账号时的密码,可参考<a href="http://pinganyun.com/ssr/help/database/RDS/User_Guide_RDS_PostgreSQL.Instance_Management.Create_Instance.Create_Common_Instance" target="_blank">创建常规实例</a>。</p>
<p>6. 创建只读和读写的测试脚本。</p>
<p> • 创建只读的测试脚本ro.sql:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">\set aid random_gaussian(1, :range, 10.0)</span></p>
<p><span style="font-family:Consolas">SELECT abalance FROM pgbench_accounts WHERE aid = :aid;</span><span style="font-family:Consolas"> </span></p>
</td>
</tr>
</tbody>
</table>
<p> • 创建读写的测试脚本rw.sql:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">\set aid random_gaussian(1, :range, 10.0) </span></p>
<p><span style="font-family:Consolas">\set bid random(1, 1 * :scale) </span></p>
<p><span style="font-family:Consolas">\set tid random(1, 10 * :scale) </span></p>
<p><span style="font-family:Consolas">\set delta random(-5000, 5000) </span></p>
<p><span style="font-family:Consolas">BEGIN; </span></p>
<p><span style="font-family:Consolas">UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid; </span></p>
<p><span style="font-family:Consolas">SELECT abalance FROM pgbench_accounts WHERE aid = :aid; </span></p>
<p><span style="font-family:Consolas">UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid; </span></p>
<p><span style="font-family:Consolas">UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid; </span></p>
<p><span style="font-family:Consolas">INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP); </span></p>
<p><span style="font-family:Consolas">END; </span></p>
</td>
</tr>
</tbody>
</table>
<p>7. 执行以下命令,进行测试。</p>
<p> • 只读测试:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">// rds.pg.test1,总数据量10亿,热数据1亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -v -r -P 1 -f ./ro.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=100000000</span></p>
<p> </p>
<p><span style="font-family:Consolas">// rds.pg.test1,总数据量10亿,热数据5亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -n -r -P 1 -f ./ro.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=500000000</span></p>
<p> </p>
<p><span style="font-family:Consolas">//rds.pg.test1,总数据量10亿,热数据10亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -n -r -P 1 -f ./ro.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=1000000000</span><span style="font-family:Consolas"> </span></p>
</td>
</tr>
</tbody>
</table>
<p> • 读写测试:</p>
<table border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#f7f8fa">
<p><span style="font-family:Consolas">// rds.pg.test1,总数据量10亿,热数据1亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -v -r -P 1 -f ./rw.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=100000000</span></p>
<p> </p>
<p><span style="font-family:Consolas">// rds.pg.test1,总数据量10亿,热数据5亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -n -r -P 1 -f ./rw.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=500000000</span></p>
<p> </p>
<p><span style="font-family:Consolas">// rds.pg.test1,总数据量10亿,热数据10亿</span></p>
<p><span style="font-family:Consolas">pgbench -M prepared -n -r -P 1 -f ./rw.sql -c 64 -j 64 -T 120 -D scale=10000 -D range=1000000000 </span></p>
</td>
</tr>
</tbody>
</table>
<p>其中:</p>
<table border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background-color:#ededed; vertical-align:top">
<p><strong>参数</strong></p>
</td>
<td style="background-color:#ededed; vertical-align:top">
<p><strong>说明</strong></p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-M</p>
</td>
<td style="vertical-align:top">
<p>请求协议,支持:</p>
<ul>
<li>simple</li>
<li>extended</li>
<li>prepared</li>
</ul>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-v</p>
</td>
<td style="vertical-align:top">
<p>测试前执行vacuum操作。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-n</p>
</td>
<td style="vertical-align:top">
<p>测试前不执行vacuum操作。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-r</p>
</td>
<td style="vertical-align:top">
<p>在测试结果中显示脚本中每条指令的平均延时。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-P</p>
</td>
<td style="vertical-align:top">
<p>每隔一段时间显示线程进度报告,单位:秒。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-f</p>
</td>
<td style="vertical-align:top">
<p>指定测试脚本位置,默认为pgbench自带的脚本。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-c</p>
</td>
<td style="vertical-align:top">
<p>测试的连接数,测试连接数不代表该规格的最大连接数,最大连接数请参考<a href="https://pinganyun.com/ssr/help/database/RDS/Product_Introduction.Instance_Type.RDS_PostgreSQL" target="_blank">实例类型的RDS-PostgreSQL</a>。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-j</p>
</td>
<td style="vertical-align:top">
<p>启动线程数,默认为1。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-T</p>
</td>
<td style="vertical-align:top">
<p>测试时间,单位:秒。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>-D</p>
</td>
<td style="vertical-align:top">
<p>传递测试脚本中的变量值。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>Scale</p>
</td>
<td style="vertical-align:top">
<p>测试的数据量,单位:10万。</p>
</td>
</tr>
<tr>
<td style="vertical-align:top">
<p>range</p>
</td>
<td style="vertical-align:top">
<p>表示活跃的数据量。</p>
</td>
</tr>
</tbody>
</table>
<p><strong><span style="font-size:18px">测试指标</span></strong></p>
<ul>
<li>只读QPS:数据库只读时,每秒执行的SQL数,仅包含SELECT。</li>
<li>读写QPS:数据库读写时,每秒执行的SQL数,包含INSERT、SELECT、UPDATE。</li>
</ul>
提交成功!非常感谢您的反馈,我们会继续努力做到更好!