compare python use py-postgresql & direct pgbench's performance

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:
使用py-postgresql驱动链接PostgreSQL, 使用unix socket链接.
测试插入性能, 单线程插入100万数据需要172秒.
$ vi test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
for i in range(0,1000000):
  ins(i)
print(time.time())

postgres@localhost-> python test.py
1422871147.8219907
1422871319.8280523
耗费172秒

TOP
 593328 postgres  20   0  189128  16960   7632 R  81.3  0.0   0:09.31 /usr/local/bin/python3 test.py                                
 593329 postgres  20   0 8628412  26260  24088 S  32.2  0.0   0:04.41 postgres: postgres postgres [local] idle  


使用while循环亦如此 : 
postgres@localhost-> cat test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
i = 0
while i<1000000:
  ins(i)
  i=i+1
print(time.time())

postgres@localhost-> python test.py
1422872074.1050985
1422872255.3471527


postgres@localhost-> cat test.py
import postgresql
import time
conn = { "user": "postgres", 
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
}

db = postgresql.open(**conn)
db.execute("create table if not exists tt(id int)")
ins = db.prepare("insert into tt values($1)")
print(time.time())
i = 0
while i<1000000:
  ins(i)
  i=i+1
print(time.time())


使用pgbench单线程插入100万, 只需要55秒. 
postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -c 1 -j 1 -t 1000000
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 1
number of threads: 1
number of transactions per client: 1000000
number of transactions actually processed: 1000000/1000000
tps = 18156.624380 (including connections establishing)
tps = 18157.507920 (excluding connections establishing)
statement latencies in milliseconds:
        0.001353        \setrandom id 0 1000000
        0.052901        insert into tt values (:id)

postgres=# select 1000000/18157.507920;
      ?column?       
---------------------
 55.0736369994104345
(1 row)


之前使用sysbench测试和pgbench测试相差也巨大.
sysbench, py-postgresql和C+libpq比性能损耗差距巨大.
python循环消耗极低, 看样子还是在驱动上, 未使用异步接口:
print(time.time())
for i in range(0,1000000):
  pass
print(time.time())

postgres@localhost-> python test.py
1422872588.5488484
1422872588.610911


其他
1. py-postgresql的例子
事务使用db.xact():
>>> import postgresql
>>> db = postgresql.open('pq://user:password@host:port/database')
>>> db.execute("CREATE TABLE emp (emp_first_name text, emp_last_name text, emp_salary numeric)")
>>> make_emp = db.prepare("INSERT INTO emp VALUES ($1, $2, $3)")
>>> make_emp("John", "Doe", "75,322")
>>> with db.xact():
...  make_emp("Jane", "Doe", "75,322")
...  make_emp("Edward", "Johnson", "82,744")



[参考]
1.  https://pypi.python.org/pypi/py-postgresql/
相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
4月前
|
SQL 关系型数据库 测试技术
postgresql|数据库|数据库测试工具pgbench之使用
postgresql|数据库|数据库测试工具pgbench之使用
81 0
|
关系型数据库 测试技术 数据库
PostgreSQL数据库压力测试工具pgbench简单应用
PG数据库提供了一款轻量级的压力测试工具叫pgbench,其实就是一个编译好后的扩展性的可执行文件。
2692 0
|
安全 关系型数据库 API
Python3连接PostgreSQL(10.5)数据库
Python3连接PostgreSQL(10.5)数据库
554 0
Python3连接PostgreSQL(10.5)数据库
|
SQL 关系型数据库 测试技术
PostgreSQL pgbench tpcb 海量数据库测试 - 分区表测试优化
标签 PostgreSQL , pgbench , tpcb 背景 pgbench是PG的一款测试工具,内置的测试CASE为tpcb测试。同时支持用户自己写测试CASE。 大量自定义CASE参考 https://github.com/digoal/blog/blob/master/201711/readme.md 当我们使用tpcb测试CASE时,如果生成的数据量过于庞大,例如我最近在生成1万亿的CASE,可以考虑使用分区表,但是目前PG内置分区表的性能在分区非常多时,使用PREPARED STATEMENT会导致性能下降。
1847 0
|
SQL 关系型数据库 测试技术
PostgreSQL pgbench tpcb 数据生成与SQL部分源码解读
标签 PostgreSQL , pgbench , tpcb 背景 pgbench是PG的一款测试工具,内置的测试CASE为tpcb测试。同时支持用户自己写测试CASE。 大量自定义CASE参考 https://github.com/digoal/blog/blob/master/201711/readme.md 本文为pgbench 内置tpcb的解读。
1618 0
|
物联网 关系型数据库 数据库
|
关系型数据库 测试技术 PostgreSQL
|
SQL 关系型数据库 PostgreSQL