我正在对我们在整个公司使用的负载测试框架进行一些修改,这是一个我很想得到答案的问题。
我的印象是以下两种生成泊松分布的方法是等效的,但我显然错了:
#!/usr/bin/env python
from numpy import average, random, std
from random import expovariate
def main():
for count in 5.0, 50.0:
data = [random.poisson(count) for i in range(10000)]
print 'npy_poisson average with count=%d: ' % count, average(data)
print 'npy_poisson std_dev with count=%d: ' % count, std(data)
rate = 1 / count
data = [expovariate(rate) for i in range(10000)]
print 'expovariate average with count=%d: ' % count, average(data)
print 'expovariate std_dev with count=%d: ' % count, std(data)
if __name__ == '__main__':
main()
这导致输出如下所示:
npy_poisson average with count=5: 5.0168
npy_poisson std_dev with count=5: 2.23685443424
expovariate average with count=5: 4.94383067075
expovariate std_dev with count=5: 4.95058985422
npy_poisson average with count=50: 49.9584
npy_poisson std_dev with count=50: 7.07829565927
expovariate average with count=50: 50.9617389096
expovariate std_dev with count=50: 51.6823970228
为什么当我使用内置 random.expovariate 时标准偏差与给定间隔内的事件数量成比例,而 expovariate std_deviation 以对数基数 10(计数)的速率进行缩放?
后续问题:如果您要模拟用户与您的服务交互的频率,哪一个更合适?