use python threading multi-thread test PostgreSQL & mongodb insert tps

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云数据库 MongoDB,通用型 2核4GB
简介:
前面两篇测试了一下python单线程压mongo和PostgreSQL的性能.
相比PostgreSQL的pgbench, python用到的这两个驱动未使用异步接口, 对性能影响极大.


本文使用threading这个模块, 测试一下多线程的性能.
PostgreSQL测试脚本, 使用8个线程 :
$ vi test.py
import threading
import time
import postgresql

conn = { "user": "postgres",
         "database": "postgres",
         "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
       }

db = postgresql.open(**conn)
db.execute("drop table if exists tt")
db.execute("create table tt(id int, username name, age int2, email text, qq text)")
print(db.query("select count(1) as a from tt"))

class n_t(threading.Thread):   #The timer class is derived from the class threading.Thread
  def __init__(self, num):
    threading.Thread.__init__(self)
    self.thread_num = num

  def run(self): #Overwrite run() method, put what you want the thread do here
    conn = { "user": "postgres", 
             "database": "postgres",
             "unix": "/data01/pgdata/pg_root/.s.PGSQL.1921"
           }

    db = postgresql.open(**conn)
    ins = db.prepare("insert into tt values($1,$2,$3,$4,$5)")
    start_t = time.time()
    print("TID:" + str(self.thread_num) + " " + str(start_t))
    for i in range(0,125000):
      ins(1,'digoal.zhou',32,'digoal@126.com','276732431')
    stop_t = time.time()
    print("TID:" + str(self.thread_num) + " " + str(stop_t))
    print(stop_t-start_t)

def test():
  t_names = dict()
  for i in range(0, 8):
    t_names[i] = n_t(i) 
    t_names[i].start()
  return

if __name__ == '__main__':
  test()

测试结果 : 
比单线程187秒还慢了几十秒.
postgres@localhost-> python test.py
[(0,)]
TID:0 1423065305.517401
TID:3 1423065305.5209844
TID:1 1423065305.52123
TID:5 1423065305.5240796
TID:4 1423065305.5249543
TID:6 1423065305.5266497
TID:2 1423065305.5301073
TID:7 1423065305.533195
TID:5 1423065526.6725013
221.14842176437378
TID:7 1423065528.599816
223.06662106513977
TID:6 1423065529.8911068
224.36445713043213
TID:2 1423065530.6830883
225.15298104286194
TID:4 1423065531.1566184
225.63166403770447
TID:1 1423065531.4046018
225.88337182998657
TID:3 1423065531.4168346
225.8958501815796
TID:0 1423065531.5486302
226.03122925758362


mongodb测试脚本, 同样使用8个线程 :
# vi test.py
import threading
import time
import pymongo

c=pymongo.MongoClient('/tmp/mongodb-5281.sock')
db = c.test_database
db.drop_collection('test_collection')
collection = db.test_collection
print(collection.count())


class n_t(threading.Thread):   #The timer class is derived from the class threading.Thread
  def __init__(self, num):
    threading.Thread.__init__(self)
    self.thread_num = num

  def run(self): #Overwrite run() method, put what you want the thread do here
    c=pymongo.MongoClient('/tmp/mongodb-5281.sock')
    db = c.test_database
    collection = db.test_collection
    start_t = time.time()
    print("TID:" + str(self.thread_num) + " " + str(start_t))
    for i in range(0,125000):
      collection.insert({'id':1, 'username': 'digoal.zhou', 'age':32, 'email':'digoal@126.com', 'qq':'276732431'})
    stop_t = time.time()
    print("TID:" + str(self.thread_num) + " " + str(stop_t))
    print(stop_t-start_t)

def test():
  t_names = dict()
  for i in range(0,8):
    t_names[i] = n_t(i) 
    t_names[i].start()
  return

if __name__ == '__main__':
  test()

测试结果和单线程测试结果差不多 : 
[root@localhost ~]# python test.py
0
TID:0 1423066038.8190722
TID:1 1423066038.819762
TID:2 1423066038.8214562
TID:3 1423066038.8254952
TID:4 1423066038.827397
TID:5 1423066038.8303092
TID:6 1423066038.8326738
TID:7 1423066038.8461218
TID:5 1423066400.8412485
362.0109393596649
TID:3 1423066402.4937685
363.6682732105255
TID:2 1423066402.8351183
364.01366209983826
TID:4 1423066402.9675741
364.14017701148987
TID:1 1423066403.0420506
364.222288608551
TID:0 1423066403.284279
364.465206861496
TID:6 1423066403.6458826
364.81320881843567
TID:7 1423066403.6860046
364.839882850647

# ./mongo 127.0.0.1:5281/test_database
MongoDB shell version: 3.0.0-rc7
connecting to: 127.0.0.1:5281/test_database

> db.test_collection.count()
1000000


最后附上PostgreSQL pgbench使用8线程的测试结果 : 
耗时仅16秒, 比使用python测试的性能高出不少. (跑了800W事务, 所以要处以8和前面的结果比)
postgres@localhost-> vi test.sql
insert into tt values (1,'digoal.zhou',32,'digoal@126.com','276732431');

postgres@localhost-> pgbench -M prepared -n -r -f ./test.sql -c 8 -j 4 -t 1000000
transaction type: Custom query
scaling factor: 1
query mode: prepared
number of clients: 8
number of threads: 4
number of transactions per client: 1000000
number of transactions actually processed: 8000000/8000000
tps = 64215.539716 (including connections establishing)
tps = 64219.452898 (excluding connections establishing)
statement latencies in milliseconds:
        0.118040        insert into tt values (1,'digoal.zhou',32,'digoal@126.com','276732431');

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
1月前
|
存储 NoSQL 关系型数据库
一篇文章带你搞懂非关系型数据库MongoDB
一篇文章带你搞懂非关系型数据库MongoDB
57 0
|
9月前
|
存储 JSON NoSQL
NoSql非关系型数据库之MongoDB应用(三):MongoDB在项目中的初步应用
NoSql非关系型数据库之MongoDB应用(三):MongoDB在项目中的初步应用
|
7月前
|
NoSQL 关系型数据库 分布式数据库
RDS、PolarDB、Redis、MongoDB、DAS中执行一条修改语句为啥要开binlog呢
RDS、PolarDB、Redis、MongoDB、DAS中执行一条修改语句为啥要开binlog呢
113 1
|
8月前
|
NoSQL Cloud Native 关系型数据库
云原生数据库比较:MySQL、PostgreSQL、MongoDB和Cassandra的优势与劣势
选择适合自己应用的云原生数据库需要考虑多个因素,包括数据模型、性能需求、扩展性、学习曲线等。如果应用需要严格的 ACID 事务,关系型数据库如 MySQL 或 PostgreSQL 是不错的选择。如果应用需要灵活的数据模型和快速迭代开发,MongoDB 可能更适合。而对于大规模数据存储和高可用性需求,Cassandra 可能是一个值得考虑的选项。
579 3
云原生数据库比较:MySQL、PostgreSQL、MongoDB和Cassandra的优势与劣势
|
9月前
|
SQL NoSQL 数据可视化
NoSql非关系型数据库之MongoDB应用(二):安装MongoDB可视化工具
NoSql非关系型数据库之MongoDB应用(二):安装MongoDB可视化工具
|
9月前
|
SQL NoSQL 数据可视化
NoSql非关系型数据库之MongoDB应用(一):安装MongoDB服务
NoSql非关系型数据库之MongoDB应用(一):安装MongoDB服务
|
10月前
|
存储 JSON NoSQL
大数据数据存储的数据库的非关系型数据库之MongoDB
当今互联网时代,数据的处理和管理已成为各行各业必不可少的一部分。尤其是在大数据领域,数据存储更是至关重要。传统关系型数据库在某些场景下并不能完全满足需求,这时非关系型数据库就应运而生。其中MongoDB作为一个非常受欢迎的非关系型数据库,备受大家关注。本文将介绍MongoDB的概念、特点以及使用方法。
92 0
|
11月前
|
SQL 存储 NoSQL
「技术选型」比较MongoDB和PostgreSQL:谁才是王者?
「技术选型」比较MongoDB和PostgreSQL:谁才是王者?
|
11月前
|
存储 NoSQL 关系型数据库
「数据库选型」抛弃MongoDB,拥抱PostgreSQL,工作更轻松
「数据库选型」抛弃MongoDB,拥抱PostgreSQL,工作更轻松
|
11月前
|
存储 NoSQL Oracle
「数据库选型」卫报从MongoDB迁移到Amazon RDS上的PostgreSQL
「数据库选型」卫报从MongoDB迁移到Amazon RDS上的PostgreSQL