批量造数据程序

上一篇 / 下一篇  2017-03-26 12:33:10 / 个人分类:python

摘要:批量造数据程序,适用于压力测试,测试数据准备,SQL查询语句优化(需要大量数据)   可一定之进程数,每个进程中开启线程数,已经进程处理的数据量等     Mr. Neo Chen (netkiller), 陈景峰(BG7NYT) <openunix@163.

批量造数据程序,适用于压力测试,测试数据准备,SQL查询语句优化(需要大量数据)

 

可一定之进程数,每个进程中开启线程数,已经进程处理的数据量等

 

 

MrNeo Chen (netkiller)陈景峰(BG7NYT)

版权 © 2011, 2012 http://netkiller.github.com

摘要

 

 

 

主程序

 

 

Python代码  收藏代码
  1. #!/usr/bin/env python3  
  2. #-*- coding: utf-8 -*-  
  3. ##############################################  
  4. # Home  : http://netkiller.sourceforge.net/  
  5. # Author: Neo <openunix@163.com>  
  6. ##############################################  
  7. # vim:ts=4:sw=4  
  8.   
  9. logfile = '/tmp/loopdata.log'  
  10. ##############################################  
  11. import MySQLdb  
  12. import Queue  
  13. import threading  
  14. import time  
  15. import logging  
  16. import os,sys  
  17. import random, string  
  18.   
  19. import table  
  20.   
  21. class ThreadDB(threading.Thread):  
  22.     def __init__(self, queue):  
  23.         threading.Thread.__init__(self)  
  24.         self.queue = queue  
  25.         logging.basicConfig(level=logging.NOTSET,  
  26.                     format='%(asctime)s %(levelname)-8s %(message)s',  
  27.                     datefmt='%Y-%m-%d %H:%M:%S',  
  28.                     filename=logfile,  
  29.                     filemode='a')  
  30.         self.logging = logging.getLogger()  
  31.         self.logging.debug(self.name + ' Start')  
  32.   
  33.   
  34.     def run(self):  
  35.         db=MySQLdb.connect(host='localhost', user='neo', passwd='chen',db="neo", charset="utf8")  
  36.   
  37.         cursor=db.cursor()  
  38.         running = True  
  39.         sql = None  
  40.         while running:  
  41.             #grabs host from queue  
  42.             try:  
  43.                 sql = self.queue.get()  
  44.             except Exception as e:  
  45.                 print (e)  
  46.                 running=False  
  47.                 break  
  48.             try:  
  49.                   
  50.                 n = 0                 
  51.                 n = cursor.execute(sql)  
  52.                 log = self.name +"\t"'' +''+' '+sql  
  53.                 self.logging.debug(log.replace('\n','').replace('\t',''))  
  54.   
  55.             except NameError as e:  
  56.                 print (e)  
  57.                 break  
  58.             except Exception as e:  
  59.                 print (e)  
  60.                 break  
  61.             except:  
  62.                 break  
  63.   
  64.             self.queue.task_done()  
  65.         db.commit()  
  66.         cursor.close()  
  67.         db.close()  
  68.         self.logging.debug(self.name + ' End')  
  69.   
  70. class Main():  
  71.     def __init__(self):  
  72.         self.queue = Queue.Queue()  
  73.     def threading(self, maxconn = 100):  
  74.         for i in range(maxconn):  
  75.             t = ThreadDB(self.queue)  
  76.             t.setDaemon(True)  
  77.             t.start()  
  78.         pass          
  79.     def run(self, count = 0):  
  80.           
  81.         n = 0  
  82.         while n < count:  
  83.               
  84.             self.queue.put(table.test1())  
  85.   
  86.             n = n + 1  
  87.   
  88.         #wait on the queue until everything has been processed       
  89.         self.queue.join()  
  90.   
  91. class Loop():  
  92.     def __init__(self, process = 1, thread = 1, count = 1):  
  93.         n = 0  
  94.         while n < process :  
  95.             try:   
  96.                 pid = os.fork()   
  97.                 if pid > 0:  
  98.                     # exit first parent  
  99.                     sys.exit(0)   
  100.                 else:  
  101.                     main = Main()  
  102.                     start = time.time()  
  103.                     main.threading(thread)  
  104.                     main.run(count)  
  105.                     print ("Elapsed Time: %s" % (time.time() - start))            
  106.             except OSError, e:   
  107.                 print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)   
  108.                 sys.exit(1)           
  109.             n = n + 1  
  110. if __name__ == '__main__':  
  111.   
  112.     """ 
  113.     main = Main() 
  114.     start = time.time() 
  115.     main.threading(5) 
  116.     main.run(5) 
  117.     print ("Elapsed Time: %s" % (time.time() - start)) 
  118.     """  
  119.   
  120.     try:  
  121.         loop = Loop(510010000)  
  122.     except KeyboardInterrupt:  
  123.         print ("Crtl+C Pressed. Shutting down.")  
  124.         os.exit()  

 

 

 

Python代码  收藏代码
  1. $ cat table.py  
  2. #-*- coding: utf-8 -*-  
  3. import random,string  
  4. def <span style="background-color: #ffffff;">test0</span>():  
  5.     sql = """ 
  6.     INSERT INTO test0 
  7.     VALUES( 
  8.         '%s', 
  9.         '%s', 
  10.         '%s', 
  11.         '%s',    
  12.         '%s', 
  13.         '%s', 
  14.         '%s', 
  15.         '%s', 

TAG:

 

评分:0

我来说两句

Open Toolbar