批量造数据程序,适用于压力测试,测试数据准备,SQL查询语句优化(需要大量数据)
可一定之进程数,每个进程中开启线程数,已经进程处理的数据量等
Mr. Neo Chen (netkiller), 陈景峰(BG7NYT)
版权 © 2011, 2012 http://netkiller.github.com
下面是我多年积累下来的经验总结,整理成文档供大家参考:
Netkiller Architect 手札 | Netkiller Linux 手札 | Netkiller Developer 手札 | Netkiller Database 手札 |
Netkiller Debian 手札 | Netkiller CentOS 手札 | Netkiller FreeBSD 手札 | Netkiller Shell 手札 |
Netkiller Web 手札 | Netkiller Monitoring 手札 | Netkiller Storage 手札 | Netkiller Mail 手札 |
Netkiller Security 手札 | Netkiller Multimedia 手札 | Netkiller Writer 手札 | Netkiller Version 手札 |
Netkiller PostgreSQL 手札 | Netkiller MySQL 手札 | Netkiller Cryptography 手札 | Netkiller Cisco IOS 手札 |
Netkiller LDAP 手札 | Netkiller Intranet 手札 | |
主程序
-
-
-
-
-
-
-
-
- logfile = '/tmp/loopdata.log'
-
- import MySQLdb
- import Queue
- import threading
- import time
- import logging
- import os,sys
- import random, string
-
- import table
-
- class ThreadDB(threading.Thread):
- def __init__(self, queue):
- threading.Thread.__init__(self)
- self.queue = queue
- logging.basicConfig(level=logging.NOTSET,
- format='%(asctime)s %(levelname)-8s %(message)s',
- datefmt='%Y-%m-%d %H:%M:%S',
- filename=logfile,
- filemode='a')
- self.logging = logging.getLogger()
- self.logging.debug(self.name + ' Start')
-
-
- def run(self):
- db=MySQLdb.connect(host='localhost', user='neo', passwd='chen',db="neo", charset="utf8")
-
- cursor=db.cursor()
- running = True
- sql = None
- while running:
-
- try:
- sql = self.queue.get()
- except Exception as e:
- print (e)
- running=False
- break
- try:
-
- n = 0
- n = cursor.execute(sql)
- log = self.name +"\t"+ '' +''+' '+sql
- self.logging.debug(log.replace('\n','').replace('\t',''))
-
- except NameError as e:
- print (e)
- break
- except Exception as e:
- print (e)
- break
- except:
- break
-
- self.queue.task_done()
- db.commit()
- cursor.close()
- db.close()
- self.logging.debug(self.name + ' End')
-
- class Main():
- def __init__(self):
- self.queue = Queue.Queue()
- def threading(self, maxconn = 100):
- for i in range(maxconn):
- t = ThreadDB(self.queue)
- t.setDaemon(True)
- t.start()
- pass
- def run(self, count = 0):
-
- n = 0
- while n < count:
-
- self.queue.put(table.test1())
-
- n &#61; n &#43; 1
-
-
- self.queue.join()
-
- class Loop():
- def __init__(self, process &#61; 1, thread &#61; 1, count &#61; 1):
- n &#61; 0
- while n < process :
- try:
- pid &#61; os.fork()
- if pid > 0:
-
- sys.exit(0)
- else:
- main &#61; Main()
- start &#61; time.time()
- main.threading(thread)
- main.run(count)
- print ("Elapsed Time: %s" % (time.time() - start))
- except OSError, e:
- print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
- sys.exit(1)
- n &#61; n &#43; 1
- if __name__ &#61;&#61; &#39;__main__&#39;:
-
-
-
-
-
-
-
-
-
- try:
- loop &#61; Loop(5, 100, 10000)
- except KeyboardInterrupt:
- print ("Crtl&#43;C Pressed. Shutting down.")
- os.exit()
- $ cat table.py
-
- import random,string
- def "background-color: #ffffff;">test0():
- sql &#61;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- chinese_name &#61; &#39;test&#39;
- sex &#61; random.choice(("male", "female"))
- id_name &#61; random.choice(("身份证","护照","军人证"))
- id_number &#61; random.randint(000000000000000000, 999999999999999999)
-
- city &#61; random.choice((
- "黑龙江省哈尔滨市",
- "黑龙江省齐齐哈尔市",
- "黑龙江省鸡西市",
- "黑龙江省鹤岗市",
- "黑龙江省双鸭山市",
- "黑龙江省大庆市",
- "黑龙江省伊春市",
- "黑龙江省佳木斯市",
- "黑龙江省七台河市",
- "黑龙江省牡丹江市",
- "黑龙江省黑河市",
-
- "广东省广州市",
- "广东省深圳市",
- "广东省珠海市",
- "广东省汕头市",
- "广东省佛山市",
- "广东省江门市",
- "广东省湛江市",
- "广东省茂名市",
- "广东省肇庆市",
- "广东省惠州市",
- "广东省梅州市",
- "广东省汕尾市",
- "广东省河源市",
- "广东省阳江市",
- "广东省清远市",
- "广东省东莞市",
- "广东省中山市",
- "广东省潮州市",
- "广东省揭阳市",
- "广东省云浮市"
- ))
-
- address &#61; city &#43; random.choice(("XXXX", "XXXX", "XXXX"))
-
- mobile_prefix &#61; random.choice(("131", "133", "134", &#39;135&#39;, &#39;136&#39;, &#39;137&#39;,&#39;138&#39;, &#39;139&#39;, &#39;186&#39;))
- mobile_suffer &#61; random.randint(00000000, 99999999)
- mobile &#61; &#39;086-&#39;&#43;mobile_prefix &#43; str(mobile_suffer)
-
- bankname &#61; random.choice((
- "中国工商银行",
- "招商银行",
- "中国农业银行",
- "中国建设银行",
- "中国银行",
- "中国民生银行",
- "中国光大银行",
- "中信银行",
- "交通银行",
- "兴业银行",
- "上海浦东发展银行",
- "中国人民银行",
- "华夏银行",
- "深圳发展银行",
- "广东发展银行",
- "国家开发银行",
- "中国邮政储蓄银行",
- "中国进出口银行",
- "中国农业发展银行"
- ))
- bankaddr &#61; city &#43; bankname
- bankno &#61; random.randint(000000000000, 9999999999999)
-
- createtime &#61; random.randint(0000000000, 9999999999)
-
- amount &#61; str(random.randint(50, 999999)) &#43; &#39;.00&#39;
- add_amount &#61; &#39;0.00&#39;
-
- question &#61; &#39;question&#39;
- answer &#61; &#39;answer&#39;
-
- browser &#61; random.choice(("Mozilla/4.0", "Firefox 3.5", "MSIE 7", "MSIE 8", "Chrome 3.0", "Opera 7.1", "Safer 5.0"))
-
- country &#61; &#39;中国(大陆)&#39;
-
- username &#61; string.join(random.sample([&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;,&#39;g&#39;,&#39;h&#39;,&#39;i&#39;,&#39;j&#39;], 3)).replace(" ","")
-
- return(sql % (chinese_name &#43; username , sex, id_name, id_number, address, mobile, bkname, bkaddr, bno, createtime,amount, question, answer, browser, ))
-
-
- def test1():
-
-
-
- insert &#61;
-
- sql &#61; insert % (_demousers_value(),_demousers_value(),_demousers_value(),_demousers_value(),_demousers_value(), _demousers_value(),_demousers_value(),_demousers_value(),_demousers_value(),_demousers_value())
-
- return(sql)
-
- def _demousers_value():
- name &#61; string.join(random.sample([&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;, &#39;f&#39;, &#39;g&#39;, &#39;h&#39;, &#39;i&#39;, &#39;j&#39;, &#39;k&#39;, &#39;l&#39;, &#39;m&#39;, &#39;n&#39;, &#39;o&#39;, &#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;u&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;, &#39;y&#39;, &#39;z&#39;,
- &#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;, &#39;E&#39;, &#39;F&#39;, &#39;G&#39;, &#39;H&#39;, &#39;I&#39;, &#39;J&#39;, &#39;K&#39;, &#39;L&#39;, &#39;M&#39;, &#39;N&#39;, &#39;O&#39;, &#39;P&#39;, &#39;Q&#39;, &#39;R&#39;, &#39;S&#39;, &#39;T&#39;, &#39;U&#39;, &#39;V&#39;, &#39;W&#39;, &#39;X&#39;, &#39;Y&#39;, &#39;Z&#39;
- ], 6)).replace(" ","")
- sex &#61; random.choice(("male", "female"))
- mobile_prefix &#61; random.choice(("131", "133", "134", &#39;135&#39;, &#39;136&#39;, &#39;137&#39;,&#39;138&#39;, &#39;139&#39;, &#39;186&#39;))
- mobile_suffer &#61; str(random.randint(00000000, 99999999))
- mobile &#61; mobile_prefix &#43; str(mobile_suffer)
- qq &#61; str(random.randint(00000000, 99999999))
- email &#61; &#39; &#39;
- other_contact &#61; &#39; &#39;
- username &#61; str(random.randint(00000000, 99999999))
- password &#61; str(random.randint(00000000, 99999999))
- createtime &#61; str(random.randint(000000000, 999999999))
- ad_from &#61; &#39; &#39;
- ad_keyword &#61; &#39; &#39;
- area &#61; &#39;cn&#39;
- browser &#61; random.choice(("Mozilla/4.0", "Firefox 3.5", "MSIE 7", "MSIE 8", "Chrome 3.0", "Opera 7.1", "Safer 5.0"))
- sms_status &#61; random.choice((&#39;succeed&#39;,&#39;failed&#39;,&#39;mobilesucceed&#39;,&#39;emailsucceed&#39;,&#39;unsend&#39;))
-
- values &#61; % (name, sex, nationality, mobile,qq,email,username,password, createtime,browser)
- return(values);
仅供参考&#xff0c;学习使用&#xff0c;不能直接运行&#xff0c;table.py需要根据你的实际情况定制