PostgreSQL 主机性能测试方法 - 单机单实例

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

背景

业界有一些通用的数据库性能测试模型,可以用来测试硬件在不同测试模型下的性能表现。
参考
http://www.tpc.org/
https://github.com/oltpbenchmark/oltpbench
http://oltpbenchmark.com/

本文主要以PostgreSQL为例,向大家介绍一下,如何使用PostgreSQL来测试硬件的性能。

PostgreSQL 的功能非常的强大,所以可以适用于几乎所有的测试模型,同时用户还可以根据自己的应用场景设计测试模型。

前面已经介绍了单机多实例的测试方法。

本文介绍的是单机单实例的测试方法。

一、单机io测试

使用fio测试磁盘或块设备的IO能力。

1. 安装libaio库

# yum install -y libaio libaio-devel
# mkdir -p /data01/digoal
# chown dege.zz /data01/digoal

2. 安装fio

$ git clone https://github.com/axboe/fio
$ cd fio
$ ./configure --prefix=/home/digoal/fiohome
$ make -j 32
$ make install

$ export PATH=/home/digoal/fiohome/bin:$PATH

3. 测试顺序读写,随机读写8K数据块。

fio -filename=/data01/digoal/testdir -direct=1 -thread -rw=write -ioengine=libaio -bs=8K -size=16G -numjobs=128 -runtime=60 -group_reporting -name=mytest >/tmp/fio_write.log 2>&1
fio -filename=/data01/digoal/testdir -direct=1 -thread -rw=read -ioengine=libaio -bs=8K -size=16G -numjobs=128 -runtime=60 -group_reporting -name=mytest >/tmp/fio_read.log 2>&1
fio -filename=/data01/digoal/testdir -direct=1 -thread -rw=randwrite -ioengine=libaio -bs=8K -size=16G -numjobs=128 -runtime=60 -group_reporting -name=mytest >/tmp/fio_randwrite.log 2>&1
fio -filename=/data01/digoal/testdir -direct=1 -thread -rw=randread -ioengine=libaio -bs=8K -size=16G -numjobs=128 -runtime=60 -group_reporting -name=mytest >/tmp/fio_randread.log 2>&1

测试数据关注

bps(MB/S)
lat(min,us)
lat(max,us)
lat(avg,us)
lat(stddev,us)

二、单机数据库准备

测试单实例,所以不使用cgroup

1. 配置环境变量

$ vi ~/envpg.sh

export PS1="$USER@`/bin/hostname -s`-> "
export PGPORT=1921
export PGDATA=/data02/digoal/pg_root_single
export LANG=en_US.utf8
export PGHOME=/home/digoal/pgsql9.6rc1
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export DATE=`date +"%Y%m%d%H%M"`
export PATH=/home/fiohome/bin:$PGHOME/bin:$PATH:.
export MANPATH=/home/fiohome/man:$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
alias rm='rm -i'
alias ll='ls -lh'
unalias vi

. ~/envpg.sh

2. 初始化数据库集群

$ initdb -D $PGDATA -U postgres -E UTF8 --locale=C -X /data01/digoal/pg_xlog_single

3. 配置数据库

$ cd $PGDATA


$ vi postgresql.conf
listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
port = 1921                             # (change requires restart)
max_connections = 300                   # (change requires restart)
unix_socket_directories = '.'   # comma-separated list of directories
shared_buffers = 96GB                   # min 128kB
maintenance_work_mem = 2GB              # min 1MB
autovacuum_work_mem = 2GB               # min 1MB, or -1 to use maintenance_work_mem
dynamic_shared_memory_type = posix      # the default is the first option
bgwriter_delay = 10ms                   # 10-10000ms between rounds
bgwriter_lru_maxpages = 1000            # 0-1000 max buffers written/round
bgwriter_lru_multiplier = 10.0          # 0-10.0 multiplier on buffers scanned/round
wal_buffers = 128MB                    # min 32kB, -1 sets based on shared_buffers
wal_writer_flush_after = 0              # 0 disables
checkpoint_timeout = 55min              # range 30s-1h
max_wal_size = 192GB 
checkpoint_completion_target = 0.0      # checkpoint target duration, 0.0 - 1.0
random_page_cost = 1.0                  # same scale as above
effective_cache_size = 480GB
constraint_exclusion = on  # on, off, or partition
log_destination = 'csvlog'              # Valid values are combinations of
logging_collector = on          # Enable capturing of stderr and csvlog
log_truncate_on_rotation = on           # If on, an existing log file with the
log_checkpoints = on
log_connections = on
log_disconnections = on
log_error_verbosity = verbose           # terse, default, or verbose messages
log_timezone = 'PRC'
autovacuum_vacuum_scale_factor = 0.002  # fraction of table size before vacuum
datestyle = 'iso, mdy'
timezone = 'PRC'
lc_messages = 'C'                       # locale for system error message
lc_monetary = 'C'                       # locale for monetary formatting
lc_numeric = 'C'                        # locale for number formatting
lc_time = 'C'                           # locale for time formatting
default_text_search_config = 'pg_catalog.english'
synchronous_commit=on
full_page_writes=on
autovacuum_naptime=10s
autovacuum_max_workers=16


$ vi pg_hba.conf
host all all 0.0.0.0/0 trust

4. 启动数据库

$ pg_ctl start

三、单机数据库测试 - TPC-B

1. 初始化TPC-B数据

tpc-b (40亿记录)

准备数据

$ pgbench -i -s 40000

2. 持续测试15天(在客户端机器启动测试,假设客户端POSTGRESQL已部署)

$ vi test.sh

date +%F%T >/tmp/single.log
pgbench -M prepared -n -r -P 1 -c 128 -j 128 -T 1296000 -h 目标机器IP -p 数据库端口 -U postgres postgres >>/tmp/single.log 2>&1
date +%F%T >>/tmp/single.log

$ chmod 700 test.sh

$ nohup ./test.sh >/dev/null 2>&1 &

3. 测试结果

$ cat /tmp/single.log

$ head -n 30000 /tmp/single.log |tail -n 7200 > /tmp/1
$ cat /tmp/1|awk '{print $4 "," $7 "," $10}' >/tmp/2

输出TPS,RT,标准差。

TPS表示数据库视角的事务处理能力(也就是单个测试脚本的每秒调用次数)。

RT表示响应时间。

标准差可以用来表示抖动,通常应该在1以内(越大,说明抖动越厉害)。

4. 主机性能结果

$ sar -f ....

四、单机数据库测试 - 定制测试1

测试分区表upsert 即 insert on conflict

安装分区表插件

$ git clone https://github.com/postgrespro/pg_pathman
$ cd pg_pathman
$ export PATH=/home/digoal/pgsql9.6rc1/bin:$PATH
$ make USE_PGXS=1
$ make install USE_PGXS=1

$ vi $PGDATA/postgresql.conf
shared_preload_libraries='pg_pathman'

$ pg_ctl restart -m fast

$ psql
postgres=# create extension pg_pathman;
CREATE EXTENSION

1. 定制测试脚本
例如测试insert on conflict, 20亿分区表,单表2000万。

$ psql
postgres=# drop table test;
postgres=# create table test(id int primary key, info text, crt_time timestamptz);
CREATE TABLE
postgres=# 
postgres=# select create_range_partitions('test'::regclass, 'id', 1, 20000000, 100, false);
 create_range_partitions 
-------------------------
                     100
(1 row)
postgres=# select disable_parent('test'::regclass);
 disable_parent 
----------------

(1 row)

reconnect  
postgres=# explain select * from test where id=1;
                                   QUERY PLAN                                    
---------------------------------------------------------------------------------
 Append  (cost=0.15..2.17 rows=1 width=44)
   ->  Index Scan using test_1_pkey on test_1  (cost=0.15..2.17 rows=1 width=44)
         Index Cond: (id = 1)
(3 rows)

新建的表如下

postgres=# \d+ test_1
                                  Table "postgres.test_1"
  Column  |            Type             | Modifiers | Storage  | Stats target | Description 
----------+-----------------------------+-----------+----------+--------------+-------------
 id       | integer                     | not null  | plain    |              | 
 info     | text                        |           | extended |              | 
 crt_time | timestamp without time zone |           | plain    |              | 
Indexes:
    "test_1_pkey" PRIMARY KEY, btree (id)
Check constraints:
    "pathman_test_1_1_check" CHECK (id >= 1 AND id < 20000001)
Inherits: test

postgres=# \d+ test_100
                                 Table "postgres.test_100"
  Column  |            Type             | Modifiers | Storage  | Stats target | Description 
----------+-----------------------------+-----------+----------+--------------+-------------
 id       | integer                     | not null  | plain    |              | 
 info     | text                        |           | extended |              | 
 crt_time | timestamp without time zone |           | plain    |              | 
Indexes:
    "test_100_pkey" PRIMARY KEY, btree (id)
Check constraints:
    "pathman_test_100_1_check" CHECK (id >= 1980000001 AND id < 2000000001)
Inherits: test

目前9.6在分区表执行on conflict有个BUG,已提交给社区。

insert into test(id,info,crt_time) values(:id, md5(random()::text), now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time;

先使用function代替

create or replace function upsert_test(int,text,timestamptz) returns void as $$ 
declare
begin
  update test set info=$2,crt_time=$3 where id=$1;
  if not found then
    insert into test(id,info,crt_time) values ($1,$2,$3);
  end if;
  exception when others then
  return;
end;
$$ language plpgsql strict;

pgbench脚本

\set id random(1,2000000000)
select upsert_test(:id,md5(random()::text),now());

2. 测试

$ vi test.sh

date +%F%T >/tmp/single.log
pgbench -M prepared -f test.sql -n -r -P 1 -c 128 -j 128 -T 1296000 -h 目标机器IP -p 数据库端口 -U postgres postgres >>/tmp/single.log 2>&1
date +%F%T >>/tmp/single.log

$ chmod 700 test.sh

$ nohup ./test.sh >/dev/null 2>&1 &

五、单机数据库测试 - 定制测试2

测试单表upsert 即 insert on conflict

1. 定制测试脚本
例如测试insert on conflict, 单表20亿。

$ psql
postgres=# drop table test cascade;
postgres=# create table test(id int primary key, info text, crt_time timestamptz);
CREATE TABLE

pgbench脚本

vi test.sql

\set id random(1,2000000000)
insert into test(id,info,crt_time) values(:id, md5(random()::text), now()) on conflict (id) do update set info=excluded.info,crt_time=excluded.crt_time;

2. 测试

$ vi test.sh

date +%F%T >/tmp/single.log
pgbench -M prepared -f test.sql -n -r -P 1 -c 128 -j 128 -T 1296000 -h 目标机器IP -p 数据库端口 -U postgres postgres >>/tmp/single.log 2>&1
date +%F%T >>/tmp/single.log

$ chmod 700 test.sh

$ nohup ./test.sh >/dev/null 2>&1 &

Count

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
15天前
|
Java
IDEA快捷测试方法可用性
IDEA快捷测试方法可用性
17 0
|
18天前
|
数据可视化 测试技术
深入理解软件测试中的风险评估方法
【4月更文挑战第19天】 在软件开发的生命周期中,风险评估是确保产品质量和项目成功的关键步骤。本文将探讨几种常用的软件测试风险评估方法,包括定性分析和定量分析,并讨论它们在不同类型的测试环境中的应用。通过案例研究和最佳实践,我们将展示如何有效识别、评估和管理测试过程中可能遇到的风险,以及如何制定相应的缓解策略,以优化资源分配和提高测试效率。
|
13天前
|
测试技术 API Python
Appium控件交互策略:优化自动化测试效率的关键方法
该文介绍了如何使用Selenium与APP进行交互,包括点击、输入和状态判断等操作。例如,通过element.click()点击控件,element.send_keys()输入文本,以及element.is_displayed()检查元素是否可见。还展示了如何获取元素属性,如resource-id、text和class,并提供了Python代码示例来定位并操作APP元素,如滑动条的显示、可点击性检测及点击滑动条中心位置。在编写测试脚本时,应注意元素定位和状态验证以确保测试稳定性。
19 1
|
7天前
|
存储 大数据 测试技术
矢量数据库的性能测试与评估方法
【4月更文挑战第30天】本文探讨了矢量数据库的性能测试与评估方法,强调其在大数据和AI时代的重要性。文中介绍了负载测试、压力测试、容量测试、功能测试和稳定性测试五大评估方法,以及实施步骤,包括确定测试目标、设计用例、准备环境、执行测试和分析结果。这些方法有助于确保数据库的稳定性和高效性,推动技术发展。
|
14天前
|
API 开发者
免费邮箱API发送邮件测试调试的方法和步骤
本文介绍了使用免费邮箱API如aoksend、Mailgun、SMTP2GO发送邮件的测试调试步骤:选择合适的API,获取访问密钥,配置邮件参数,编写测试代码,调试和测试,查看发送日志,以及优化改进邮件发送功能,确保其稳定运行。
|
15天前
|
XML 测试技术 数据库
深入理解自动化测试中的数据驱动方法
【4月更文挑战第23天】 在软件测试领域,为了提高测试效率和质量,自动化测试已成为不可或缺的实践。数据驱动测试(DDT)作为一种高效的自动化测试策略,它通过将测试逻辑与测试数据分离,允许测试人员以更灵活、可维护的方式设计用例。本文将探讨数据驱动方法的基本原理,实现方式以及在实际项目中的应用案例,旨在帮助读者深入理解并有效运用数据驱动方法来提升自动化测试的效率和质量。
|
29天前
|
自然语言处理 测试技术 持续交付
现代软件测试方法与挑战
传统软件测试方法在当前快速发展的软件开发环境下面临着诸多挑战,因此,现代软件测试方法的探索与应用显得尤为重要。本文将介绍几种现代软件测试方法,并探讨其在应对软件开发挑战方面的作用。
12 0
|
17天前
|
网络协议 安全 测试技术
性能工具之emqtt-bench BenchMark 测试示例
【4月更文挑战第19天】在前面两篇文章中介绍了emqtt-bench工具和MQTT的入门压测,本文示例 emqtt_bench 对 MQTT Broker 做 Beachmark 测试,让大家对 MQTT消息中间 BenchMark 测试有个整体了解,方便平常在压测工作查阅。
102 7
性能工具之emqtt-bench BenchMark 测试示例
|
28天前
|
测试技术 C语言
网站压力测试工具Siege图文详解
网站压力测试工具Siege图文详解
29 0
|
10天前
|
机器学习/深度学习 数据采集 人工智能
【专栏】利用AI辅助工具提高软件测试效率与准确性
【4月更文挑战第27天】本文探讨了AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计。AI辅助工具利用机器学习、自然语言处理和图像识别提高效率,但面临数据质量、模型解释性、维护更新及安全性挑战。未来,AI将更注重用户体验,提升透明度,并在保护隐私的同时,通过联邦学习等技术共享知识。AI在软件测试领域的前景广阔,但需解决现有挑战。

相关产品

  • 云原生数据库 PolarDB