python实现多线程压测post接口服务

来源:互联网 发布:数据结构设计怎么写 编辑:程序博客网 时间:2024/06/05 19:25
import urllibimport urllib2import randomimport sysimport threading,timefrom time import sleep, ctimeimport jsonimport time host_url='http://127.0.0.1:8070'def now() :    return str( time.strftime( '%Y-%m-%d %H:%M:%S' , time.localtime() ) )    def post1():   print 'start loop', 'at:', now()  start = time.clock()  uid=random.randint(0, 2000000)  url=host_url+'/bigdata/crm/getGroupResCount'  values ={"query_where": [{"item": "uid","query": [{"op": "<","value": uid}]}]}  jdata = json.dumps(values)               req = urllib2.Request(url, jdata)        req.add_header('Content-Type', 'application/json')  response = urllib2.urlopen(req)         end = time.clock()  print "run: %f s" % (end - start)  print response.read()  def post2():  print 'start loop', 'at:', now()  start = time.clock()  uid=random.randint(0, 2000000)  url=host_url+'/bigdata/crm/generateDataFile'  values ={"groupType": "dynamic","groupID": "123","query_where": [{"item": "uid","query": [{"op": "<","value": uid}]}]}  jdata = json.dumps(values)               req = urllib2.Request(url, jdata)        req.add_header('Content-Type', 'application/json')  response = urllib2.urlopen(req)     end = time.clock()  print "run: %f s" % (end - start)    print response.read()  def post():        post1()        #post2()def main():   loop=int(sys.argv[1])  ths=int(sys.argv[2])    for i in xrange(loop):    threadpool=[]    for i in xrange(ths):        th = threading.Thread(target= post)        threadpool.append(th)    for th in threadpool:        th.start()    for th in threadpool :        threading.Thread.join( th )      post() if __name__ == '__main__':   main() 

0 0
原创粉丝点击