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

前面两篇测试了一下python单线程压mongo和PostgreSQL的性能.

相比PostgreSQL的pgbench, python用到的这两个驱动未使用异步接口, 对性能影响极大.

http://blog.163.com/digoal@126/blog/static/1638770402015142858224/

http://blog.163.com/digoal@126/blog/static/16387704020151210840303/

本文使用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');
时间: 2024-08-27 07:45:51

use python threading multi-thread test PostgreSQL & mongodb insert tps的相关文章

Python中多线程thread与threading的实现方法_python

学过Python的人应该都知道,Python是支持多线程的,并且是native的线程.本文主要是通过thread和threading这两个模块来实现多线程的. python的thread模块是比较底层的模块,python的threading模块是对thread做了一些包装的,可以更加方便的被使用. 这里需要提一下的是python对线程的支持还不够完善,不能利用多CPU,但是下个版本的python中已经考虑改进这点,让我们拭目以待吧. threading模块里面主要是对一些线程的操作对象化了,创建

python threading模块线程锁的例子

python threading模块有两类锁:互斥锁(threading.Lock )和可重用锁(threading.RLock).两者的用法基本相同,具体如下: lock = threading.Lock() lock.acquire() dosomething-- lock.release() RLock的用法是将threading.Lock()修改为threading.RLock().便于理解,先来段代码: [root@361way lock]# cat lock1.py #!/usr/b

python threading模块操作多线程介绍_python

python是支持多线程的,并且是native的线程.主要是通过thread和threading这两个模块来实现的.thread是比较底层的模块,threading是对thread做了一些包装的,可以更加方便的被使用.这里需要提一下的是python对线程的支持还不够完善,不能利用多CPU,但是下个版本的python中已经考虑改进这点,让我们拭目以待吧.     threading模块里面主要是对一些线程的操作对象化了,创建了叫Thread的class.一般来说,使用线程有两种模式,一种是创建线程

Python Web框架Pylons中使用MongoDB的例子

 这篇文章主要介绍了Python Web框架Pylons中使用MongoDB 的例子,大家参考使用 Pylons 经过漫长的开发,终于放出了 1.0 版本.对于正规的产品开发来说,1.0 版本的意义很大,这表明 Pylons 的 API 终于稳定下来了.   Pylons 虽是山寨 Rails 而生,但作为一个纯 Python 的 Web 框架,它有一个鲜明的特点:可定制性强.框架每一层都没重新发明轮子,而是尽量整合现有的 Python 库.在 MVC 的 Model 层,Pylons 默认支持

python-scrapy+mongodb insert的文档数目不够的原因?

问题描述 scrapy+mongodb insert的文档数目不够的原因? 我使用mongo储存scrapy爬下来的页面数据,在管道中为同时向db和txt中写入结果,结果发现txt有8000多条记录,而db中count才831条,百思不得其解.后来将db中数据导出,发现似乎是item['content'](文章内容)字段内容比较多的就没有成功插入db.后来想了下,似乎是db的单个文档大小有限制,但是这些文本写到txt最多不过几十kb,这到底是什么问题?附上管道process_item的代码 de

Python threading多线程编程实例_python

Python 的多线程有两种实现方法: 函数,线程类 1.函数 调用 thread 模块中的 start_new_thread() 函数来创建线程,以线程函数的形式告诉线程该做什么 复制代码 代码如下: # -*- coding: utf-8 -*- import thread def f(name):   #定义线程函数   print "this is " + name   if __name__ == '__main__':   thread.start_new_thread(f

Python THREADING模块中的JOIN()方法深入理解_python

看了oschina上的两个代码,受益匪浅.其中对join()方法不理解,看python官网文档的介绍: join([timeout]):等待直到进程结束.这将阻塞正在调用的线程,直到被调用join()方法的线程结束.(好难翻译,应该是这个意思) 哈哈,这个易懂. join方法,如果一个线程或者一个函数在执行过程中要调用另外一个线程,并且待到其完成以后才能接着执行,那么在调用这个线程时可以使用被调用线程的join方法. 复制代码 代码如下: #-*- encoding: gb2312 -*- im

python threading编程中的LOCK和RLOCK(可重入锁)

找到一本PYTHON并发编辑的书, 弄弄.. #!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time shared_resource_with_lock = 0 shared_resource_with_no_lock = 0 COUNT = 100000 shared_resource_lock = threading.Lock() class Box(object): lock = threadin

python threading之Condition的例子

Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的acquire和release方法外,还提供了wait和notify方法.线程首先acquire一个条件变量,然后判断一些条件.如果条件不满足则wait:如果条件满足,进行一些处理改变条件后,通过notify方法通知其他线程,其他处于wait状态的线程接到通知后会重新判断条件.不断的重复这一过程,从而解决复杂的同步问题. 除了上面画的acquire方法. releas