PostgreSQL数据库压力测试工具pgbench简单应用

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核4GB 50GB
简介: PG数据库提供了一款轻量级的压力测试工具叫pgbench,其实就是一个编译好后的扩展性的可执行文件。
PG数据库提供了一款轻量级的压力测试工具叫pgbench,其实就是一个编译好后的扩展性的可执行文件。介绍如下。 
 环境: 
CentOS 5.7(final) 
PG:9.1.2 
Vmware 8.0 
数据库参数: max_connection=100 ,其他略,默认 

1.安装 
进入源码安装包,编译,安装
[postgres@localhost  ~]$ cd postgresql-9.1.2/contrib/pgbench/
[postgres@localhost  pgbench]$ ll
total 164
-rw-r--r--. 1 postgres postgres   538 Dec  1  2011 Makefile
-rwxrwxr-x. 1 postgres postgres 50203 Apr 26 23:50 pgbench
-rw-r--r--. 1 postgres postgres 61154 Dec  1  2011 pgbench.c
-rw-rw-r--. 1 postgres postgres 47920 Apr 26 23:50 pgbench.o
[postgres@localhost  pgbench]$make all
[postgres@localhost  pgbench]$make install
安装完毕以后可以在bin文件夹下看到新生成的pgbench文件
[postgres@localhost  bin]$ ll $PGHOME/bin pgbench
-rwxr-xr-x. 1 postgres postgres 50203 Jul  8 20:28 pgbench
2.参数介绍
[postgres@localhost  bin]$ pgbench --help
pgbench is a benchmarking tool for PostgreSQL.

Usage:
  pgbench [OPTIONS]... [DBNAME]

Initialization options:
  -i           invokes initialization mode
  -F NUM       fill factor
  -s NUM       scaling factor

Benchmarking options:
  -c NUM       number of concurrent database clients (default: 1)
  -C           establish new connection for each transaction
  -D VARNAME=VALUE
               define variable for use by custom script
  -f FILENAME  read transaction script from FILENAME
  -j NUM       number of threads (default: 1)
  -l           write transaction times to log file
  -M {simple|extended|prepared}
               protocol for submitting queries to server (default: simple)
  -n           do not run VACUUM before tests
  -N           do not update tables "pgbench_tellers" and "pgbench_branches"
  -r           report average latency per command
  -s NUM       report this scale factor in output
  -S           perform SELECT-only transactions
  -t NUM       number of transactions each client runs (default: 10)
  -T NUM       duration of benchmark test in seconds
  -v           vacuum all four standard tables before tests

Common options:
  -d           print debugging output
  -h HOSTNAME  database server host or socket directory
  -p PORT      database server port number
  -U USERNAME  connect as specified database user
  --help       show this help, then exit
  --version    output version information, then exit

Report bugs to .
3.初始化测试数据
[postgres@localhost  ~]$ pgbench -i pgbench
creating tables...
10000 tuples done.
20000 tuples done.
30000 tuples done.
40000 tuples done.
50000 tuples done.
60000 tuples done.
70000 tuples done.
80000 tuples done.
90000 tuples done.
100000 tuples done.
set primary key...
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "pgbench_branches_pkey" for table "pgbench_branches"
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "pgbench_tellers_pkey" for table "pgbench_tellers"
NOTICE:  ALTER TABLE / ADD PRIMARY KEY will create implicit index "pgbench_accounts_pkey" for table "pgbench_accounts"
vacuum...done.
[postgres@localhost  ~]$ psql -d pgbench
psql (9.1.2)
Type "help" for help.

pgbench=# select count(1) from pgbench_accounts;
 count  
--------
 100000
(1 row)

pgbench=# select count(1) from pgbench_branches;
 count 
-------
     1
(1 row)

pgbench=# select count(1) from pgbench_history;
 count 
-------
     0
(1 row)

pgbench=# select count(1) from pgbench_tellers;
 count 
-------
    10
(1 row)

pgbench=# \d+ pgbench_accounts
                Table "public.pgbench_accounts"
  Column  |     Type      | Modifiers | Storage  | Description 
----------+---------------+-----------+----------+-------------
 aid      | integer       | not null  | plain    | 
 bid      | integer       |           | plain    | 
 abalance | integer       |           | plain    | 
 filler   | character(84) |           | extended | 
Indexes:
    "pgbench_accounts_pkey" PRIMARY KEY, btree (aid)
Has OIDs: no
Options: fillfactor=100

pgbench=# \d+ pgbench_branches
                Table "public.pgbench_branches"
  Column  |     Type      | Modifiers | Storage  | Description 
----------+---------------+-----------+----------+-------------
 bid      | integer       | not null  | plain    | 
 bbalance | integer       |           | plain    | 
 filler   | character(88) |           | extended | 
Indexes:
    "pgbench_branches_pkey" PRIMARY KEY, btree (bid)
Has OIDs: no
Options: fillfactor=100

pgbench=# \d+ pgbench_history 
                      Table "public.pgbench_history"
 Column |            Type             | Modifiers | Storage  | Description 
--------+-----------------------------+-----------+----------+-------------
 tid    | integer                     |           | plain    | 
 bid    | integer                     |           | plain    | 
 aid    | integer                     |           | plain    | 
 delta  | integer                     |           | plain    | 
 mtime  | timestamp without time zone |           | plain    | 
 filler | character(22)               |           | extended | 
Has OIDs: no

pgbench=# \d+ pgbench_tellers 
                Table "public.pgbench_tellers"
  Column  |     Type      | Modifiers | Storage  | Description 
----------+---------------+-----------+----------+-------------
 tid      | integer       | not null  | plain    | 
 bid      | integer       |           | plain    | 
 tbalance | integer       |           | plain    | 
 filler   | character(84) |           | extended | 
Indexes:
    "pgbench_tellers_pkey" PRIMARY KEY, btree (tid)
Has OIDs: no
Options: fillfactor=100
说明: 
a.这里使用的是默认的参数值,带-s 参数时可指定测试数据的数据量,-f可以指定测试的脚本,这里用的是默认脚本 
b.不要在生产的库上做,新建一个测试库,当生产上有同名的测试表时将被重置 

4.测试过程 
4.1 1个session

[postgres@localhost  ~]$ nohup pgbench -c 1 -T 20 -r pgbench > file.out  2>&1
[postgres@localhost  ~]$ more file.out 
nohup: ignoring input
starting vacuum...end.
transaction type: TPC-B (sort of)
scaling factor: 1
query mode: simple
number of clients: 1
number of threads: 1
duration: 20 s
number of transactions actually processed: 12496                                                     tps = 624.747958 (including connections establishing)                                                tps = 625.375564 (excluding connections establishing)
statement latencies in milliseconds:
        0.005299        \set nbranches 1 * :scale
        0.000619        \set ntellers 10 * :scale
        0.000492        \set naccounts 100000 * :scale
        0.000700        \setrandom aid 1 :naccounts
        0.000400        \setrandom bid 1 :nbranches
        0.000453        \setrandom tid 1 :ntellers
        0.000430        \setrandom delta -5000 5000
        0.050707        BEGIN;
        0.200909        UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
        0.098718        SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
        0.111621        UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
        0.107297        UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
        0.095156        INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
        0.919101        END;
4.2 30个session
[postgres@localhost  ~]$nohup pgbench -c 30 -T 20 -r pgbench > file.out  2>&1
[postgres@localhost  ~]$ more file.out 
nohup: ignoring input
starting vacuum...end.
transaction type: TPC-B (sort of)
scaling factor: 1
query mode: simple
number of clients: 30
number of threads: 1
duration: 20 s
number of transactions actually processed: 8056                                                      tps = 399.847446 (including connections establishing)                                                tps = 404.089024 (excluding connections establishing)
statement latencies in milliseconds:
    0.004195        \set nbranches 1 * :scale
    0.000685        \set ntellers 10 * :scale
    0.000887        \set naccounts 100000 * :scale
    0.000805        \setrandom aid 1 :naccounts
    0.000656        \setrandom bid 1 :nbranches
    0.000523        \setrandom tid 1 :ntellers
    0.000499        \setrandom delta -5000 5000
    0.515565        BEGIN;
    0.865217        UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
    0.307207        SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
    50.543371       UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
    19.210089       UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
    0.384190        INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
    2.116383        END;
4.3 50个session
[postgres@localhost  ~]$nohup pgbench -c 50 -T 20 -r pgbench > file.out  2>&1
[postgres@localhost  ~]$ more file.out 
nohup: ignoring input
starting vacuum...end.
transaction type: TPC-B (sort of)
scaling factor: 1
query mode: simple
number of clients: 50
number of threads: 1
duration: 20 s
number of transactions actually processed: 7504                                                      tps = 370.510431 (including connections establishing)                                                tps = 377.964565 (excluding connections establishing)
statement latencies in milliseconds:
        0.004291        \set nbranches 1 * :scale
        0.000769        \set ntellers 10 * :scale
        0.000955        \set naccounts 100000 * :scale
        0.000865        \setrandom aid 1 :naccounts
        0.000513        \setrandom bid 1 :nbranches
        0.000580        \setrandom tid 1 :ntellers
        0.000522        \setrandom delta -5000 5000
        0.604671        BEGIN;
        1.480723        UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
        0.401148        SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
        104.713566      UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
        21.562787       UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
        0.412209        INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
        2.243497        END;
4.4 100个session 
超过100个会报错,因为数据库当前设置最大session是100
[postgres@localhost  ~]$ nohup pgbench -c 100 -T 20 -r pgbench> file.out  2>&1
[postgres@localhost  ~]$ more file.out 
nohup: ignoring input
starting vacuum...end.
transaction type: TPC-B (sort of)
scaling factor: 1
query mode: simple
number of clients: 100
number of threads: 1
duration: 20 s
number of transactions actually processed: 6032                                                      tps = 292.556692 (including connections establishing)                                                tps = 305.595090 (excluding connections establishing)
statement latencies in milliseconds:
        0.004508        \set nbranches 1 * :scale
        0.000787        \set ntellers 10 * :scale
        0.000879        \set naccounts 100000 * :scale
        0.001620        \setrandom aid 1 :naccounts
        0.000485        \setrandom bid 1 :nbranches
        0.000561        \setrandom tid 1 :ntellers
        0.000656        \setrandom delta -5000 5000
        3.660809        BEGIN;
        4.198062        UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
        1.727076        SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
        281.955832      UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
        27.054125       UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
        0.524155        INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
        2.710619        END;
5.说明 
我们主要关心的是最后的输出报告中的TPS值,里面有两个,一个是包含网络开销(including),另一个是不包含网络开销的(excluding),这个值是反映的每秒处理的事务数,反过来也可以查出每个事务数所消耗的平均时间,一般认为能将硬件用到极致,速度越快越好。 
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
15天前
|
Java 流计算
Flink-03 Flink Java 3分钟上手 Stream 给 Flink-02 DataStreamSource Socket写一个测试的工具!
Flink-03 Flink Java 3分钟上手 Stream 给 Flink-02 DataStreamSource Socket写一个测试的工具!
31 1
Flink-03 Flink Java 3分钟上手 Stream 给 Flink-02 DataStreamSource Socket写一个测试的工具!
|
2天前
|
监控 测试技术 持续交付
掌握跨平台测试策略:确保应用的无缝体验
【10月更文挑战第14天】在多元化设备和操作系统的今天,跨平台测试策略成为确保应用质量和性能的关键。本文探讨了跨平台测试的重要性、核心优势及实施步骤,涵盖Web、移动和桌面应用的测试方法,帮助开发者提高应用的无缝体验。
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
探索AI在软件测试中的创新应用与实践###
本文旨在探讨人工智能(AI)技术如何革新软件测试领域,提升测试效率、质量与覆盖范围。通过深入分析AI驱动的自动化测试工具、智能化缺陷预测模型及持续集成/持续部署(CI/CD)流程优化等关键方面,本研究揭示了AI技术在解决传统软件测试痛点中的潜力与价值。文章首先概述了软件测试的重要性和当前面临的挑战,随后详细介绍了AI技术在测试用例生成、执行、结果分析及维护中的应用实例,并展望了未来AI与软件测试深度融合的趋势,强调了技术伦理与质量控制的重要性。本文为软件开发与测试团队提供了关于如何有效利用AI技术提升测试效能的实践指南。 ###
|
5天前
|
jenkins 测试技术 持续交付
提升软件测试效率的实用技巧与工具
【10月更文挑战第12天】 本文将深入探讨如何通过优化测试流程、引入自动化工具和持续集成等策略,来显著提高软件测试的效率。我们将分享一些实用的技巧和工具,帮助测试人员更高效地发现和定位问题,确保软件质量。
16 2
|
5天前
|
机器学习/深度学习 弹性计算 自然语言处理
前端大模型应用笔记(二):最新llama3.2小参数版本1B的古董机测试 - 支持128K上下文,表现优异,和移动端更配
llama3.1支持128K上下文,6万字+输入,适用于多种场景。模型能力超出预期,但处理中文时需加中英翻译。测试显示,其英文支持较好,中文则需改进。llama3.2 1B参数量小,适合移动端和资源受限环境,可在阿里云2vCPU和4G ECS上运行。
|
13天前
|
Web App开发 敏捷开发 Java
自动化测试框架的选择与应用
【10月更文挑战第4天】在软件开发的海洋中,自动化测试如同一艘航船,帮助开发者们快速穿越测试的波涛。选择适合项目的自动化测试框架,是确保航行顺利的关键。本文将探讨如何根据项目需求选择合适的自动化测试框架,并分享一些实用的代码示例,助你启航。
|
13天前
|
测试技术
黑盒功能测试工具UFT的使用
黑盒功能测试工具UFT的使用
24 0
黑盒功能测试工具UFT的使用
|
17天前
|
XML 网络安全 数据格式
Kali渗透测试:Windows事件管理工具wevtutil的使用方法(一)
Kali渗透测试:Windows事件管理工具wevtutil的使用方法(一)
53 2
|
17天前
|
敏捷开发 测试技术 持续交付
自动化测试框架的选择与应用
在软件开发的海洋中,自动化测试犹如一座灯塔,指引着质量保证的方向。本文将探讨如何根据项目需求选择适合的自动化测试框架,以及在实际工作中如何有效应用这些框架来提升软件质量和开发效率。我们将从框架的基本概念出发,逐步深入到框架选择的标准,最后通过实际案例分析,展示自动化测试框架的应用效果。
|
7天前
|
机器学习/深度学习 数据采集 人工智能
软件测试中的人工智能应用与挑战
【10月更文挑战第10天】 在当今信息技术飞速发展的时代,软件系统日益复杂且多样化,传统的手工测试方法已无法满足快速迭代和高效发布的需求。人工智能(AI)技术的引入为软件测试领域带来了新的希望和机遇。本文将探讨人工智能在软件测试中的应用现状、所面临的挑战以及未来的发展趋势,旨在启发读者思考如何更好地利用AI技术提升软件测试的效率和质量。
22 0