发出多个请求以从CherryPy Web服务获取数据

时间:2014-10-30 12:09:53

标签: python networking cherrypy

我创建了一个CherryPy Web服务,它将数据存储在地图中,从客户端接收密钥并返回相应的数据:

import sys
import imp
import cherrypy

data_source = get_data() # get data from the database and store it in the map

class Provider:
    exposed = True

    def POST(self, key):
        global data_source
        data = data_source[key] # get stored data based on given key
        return data

if __name__ == '__main__':
    cherrypy.tree.mount(Provider(), '/Provider',{'/':
            {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
        })
    cherrypy.config.update({'server.socket_host': '0.0.0.0',
            'server.socket_port': 8080,
        })
    cherrypy.server.max_request_body_size = 1048576000
    cherrypy.engine.start()
    cherrypy.engine.block()

然后,在另一台机器上,我创建了一个脚本来向提供者请求数据。使用该脚本,可以指定我想要生成的并发请求数:

import requests
import time
from threading import Thread

def make_request(id, key):
    start = time.time()
    r = requests.post("http://provider-host/Provider", {'key':key})
    end = time.time()
    print 'Thread {0} takes {1} seconds to finish with status code {2}'.format(id, end - start, r.status_code)

def start(num, key):
    ts = []
    for i in range(num):
        t = Thread(target=make_request, args=(i, key))
        ts.append(t)
    for t in ts: t.start()
    for t in ts: t.join()

最后,我做了一个测试,用两种不同的方法请求相同的密钥10次:顺序和并发。

顺序方法:

time for i in range(10): start(1, 'big_data_key')

结果是:

Thread 0 takes 2.51558494568 seconds to finish with status code 200
Thread 0 takes 2.47761011124 seconds to finish with status code 200
Thread 0 takes 2.66229009628 seconds to finish with status code 200
Thread 0 takes 2.47381901741 seconds to finish with status code 200
Thread 0 takes 2.4907720089 seconds to finish with status code 200
Thread 0 takes 2.93357181549 seconds to finish with status code 200
Thread 0 takes 2.47671484947 seconds to finish with status code 200
Thread 0 takes 2.40888786316 seconds to finish with status code 200
Thread 0 takes 2.6319899559 seconds to finish with status code 200
Thread 0 takes 2.77075099945 seconds to finish with status code 200
CPU times: user 1.79 s, sys: 1.06 s, total: 2.85 s
Wall time: 25.9 s

并发方法:

time start('138.251.195.251', 10, 'big_data_key')

结果是:

Thread 5 takes 15.5736939907 seconds to finish with status code 200
Thread 1 takes 19.4057281017 seconds to finish with status code 200
Thread 7 takes 21.4743158817 seconds to finish with status code 200
Thread 8 takes 22.4408829212 seconds to finish with status code 200
Thread 0 takes 24.1915988922 seconds to finish with status code 200
Thread 2 takes 24.3175201416 seconds to finish with status code 200
Thread 6 takes 24.3368370533 seconds to finish with status code 200
Thread 4 takes 24.3618791103 seconds to finish with status code 200
Thread 9 takes 24.3891952038 seconds to finish with status code 200
Thread 3 takes 24.5536601543 seconds to finish with status code 200
CPU times: user 2.34 s, sys: 1.67 s, total: 4.01 s
Wall time: 24.6 s

很明显,使用并发方法,完成一个请求所需的时间高于顺序方法。

所以,我的问题是:下载时间的差异是由两台机器之间的带宽或其他因素造成的,例如:樱桃相关?如果它是由其他东西引起的,我将不胜感激任何建议来处理它。

1 个答案:

答案 0 :(得分:1)

然后很明显,你的瓶颈就是网络。 220MiB以10.8MiB / s的速度传输至少需要20秒。您的实验需要约25秒,即8.8MiB / s,即在100MBit / s的最大理论容量中有效~74Mbit / s。考虑到所有可能的测量误差,这是一个很好的结果。

串行和并行情况(~5%)之间的区别表明多路复用没有帮助,因为瓶颈是网络带宽,而不是单独的连接限制。

要测量CherryPy影响,您可以设置用本机代码编写的Web服务器,我建议 nginx ,将文件放在那里,并尝试下载10次。对于并行测试,您可以尝试Apache ab,例如ab -n 10 -c 10 http://provider-host/some-big-file

还有关于CherryPy的几点说明:

  • CherryPy是一个线程服务器,默认线程池server.thread_pool,有10个工作者,
  • 尝试在配置中激活gzip压缩'/' : {'tools.gzip.on': True},这将显着增强纯文本数据。

您还可以查看this question,了解如何使用CherryPy处理大文件下载。