| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 | # -*-coding:utf-8-*-import requestsimport randomimport jsonimport timeimport threadingclass MyThread(threading.Thread):    def __init__(self, func, args, name=''):        threading.Thread.__init__(self)        self.name = name        self.func = func        self.args = args    def run(self):        self.result = self.func(self.args)    def get_result(self):        try:            return self.result        except Exception:            return Noneurl = 'http://106.14.187.241:5555/elab-marketing-user//zhidi/activity/concurrent/turntableActivity'headers_2 = {'content-type': "application/json", 'Authorization': 'APP appid = 4abf1a,token = 9480295ab2e2eddb8',             'dsNo': 'source2'}def loop(nloop):    response = requests.post(url, json=nloop, headers=headers_2)    return response.text# 15def main():    result_list = []    threads = []    people = 200    times = range(people)    for i in times:        params = {            "actionType": 1,            "activityId": 8939,            "brandId": 1,            "customerId": 223222 + random.randint(5000, 9000),            "houseId": 10110,            "mobile": "15622363" + str(random.randint(1000, 9999)),            "shareActivityId": 0,            "uuid": ""        }        t = MyThread(loop, params, ' 第' + str(i + range(i + 1)[0]) + '线程')        threads.append(t)    for i in range(people):  # start threads 此处并不会执行线程,而是将任务分发到每个线程,同步线程。等同步完成后再开始执行start方法        threads[i].start()    for i in range(people):  # jion()方法等待线程完成        threads[i].join()        result = threads[i].get_result()        result_list.append(json.loads(result))    return result_listdef analysis_result(result_list):    # 抽奖结果统计    dict_data = {}    # 中奖    prize_count = 0    # 谢谢参与    think_prize = 0    # 抽奖失败    failed_prize = 0    for x in result_list:        success = x['success']        if success is True:            # 抽奖成功,统计具体的抽奖情况            single = x['single']            name = single['name']            if name in dict_data.keys():                count = dict_data[name]                dict_data[name] = count + 1            else:                dict_data[name] = 1            thinks = single['thinks']            if str(thinks) == str(1):                think_prize += 1            else:                prize_count += 1        else:            # 抽奖失败统计            failed_prize += 1    all = prize_count + think_prize    for key in dict_data.keys():        count = dict_data[key]        if all > 0:            print(key + ':   ' + str(count), ",占比: " + str(count/all))        else:            print(key + ':   ' + str(count))    print('------------,参与抽奖人数:' + str(all))    print("中奖:" + str(prize_count), ",中奖率:" + str(prize_count / all))    print("谢谢参与; " + str(think_prize), ",谢谢参与率: " + str(think_prize / all))    # print("抽奖失败:" + str(failed_prize), ",抽奖失败率; " + str(failed_prize / len(result_list)))if __name__ == '__main__':    result = main()    analysis_result(result)
 |