python高性能web框架——Japronto

  近期做了一个简单的demo需求,搭建一个http server,支持简单的qa查询。库中有10000个qa对,需要支持每秒10000次以上的查询请求。

  需求比较简单,主要难点就是10000+的RPS。首先使用python + uwsgi写了个简单的demo,压测后发现,RPS只有几千,达不到性能要求。后来部署了多个服务,使用nginx做负载均衡才勉强达到需求。

Japronto

  后来经过google 搜索,发现了Japronto,github地址https://github.com/squeaky-pl/japronto,性能非常强悍,可以看下作者提供的性能图:

  为什么可以有这么高的性能,因为Japronto 做了大量优化,其中最主要的是HTTP pipelining,Japronto 用它来做执行并发请求的优化。大多数服务器把来自客户端的pipelining和non-pipelining请求都一视同仁,用同样的方法处理,并没有做针对性的优化。

  其他细节可以参考 https://medium.freecodecamp.org/million-requests-per-second-with-python-95c137af319 和 https://github.com/squeaky-pl/japronto

测试

  采用docker的方式进行部署的,按照官网的例子

  1、拉取镜像

  docker pull japronto/japronto

  2、编写测试代码

# examples/1_hello/hello.py
from japronto import Application


# Views handle logic, take request as a parameter and
# returns Response object back to the client
def hello(request):
    return request.Response(text='Hello world!')


# The Application instance is a fundamental concept.
# It is a parent to all the resources and all the settings
# can be tweaked here.
app = Application()

# The Router instance lets you register your handlers and execute
# them depending on the url path and methods
app.router.add_route('/', hello)

# Finally start our server and handle requests until termination is
# requested. Enabling debug lets you see request logs and stack traces.
app.run(debug=True)

   3、启动docker 容器

  docker run -p 8080:8080 -v $(pwd)/hello.py:/hello.py japronto/japronto --script /hello.py

  

  使用wrk进行压测,使用 单线程,100个连接,压测30s。结果如下

wrk -c 100 -t 1 -d 30s http://192.168.86.10:8077/

Running 30s test @ http://192.168.86.10:8077/
  1 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency     1.88ms  548.76us  17.70ms   88.46%
    Req/Sec    53.43k     2.40k   54.86k    96.33%
  1593994 requests in 30.02s, 139.85MB read
Requests/sec:  53104.58
Transfer/sec:      4.66MB

  

 压测结果受服务器,运行方式等影响,虽然和给出的数据相差较大,但是性能也是非常强悍的。

不过比较遗憾的是,目前这个项目已经暂停更新了

猜你喜欢

转载自www.cnblogs.com/lilinwei340/p/9565126.html