pgbench 是一个方便的pg 性能测试工具,以下是简单的测试试用

安装

安装pg

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm
yum install postgresql10
yum install postgresql10-server
yum install -y postgresql10-contrib

配置环境变量

默认安装是/usr/pgsql-10/bin,所以需要配置path 路径

/etc/profile
export PATH=$PATH:/usr/pgsql-10/bin

简单试用

启动参数

以下为支持的参数

pgbench is a benchmarking tool for PostgreSQL.

Usage:
pgbench [OPTION]... [DBNAME] Initialization options:
-i, --initialize invokes initialization mode
-F, --fillfactor=NUM set fill factor
-n, --no-vacuum do not run VACUUM after initialization
-q, --quiet quiet logging (one message each 5 seconds)
-s, --scale=NUM scaling factor
--foreign-keys create foreign key constraints between tables
--index-tablespace=TABLESPACE
create indexes in the specified tablespace
--tablespace=TABLESPACE create tables in the specified tablespace
--unlogged-tables create tables as unlogged tables Options to select what to run:
-b, --builtin=NAME[@W] add builtin script NAME weighted at W (default: 1)
(use "-b list" to list available scripts)
-f, --file=FILENAME[@W] add script FILENAME weighted at W (default: 1)
-N, --skip-some-updates skip updates of pgbench_tellers and pgbench_branches
(same as "-b simple-update")
-S, --select-only perform SELECT-only transactions
(same as "-b select-only") Benchmarking options:
-c, --client=NUM number of concurrent database clients (default: 1)
-C, --connect establish new connection for each transaction
-D, --define=VARNAME=VALUE
define variable for use by custom script
-j, --jobs=NUM number of threads (default: 1)
-l, --log write transaction times to log file
-L, --latency-limit=NUM count transactions lasting more than NUM ms as late
-M, --protocol=simple|extended|prepared
protocol for submitting queries (default: simple)
-n, --no-vacuum do not run VACUUM before tests
-P, --progress=NUM show thread progress report every NUM seconds
-r, --report-latencies report average latency per command
-R, --rate=NUM target rate in transactions per second
-s, --scale=NUM report this scale factor in output
-t, --transactions=NUM number of transactions each client runs (default: 10)
-T, --time=NUM duration of benchmark test in seconds
-v, --vacuum-all vacuum all four standard tables before tests
--aggregate-interval=NUM aggregate data over NUM seconds
--log-prefix=PREFIX prefix for transaction time log file
(default: "pgbench_log")
--progress-timestamp use Unix epoch timestamps for progress
--sampling-rate=NUM fraction of transactions to log (e.g., 0.01 for 1%) Common options:
-d, --debug print debugging output
-h, --host=HOSTNAME database server host or socket directory
-p, --port=PORT database server port number
-U, --username=USERNAME connect as specified database user
-V, --version output version information, then exit
-?, --help show this help, then exit Report bugs to <pgsql-bugs@postgresql.org>.

准备一个数据库

为了方便使用docker-compose 运行,内容有点多,包含了mysql pg 以及一个pgbouncer(连接池工具)

version: "3"
services:
mysql:
image: mysql:5.7.16
volumes:
- ./gogs/mysql:/var/lib/mysql
ports:
- 3306:3306
command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
MYSQL_ROOT_PASSWORD: dalongrong
MYSQL_DATABASE: gogs
MYSQL_USER: gogs
MYSQL_PASSWORD: dalongrong
TZ: Asia/Shanghai
postgres:
image: postgres:9.6
ports:
- "5432:5432"
environment:
- "POSTGRES_PASSWORD:dalong"
volumes:
- ./db_data2:/var/lib/postgresql/data
pgbouncer:
image: brainsam/pgbouncer
environment:
- DB_HOST=postgres
- DB_USER=postgres
- DB_PASSWORD=dalong
ports:
- "6432:6432"

启动数据库

docker-compose up -d

简单测试

  • 初始化
pgbench -i --unlogged-tables -s 2 -U postgres -p 5432 -d postgres -h localhost

效果

pgbench -i --unlogged-tables -s 2 -U postgres -p 5432 -d postgres -h localhost
NOTICE: table "pgbench_history" does not exist, skipping
NOTICE: table "pgbench_tellers" does not exist, skipping
NOTICE: table "pgbench_accounts" does not exist, skipping
NOTICE: table "pgbench_branches" does not exist, skipping
creating tables...
100000 of 200000 tuples (50%) done (elapsed 0.03 s, remaining 0.03 s)
200000 of 200000 tuples (100%) done (elapsed 0.06 s, remaining 0.00 s)
vacuum...
set primary keys...
done. pgbench -i --unlogged-tables -s 16 -U postgres -p 5432 -h localhost -d postgres
creating tables...
100000 of 1600000 tuples (6%) done (elapsed 0.03 s, remaining 0.42 s)
200000 of 1600000 tuples (12%) done (elapsed 0.06 s, remaining 0.40 s)
300000 of 1600000 tuples (18%) done (elapsed 0.09 s, remaining 0.37 s)
400000 of 1600000 tuples (25%) done (elapsed 0.14 s, remaining 0.41 s)
500000 of 1600000 tuples (31%) done (elapsed 0.17 s, remaining 0.37 s)
600000 of 1600000 tuples (37%) done (elapsed 0.20 s, remaining 0.33 s)
700000 of 1600000 tuples (43%) done (elapsed 0.23 s, remaining 0.29 s)
800000 of 1600000 tuples (50%) done (elapsed 0.25 s, remaining 0.25 s)
900000 of 1600000 tuples (56%) done (elapsed 0.28 s, remaining 0.22 s)
1000000 of 1600000 tuples (62%) done (elapsed 0.31 s, remaining 0.19 s)
1100000 of 1600000 tuples (68%) done (elapsed 0.42 s, remaining 0.19 s)
1200000 of 1600000 tuples (75%) done (elapsed 0.52 s, remaining 0.17 s)
1300000 of 1600000 tuples (81%) done (elapsed 0.75 s, remaining 0.17 s)
1400000 of 1600000 tuples (87%) done (elapsed 0.96 s, remaining 0.14 s)
1500000 of 1600000 tuples (93%) done (elapsed 1.15 s, remaining 0.08 s)
1600000 of 1600000 tuples (100%) done (elapsed 1.55 s, remaining 0.00 s)
vacuum...
set primary keys...
done.
  • 执行压力测试
pgbench -M prepared -r -c 8 -j 2 -T 10 -U postgres -h localhost -p 5432 -d postgres -l

参数说明
以上参数中,-M prepared表示绑定变量形式的调用SQL,-r表示报告测试文件中每条SQL的平均执行延迟,
-c 8表示模拟8个客户端,-j 2表示pgbench的工作线程是2个,
-T 10表示压力测试的时间是10秒,-l表示把事务统计写入log,其余的是postgres连接相关的参数
以上参数,也可以结合命令帮助看出来
效果

scaling factor: 16
query mode: prepared
number of clients: 8
number of threads: 2
duration: 10 s
number of transactions actually processed: 4115
latency average = 19.924 ms
tps = 401.527925 (including connections establishing)
tps = 401.690314 (excluding connections establishing)
script statistics:
- statement latencies in milliseconds:
0.370 \set aid random(1, 100000 * :scale)
0.005 \set bid random(1, 1 * :scale)
0.005 \set tid random(1, 10 * :scale)
0.005 \set delta random(-5000, 5000)
3.051 BEGIN;
2.684 UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
1.693 SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
2.799 UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
3.317 UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
3.203 INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
2.740 END;

说明

我们主要比较关注的是tps 参数,结合一些系统调优的技巧进行tps 参数的调整,类似的sysbench 也是可以进行pg 压测的,以下为
sysbench 的简单测试命令

sysbench \
--db-driver=pgsql \
--pgsql-host=127.0.0.1 \
--pgsql-port=5432 \
--pgsql-user=postgres \
--pgsql-password=dalong \
--pgsql-db=postgres \
--oltp-table-size=10000 \
--rand-init=on \
--threads=10 \
--time=120 \
--events=0 \
--report-interval=10 \
--percentile=99 \
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
prepare sysbench \
--db-driver=pgsql \
--pgsql-host=127.0.0.1 \
--pgsql-port=5432 \
--pgsql-user=postgres \
--pgsql-password=dalong \
--pgsql-db=postgres \
--oltp-table-size=10000 \
--threads=10 \
--time=120 \
--events=0 \
--report-interval=10 \
--percentile=99 \
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
run sysbench \
--db-driver=pgsql \
--pgsql-host=127.0.0.1 \
--pgsql-port=5432 \
--pgsql-user=postgres \
--pgsql-password=dalong \
--pgsql-db=postgres \
--oltp-table-size=10000 \
--threads=10 \
--time=120 \
--events=0 \
--report-interval=10 \
--percentile=99 \
/usr/share/sysbench/tests/include/oltp_legacy/oltp.lua \
cleanup

参考资料

https://www.postgresql.org/docs/10/pgbench.html
https://github.com/rongfengliang/mysql-postgres-pgbouncer-docker-compose

 

pgbench 安装试用的更多相关文章

  1. 项目管理工具 Redmine 安装试用手记

    原文:项目管理工具 Redmine 安装试用手记 项目管理工具 Redmine 安装试用手记 分类: Redmine2009-06-01 10:12 996人阅读 评论(1) 收藏 举报 项目管理工具 ...

  2. Tencent Server Web 安装试用

    Tencent Server Web 安装试用 私有环境搭建,使用docker-compose 进行memcache 安装 参考github 代码 https://github.com/rongfen ...

  3. toxiproxy 安装试用

    备注:    实际上是一个代理工具,但是又不是简单的进行代理(tcp,可以配置策略,toxics 实现延迟,模拟故障,    对于这个大家可能了解的就是netflix 公司的chaos monkey, ...

  4. windows openssh server 安装试用

    使用Windows的可能会知道win10 的已经包好了openssh 服务,但是对于其他机器win 7 windows 2008 ,就需要其他的方法了 还好powershell 团队开发了支持wind ...

  5. flynn 开源paas 平台安装试用

    flynn 是一个不错的开源paas 平台,基于git 以及容器技术,开发模型与 heroku 基本一样,同时构建方式就是基于heroku 的buildpacks 安装 官方文档提示说明是ubuntu ...

  6. nsq 安装试用

    因为是mac 系统安装试用brew install nsq 安装 brew install nsq 组件说明 nsqd 守护进程进行消息的接受,缓存以及传递消息给客户端,需要配置nsqlookupd地 ...

  7. apache phoenix 安装试用

    备注:   本次安装是在hbase docker 镜像的基础上配置的,主要是为了方便学习,而hbase搭建有觉得   有点费事,用镜像简单.   1. hbase 镜像 docker pull har ...

  8. Nchan 安装试用(openresty 同时支持)

    备注:        使用nginx最新的源码包(nginx-1.13.6),以及源码安装   1. 下载源码包(nginx+ Nchan) https://nginx.org/download/ng ...

  9. casperjs 安装试用

    纠结了好久,一直报错, 具体错误信息现在已经忘了,后来看了官网的prerequisite,才知道要安装特定版本或greater 的 phantomjs 和 Python.我这边主要是没装python. ...

随机推荐

  1. Cracking The Coding Interview 3.5

    //Implement a MyQueue class which implements a queue using two stacks. #include <iostream> #in ...

  2. Spring-data-JPA详细介绍

    Spring-data-JPA学习: 1. https://blog.csdn.net/liujianwd/article/details/75411009 2.http://www.cnblogs. ...

  3. 1076 Wifi密码

    下面是微博上流传的一张照片:“各位亲爱的同学们,鉴于大家有时需要使用 wifi,又怕耽误亲们的学习,现将 wifi 密码设置为下列数学题答案:A-1:B-2:C-3:D-4:请同学们自己作答,每两日一 ...

  4. 从命令行模式运行Windows管理工具。

    从命令行模式运行Windows管理工具. 分类: Play Windows 2004-08-06 16:39 6076人阅读 评论(3) 收藏 举报 1.可以直接在开始-〉运行里面输入的管理工具: 文 ...

  5. 分析无线遥控器信号并制作Hack硬件进行攻击

    无线遥控器(无线电遥控器)在我们生活中非常常见,应用于各种场景,方便着用户的使用.不过大多数还是用于安防方面的,比如: 遥控报警器.电动卷帘门.电动伸缩门.遥控电开关.无线遥控门铃…… 1.无线遥控器 ...

  6. uwsgi理解

    uwsgi uWSGI 是一个 Web 服务器,它实现了 WSGI 协议.uwsgi.http 等协议.Nginx 中HttpUwsgiModule 的作用是与 uWSGI 服务器进行交换.WSGI ...

  7. SQL server 存储过程学习

    一.定义变量--简单赋值 declare @a intset @a=5 print @a --使用select语句赋值 declare @user1 nvarchar(50) select @user ...

  8. 深度强化学习介绍 【PPT】 Human-level control through deep reinforcement learning (DQN)

    这个是平时在实验室讲reinforcement learning 的时候用到PPT, 交期末作业.汇报都是一直用的这个,觉得比较不错,保存一下,也为分享,最早该PPT源于师弟汇报所做.

  9. libev

    libev是一个**事件驱动库**,它需要循环探测事件是否发生,在Linux上实际是封装了epoll等系统调用. 其循环过程由ev_loop( )函数设置,循环体是ev_loop结构. //创建事件循 ...

  10. 模拟主库创建数据文件,dg备库空间不足时问题处理

    本篇文档测试目的: 模拟实际环境中,主库对表空间添加数据文件,备库空间不足,最终导致MRP进程自动断开,处理方式. 1.问题环境模拟 1)正常情况下的dg 主库创建数据文件,备库接受日志,自动创建表空 ...