如何测试PostgreSQL数据库的性能

在这篇博客里,我将展示如何测量 PostgreSQL 数据库服务器的性能,并指导你如何运行一个基准测试。

基准测试旨在评估和比较不同配置、拓扑结构、系统和组件的性能。为此,我将使用 pgbench 工具。

你可能会问,为什么要使用一个单独的工具来测量 PostgreSQL 数据库的性能呢?毕竟,我们通常有一个特定的应用程序或系统访问数据库并操作数据。通过以不同模式运行现有应用程序并模拟不同的负载场景,我们可以检查数据库的性能。

然而,这种方法并不总是有效,因为应用程序本身可能有架构或系统限制。因此,生成足够的负载并客观评估数据库的性能可能非常困难和具有挑战性。当我们谈论测量数据库性能时,最具挑战性的事情是生成足够的负载。

pgbench 是一个针对 PostgreSQL 数据库的基准测试工具。它允许模拟多个客户端在 PostgreSQL 数据库上执行事务的工作负载。pgbench 实用程序在不同场景下测量数据库的性能。

假设我们已经安装了 PostgreSQL 数据库服务器:

dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d postgres  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
postgres=#

为了运行基准测试,我将创建一个新数据库。

create database my_benchmark_test_db;

通过运行 \list 命令,我可以验证数据库已成功创建:

postgres=# \l  
                                                  List of databases  
         Name         |  Owner   | Encoding | Collate | Ctype | ICU Locale | Locale Provider |   Access privileges  
----------------------+----------+----------+---------+-------+------------+-----------------+-----------------------  
 my_benchmark_test_db | postgres | UTF8     | en_IL   | en_IL |            | libc            |  
 postgres             | postgres | UTF8     | en_IL   | en_IL |            | libc            |  
 template0            | postgres | UTF8     | en_IL   | en_IL |            | libc            | =c/postgres          +  
                      |          |          |         |       |            |                 | postgres=CTc/postgres  
 template1            | postgres | UTF8     | en_IL   | en_IL |            | libc            | =c/postgres          +  
                      |          |          |         |       |            |                 | postgres=CTc/postgres  
(4 rows)  
  
postgres=#

为了让 pgbench 与新创建的数据库 my_benchmark_test_db 一起工作,运行以下命令:

pgbench -i -s 50 my_benchmark_test_db -h <db_hostname> -p <db_port> -U <db_user>

例如:

dmi@dmi-VirtualBox:~$ pgbench -i -s 50 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
dropping old tables...  
NOTICE:  table "pgbench_accounts" does not exist, skipping  
NOTICE:  table "pgbench_branches" does not exist, skipping  
NOTICE:  table "pgbench_history" does not exist, skipping  
NOTICE:  table "pgbench_tellers" does not exist, skipping  
creating tables...  
generating data (client-side)...  
5000000 of 5000000 tuples (100%) done (elapsed 10.19 s, remaining 0.00 s)  
vacuuming...  
creating primary keys...  
done in 30.29 s (drop tables 0.05 s, create tables 0.04 s, client-side generate 10.64 s, vacuum 4.75 s, primary keys 14.81 s).  
dmi@dmi-VirtualBox:~$

-i(初始化)选项告诉 pgbench 初始化指定的数据库。

连接到 PostgreSQL 数据库 my_benchmark_test_db,我们可以看到 pgbench 创建了四个表:pgbench_accounts、pgbench_branches、pgbench_history、pgbench_tellers


dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d my_benchmark_test_db  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
my_benchmark_test_db=# \d  
              List of relations  
 Schema |       Name       | Type  |  Owner  
--------+------------------+-------+----------  
 public | pgbench_accounts | table | postgres  
 public | pgbench_branches | table | postgres  
 public | pgbench_history  | table | postgres  
 public | pgbench_tellers  | table | postgres  
(4 rows)  
my_benchmark_test_db=#

让我们检查每个表中的行数:


my_benchmark_test_db=# select count(1) from pgbench_accounts;  
  count  
---------  
 5000000  
(1 row)  
  
my_benchmark_test_db=# select count(1) from pgbench_branches;  
 count  
-------  
    50  
(1 row)  
my_benchmark_test_db=# select count(1) from pgbench_history;  
 count  
-------  
     0  
(1 row)  
my_benchmark_test_db=# select count(1) from pgbench_tellers;  
 count  
-------  
   500  
(1 row)  
my_benchmark_test_db=#

数据库 my_benchmark_test_db 现在已经准备好用于测量我们的 PostgreSQL 数据库服务器的性能了。

当我们想要确定一个系统的性能时,我们通常会将其指标与某个基准进行比较。让我们运行 pgbench 实用程序并设置一个基准。

为此,我们使用以下参数运行 pgbench:

pgbench -c <要连接的客户端数量> -j <工作进程的数量> -t <要执行的事务数量> <样本数据库名称>

例如:


pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
  
dmi@dmi-VirtualBox:~$ pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
pgbench (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
starting vacuum...end.  
transaction type: <builtin: TPC-B (sort of)>  
scaling factor: 50  
query mode: simple  
number of clients: 10  
number of threads: 2  
maximum number of tries: 1  
number of transactions per client: 1000  
number of transactions actually processed: 10000/10000  
number of failed transactions: 0 (0.000%)  
latency average = 75.438 ms  
initial connection time = 160.700 ms  
tps = 132.559344 (without initial connection time)  
dmi@dmi-VirtualBox:~$

输出信息有很多,其中最有趣的是:

latency average = 75.438 ms  
initial connection time = 160.700 ms  
tps = 132.559344 (without initial connection time)

现在,让我们增加 PostgreSQL DB 服务器可用于缓存的内存量。
目的是将表和索引的内容存储在内存中。
为此,我们将更改 PostgreSQL 参数 shared_buffers。

当前设置为:

my_benchmark_test_db=# show shared_buffers;  
 shared_buffers  
----------------  
 128MB  
(1 row)  
  
my_benchmark_test_db=#

让我们将其更改为 2GB:


sudo vi /etc/postgresql/15/main/postgresql.conf  
  
...  
#------------------------------------------------------------------------------  
# RESOURCE USAGE (except WAL)  
#------------------------------------------------------------------------------  
# - Memory -  
shared_buffers = 1GB                    # min 128kB  
                                        # (change requires restart)  
...

更改需要重启 PostgreSQL 服务:

sudo systemctl restart postgresql

检查更改后的 PostgreSQL 参数 shared_buffers:


dmi@dmi-VirtualBox:~$ psql -h 127.0.0.1 -p 5444 -U postgres -d my_benchmark_test_db  
Password for user postgres:  
psql (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)  
Type "help" for help.  
  
my_benchmark_test_db=# show shared_buffers;  
 shared_buffers  
----------------  
 1GB  
(1 row)  
my_benchmark_test_db=#

在增加 shared_buffers PostgreSQL 参数后,重新运行 pgbench 实用程序:


pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
  
dmi@dmi-VirtualBox:~$ pgbench -c 10 -j 2 -t 1000 my_benchmark_test_db -h 127.0.0.1 -p 5444 -U postgres  
Password:  
pgbench (15.1 (Ubuntu 15.1-1.pgdg22.04+1))  
starting vacuum...end.  
transaction type: <builtin: TPC-B (sort of)>  
scaling factor: 50  
query mode: simple  
number of clients: 10  
number of threads: 2  
maximum number of tries: 1  
number of transactions per client: 1000  
number of transactions actually processed: 10000/10000  
number of failed transactions: 0 (0.000%)  
latency average = 47.632 ms  
initial connection time = 148.379 ms  
tps = 209.944478 (without initial connection time)  
dmi@dmi-VirtualBox:~$

latency average = 47.632 ms
initial connection time = 148.379 ms
tps = 209.944478 (without initial connection time)

在基准测试中,我们达到了每秒 132 次事务的速率。在这次运行中,增加 shared_buffers 参数后,我们达到了每秒 209 次事务,增加了约 58%。

最佳实践建议将 shared_buffers 值设置为系统内存的四分之一(1/4)。

通过使用 pgbench 实用程序测量数据库性能,我们可以继续进行。例如,我们可以比较不同版本的 PostgreSQL 引擎的性能。这样,我们可以确定 PostgreSQL 引擎的一个版本的性能是优于还是劣于另一个版本。

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值