import time, json
import urllib.request
import matplotlib.pyplot as plt
import numpy
from multiprocessing import Process, Manager
url = "https://service-px5f98f4-1256773370.gz.apigw.tencentcs.com/release/scf_demo"
times = 200
serialColdStart = []
serialHotStart = []
for i in range(0,times):
timeStart = time.time()
responseAttr = urllib.request.urlopen(url)
endTime = time.time()
response = json.loads(responseAttr.read().decode("utf-8"))
if response['isNew']:
serialColdStart.append(endTime-timeStart)
else:
serialHotStart.append(endTime-timeStart)
def worker(url, return_list):
timeStart = time.time()
responseAttr = urllib.request.urlopen(url)
endTime = time.time()
return_list.append({
"duration": endTime-timeStart,
"response": json.loads(responseAttr.read().decode("utf-8"))
})
manager = Manager()
return_list = manager.list()
jobs = []
for i in range(times):
p = Process(target=worker, args=(url ,return_list))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
parallelColdStart = []
parallelHotStart = []
for eveData in return_list:
if eveData['response']['isNew']:
parallelColdStart.append(eveData['duration'])
else:
parallelHotStart.append(eveData['duration'])
print("-"*10, "串行测试", "-"*10)
print("总触发次数:", len(serialColdStart) + len(serialHotStart))
print("冷启动次数:", len(serialColdStart))
print("热启动次数:", len(serialHotStart))
print("最大耗时量:", max(serialColdStart + serialHotStart))
print("最小耗时量:", min(serialColdStart + serialHotStart))
print("平均耗时量:", numpy.mean(serialColdStart + serialHotStart))
print("-"*10, "并行测试", "-"*10)
print("总触发次数:", len(parallelColdStart) + len(parallelHotStart))
print("冷启动次数:", len(parallelColdStart))
print("热启动次数:", len(parallelHotStart))
print("最大耗时量:", max(parallelColdStart + parallelHotStart))
print("最小耗时量:", min(parallelColdStart + parallelHotStart))
print("平均耗时量:", numpy.mean(parallelColdStart + parallelHotStart))
plt.figure(figsize=(15,10))
plt.subplot(4, 2, 1)
plt.title('(Serial) Cold Start Time')
plt.plot(range(0, len(serialColdStart)), serialColdStart)
plt.subplot(4, 2, 3)
plt.title('(Serial) Cold Start Time')
plt.hist(serialColdStart, bins=20)
plt.subplot(4, 2, 5)
plt.title('(Serial) Hot Start Time')
plt.plot(range(0, len(serialHotStart)), serialHotStart)
plt.subplot(4, 2, 7)
plt.title('(Serial) Hot Start Time')
plt.hist(serialHotStart, bins=20)
plt.subplot(4, 2, 2)
plt.title('(Parallel) Hot Start Time')
plt.plot(range(0, len(parallelColdStart)), parallelColdStart)
plt.subplot(4, 2, 4)
plt.title('(Parallel) Cold Start Time')
plt.hist(parallelColdStart, bins=20)
plt.subplot(4, 2, 6)
plt.title('(Parallel) Hot Start Time')
plt.plot(range(0, len(parallelHotStart)), parallelHotStart)
plt.subplot(4, 2, 8)
plt.title('(Parallel) Hot Start Time')
plt.hist(parallelHotStart, bins=20)
plt.show()import time, json
import urllib.request
import matplotlib.pyplot as plt
import numpy
from multiprocessing import Process, Manager
url = "https://service-px5f98f4-1256773370.gz.apigw.tencentcs.com/release/scf_demo"
times = 200
serialColdStart = []
serialHotStart = []
for i in range(0,times):
timeStart = time.time()
responseAttr = urllib.request.urlopen(url)
endTime = time.time()
response = json.loads(responseAttr.read().decode("utf-8"))
if response['isNew']:
serialColdStart.append(endTime-timeStart)
else:
serialHotStart.append(endTime-timeStart)
def worker(url, return_list):
timeStart = time.time()
responseAttr = urllib.request.urlopen(url)
endTime = time.time()
return_list.append({
"duration": endTime-timeStart,
"response": json.loads(responseAttr.read().decode("utf-8"))
})
manager = Manager()
return_list = manager.list()
jobs = []
for i in range(times):
p = Process(target=worker, args=(url ,return_list))
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
parallelColdStart = []
parallelHotStart = []
for eveData in return_list:
if eveData['response']['isNew']:
parallelColdStart.append(eveData['duration'])
else:
parallelHotStart.append(eveData['duration'])
print("-"*10, "串行测试", "-"*10)
print("总触发次数:", len(serialColdStart) + len(serialHotStart))
print("冷启动次数:", len(serialColdStart))
print("热启动次数:", len(serialHotStart))
print("最大耗时量:", max(serialColdStart + serialHotStart))
print("最小耗时量:", min(serialColdStart + serialHotStart))
print("平均耗时量:", numpy.mean(serialColdStart + serialHotStart))
print("-"*10, "并行测试", "-"*10)
print("总触发次数:", len(parallelColdStart) + len(parallelHotStart))
print("冷启动次数:", len(parallelColdStart))
print("热启动次数:", len(parallelHotStart))
print("最大耗时量:", max(parallelColdStart + parallelHotStart))
print("最小耗时量:", min(parallelColdStart + parallelHotStart))
print("平均耗时量:", numpy.mean(parallelColdStart + parallelHotStart))
plt.figure(figsize=(15,10))
plt.subplot(4, 2, 1)
plt.title('(Serial) Cold Start Time')
plt.plot(range(0, len(serialColdStart)), serialColdStart)
plt.subplot(4, 2, 3)
plt.title('(Serial) Cold Start Time')
plt.hist(serialColdStart, bins=20)
plt.subplot(4, 2, 5)
plt.title('(Serial) Hot Start Time')
plt.plot(range(0, len(serialHotStart)), serialHotStart)
plt.subplot(4, 2, 7)
plt.title('(Serial) Hot Start Time')
plt.hist(serialHotStart, bins=20)
plt.subplot(4, 2, 2)
plt.title('(Parallel) Hot Start Time')
plt.plot(range(0, len(parallelColdStart)), parallelColdStart)
plt.subplot(4, 2, 4)
plt.title('(Parallel) Cold Start Time')
plt.hist(parallelColdStart, bins=20)
plt.subplot(4, 2, 6)
plt.title('(Parallel) Hot Start Time')
plt.plot(range(0, len(parallelHotStart)), parallelHotStart)
plt.subplot(4, 2, 8)
plt.title('(Parallel) Hot Start Time')
plt.hist(parallelHotStart, bins=20)
plt.show()
评论 (1 条评论)