标签:学习 ext oca random odi 多少 ini daemon long
以前写过的python多线程终于派上用场了,其实还没开始测试,但下周会使用这个脚本测试一下,虽然boss让我用C++来做:
# coding=utf-8
import random
import string
import threading
import time
from requests import post
class MultiThread(threading.Thread):
def __init__(self, url, qlock):
threading.Thread.__init__(self)
self.url = url
self.qlock = qlock
def run(self):
send_requests(self.url, self.qlock)
def send_requests(url, qlock):
"""
:param url: http://xxx
:param qlock: 线程锁
:return:
"""
qlock.acquire()
nums = ‘0123456789‘
try:
json = {
‘longitude‘: ‘‘.join(random.sample(nums + string.digits, 3)) + ‘.‘ + ‘‘.join(random.sample(nums + string.digits, 10)),
‘latitude‘: ‘‘.join(random.sample(nums + string.digits, 2)) + ‘.‘ + ‘‘.join(random.sample(nums + string.digits, 10)),
‘subTime‘: time.strftime(‘%Y-%m-%d %H:%M:%S‘, time.localtime()),
‘machineId‘: ‘‘.join(random.sample(string.ascii_letters + string.digits, 6))
}
r = post(url, json=json, timeout=1)
print r.status_code
# print r.text
finally:
qlock.release()
def run_thread(url, concurrency):
"""
:param url: http://xxx
:param concurrency: 并发数
:return:
"""
lock = threading.Lock()
threads = []
for cncu in range(1, concurrency):
t = MultiThread(url, lock)
t.daemon = True
t.start()
threads.append(t)
for t in threads:
t.join()
if __name__ == ‘__main__‘:
url = ‘http://xxx‘
for i in range(0, 1000000):
run_thread(url, 10)
其实我不太明白如果用C++来做在做压力测试会更好吗?虽然众所周知python的多线程是假的(GIL锁),不管开了多少个线程,实际上也只有1个线程在跑。。。C++多线程当然性能更好,但我个人总觉得做压力测试貌似用不上C++?
个人对这两门语言的认识是这样的:python是解释型语言,所以每次运行都需要解释器边解释边运行(我记得python为了解决这个问题 --- 提高性能,所以会生成一些配置文件,如果代码没有改动,那么就按上次运行的过程执行 --- 貌似是这样 = =);而C/C++通过编译器生成的可执行文件在运行时可能会需要调用一些动态链接库(dll),但不需要每次运行都编译一遍,所以性能上是优秀的。
这一块完全是自己的知识盲区(学习深度还是不够),如果有很懂的同学,还望能不吝教指!
标签:学习 ext oca random odi 多少 ini daemon long
原文地址:https://www.cnblogs.com/darkchii/p/9026740.html