456 points · by c17r · 10y ago · 130 comments
This is exactly the same observation that motivated the Mongrel web server for Ruby, 10 years ago this year.
"Mongrel is a small library that provides a very fast HTTP 1.1 server for Ruby web applications. [...] What makes Mongrel so fast is the careful use of an Ragel [C] extension to provide fast, accurate HTTP 1.1 protocol parsing. This makes the server scream without too many portability issues." -- https://github.com/mongrel/mongrel
And its successor Thin:
"Thin is a Ruby web server that glues together 3 of the best Ruby libraries in web history: (1) the Mongrel parser, the root of Mongrel speed and security" -- http://code.macournoyer.com/thin/
In case anyone is still wondering, parsing in Ruby/Python/Lua is pretty slow compared to C/C++. That's why I personally have been really interested for a long time in writing parsers in C that can be used from higher level languages. That way you can get the best of both worlds.
He goes into detail about how these types of libraries are particularly difficult or impossible to fuzz. He uses OpenSSL as an example but I would imagine an http library being similar.
[0] https://github.com/aspnet/KestrelHttpServer
[1] https://github.com/aspnet/KestrelHttpServer/blob/3a424f6abac...
[2] https://github.com/aspnet/KestrelHttpServer/blob/dev/src/Mic...
[3] https://github.com/aspnet/KestrelHttpServer/blob/dev/src/Mic...
1. https://uwsgi-docs.readthedocs.io/en/latest/Protocol.html
This uses Dahl's original http_parser.c FSM. With a little work you can write a WSGI handler around it. Highly recommend.
† On a related note, I'm finding it difficult finding (fast) GLR or GLL parser generators for Ruby
I'm working on what I consider a parsing stack. The key observation, in my opinion, is that you need a way of representing structured data in the high-level language that is both rich and efficient. I think Protocol Buffers can serve that role.
Once you have the structured data representation, you can write parsers to read/write it. But you want the parsers to be independent of how you represent the structured data in any one language.
http://curio.readthedocs.io/en/latest/
The caveat is that it uses the new async/await coroutine bits that just landed in Python 3.5, so it only works with Python 3.5+. He also gave a talk on concurrency in python recently at last year's PyCon:
Compare
https://github.com/MagicStack/vmbench/blob/master/servers/as...
to
https://github.com/nodejs/node/blob/master/lib/_http_outgoin...
https://github.com/nodejs/node/blob/master/lib/_http_server....
https://github.com/golang/go/blob/master/src/net/http/server...
The benchmark is almost equivalent to testing raw eventloop performance against a complete http server.
To make this test fair, you need to write the same code in asyncio_http_server.py in other languages.
I plan to add a complete HTTP protocol implementation to httptools, but honestly, I don't expect it to be more than 20% slower.
How so? It uses a binding to http-parser, just like nodejs.
These benchmarks seem to only use 10 clients concurrently, max. That's ridiculously low.
A few questions I'd like to see answered.
How many clients can you connect to each server, and have each one ping once per 5 mins, before the CPU gets overloaded?
How much memory is used per connection?
How does PyPy+Twisted fair in this?
I've just update the post with more details on HTTP benchmarks and attached the correct full-results file [1]. The concurrency level for HTTP benchmarks is 300, not 10.
To answer other questions, I'll have to run some benchmarks tomorrow :)
[1] http://magic.io/blog/uvloop-blazing-fast-python-networking/h...
In my experience, PyPy+twisted is around 5-25x faster CPython/twisted, and smoked asyncio as well. Would be great to see how uvloop compares there, and of course, someday when PyPy supports Python 3.5, there's no reason it couldn't use uvloop via cffi I'd hope.
Yep, I'm curious to see what will happen there. Do you have any suggestions on what tool to use to generate the load?
> [..] someday when PyPy supports Python 3.5, there's no reason it couldn't use uvloop via cffi I'd hope.
We'll figure that out! ;)
2) Can uvloop be used with frameworks like flask or django?
3) gevent uses monkey patching to turn blocking libraries such as DB drivers non-blocking, does uvloop do anything similar? If not how does it work with blocking libraries?
2) No, they have a different architecture. Although I heard that there is a project to integrate asyncio into django to get websockets and http/2.
3) asyncio/uvloop require you to use explicit async/await. So, unfortunately, the existing networking code that isn't built for asyncio can't be reused. On the bright side, there are so many asyncio DB drivers and other modules now!
Having a web framework (a next generation Flask if you will) built ground up with concurrency is the missing key. I have used Flask for many years, but this is the right time to introduce a new framework - because of the internal restructuring and slowdown of Flask's maintainers (and I say this with the utmost respect).
If you are keen, this has the potential to be the killer application for Python 3.
Or maybe it's a simple problem of aiohttp performance -- as shown in the blog post, its HTTP parser is a bit slow.
In general, I'd recommend to use a fewer number of sockets and implement some pipelining of API requests.
Not long ago I saw example here that someone was creating a new session for every single connection. This is not very optimal way of using it. If you use it within same session, aiohttp will make use of keep-alive, which in turn will reuse existing connections and reduce overhead. You also won't need to use a semaphore, since you can define limit in TCPConnector.
Why you had performance issues? As other said, you were making thousands of connections, each socket need to be in TIME_WAIT state for 2 minutes after closing (limitation of TCP, SCTP does not have this problem). So if you use all connections within short time, you'll essentially run out of them. Some people use tcp_tw_reuse/recycle, and that solves this issue, but that makes your connections no longer follow RFC and you might encounter strange issues later on. The advice above should resolve your problem without any hacks.
I did not see any benchmarks in the repo to support this. How was this statistic determined?
[1] https://github.com/MagicStack/vmbench [2] http://magic.io/blog/uvloop-blazing-fast-python-networking/t...
http://magic.io/blog/uvloop-blazing-fast-python-networking/t...
Building something in C is very hard. uvloop wraps all libuv primitives in Python objects which know how to manage the memory safely (i.e. not to "free" something before libuv is done with it). So development time wise, uvloop is much better.
As for the performance, I guess you'd be able to squeeze another 5-15% if you write the echo server in C.
> I'm not surprised that python (especially cython) is faster than node in this case
Cython is a statically typed compiled language, it can be anywhere from 2x to 100x faster than CPython.
uvloop shouldn't behave any differently, I've paid special attention to make sure it works exactly the same way as asyncio (down to when its objects are garbage collected).
Work on Trollius was stopped a few weeks ago, there wasn't enough interest/use. Call for interested maintainers, http://trollius.readthedocs.io/deprecated.html#deprecated
Not very, it's an alternative event loop for asyncio[0] which was introduced in 3.4 and builds upon other Python 3 features (e.g. `yield from`)
For example you can install python 2.6, 2.7, 3.3, 3.4, & 3.5 all on one host without any conflicts. The limitation is that many distributions prefer to not maintain different versions of supposedly the same language.
If you use RedHat or CentOS you can just use https://ius.io/ and get access to the other python versions. This is one of few repos that makes sure the packages don't conflict with system ones.
How does it compare to PyPy?
Once PyPy3 is available, will this work with it?
We'll find a way!
[1] http://magic.io/blog/uvloop-blazing-fast-python-networking/h...
1. The benchmarks make servers generate a huge number of objects, so maybe, the GC is under too much pressure.
2. Another possibility is that the v8 JIT can't optimize some JS code in nodejs, or does a poor job.
That said, only careful profiling can answer your question :)
socket.pipe(socket);How so? Clustering for node just makes it run in several OS processes. You can (and should) do the same for Python code, it's easy.
I'm surprise uvloop is faster than node.js since libuv was developed for the latter. Kudos to the author.
Sure, but it really depends on how complex your Python code is.
> I'm surprise uvloop is faster than node.js since libuv was developed for the latter. Kudos to the author.
Thanks!
When I see numbers like 50k requests per second it's meaningless, unless I have no database or some kind of in memory cache only db.
That said, uvloop should be fully compatible with asyncio. All APIs should be ready, so you can start testing it in your projects.
uvloop, as of now, only runs on *nix; but I hope we'll have Windows supported soon too.
What exactly is the -streams addition that makes uvloop-streams perform so much worse than plain uvloop?
Specifically, the http server example(1), doesn't even bother using the standard library provided Cluster module(2). Cluster is specifically designed for distributing server workloads across multiple cores.
All node.js services/applications I've worked on in the past 3 years (that are concerned with scale) utilize a multi-process node architecture.
The current benchmark can only claim that a single python process that spawns multiple threads is 2x faster than a single node.js process that spawns only one thread.
This fact may be interesting to some, but is irrelevant to real world performance.
[1]: https://github.com/MagicStack/vmbench/blob/master/servers/no...
Yes, in production you should run your nodejs app in cluster, your Python apps in a multiprocess configuration, and you should never use GOMAXPROCS=1 for your go apps in production!
Running all benchmarks in multiprocess configuration wouldn't add anything new to the results.
The comment above (https://news.ycombinator.com/item?id=11626762) further expands on why these kinds of benchmarks, although interesting, have no real value.
Each implementation does something wildly different and responds to different inputs with completely different outputs.
To put it metaphorically, if you put a car engine in two completely different chassis and then race them on a track, you aren't gaining any real insight into relative performance of the engine in the two vehicles.
Also, just to be clear, my qualms are with the benchmarks alone, I think the library is great! Thanks for all the hard work :)
These benchmarks are primarily comparing event loops and their performance. TCP benchmark is very fair, HTTP - maybe not so much. The point is to show that you can write super fast servers in Python too, just have a fast protocol parser.
As for the HTTP benchmarks, I plan to add more stuff to httptools and implement a complete HTTP protocol in it. Will rerun the benchmakrs, but I don't expect more than 20% performance drop.
Yes it's true you normally run multiple node processes in production, but you likewise normally run multiple asyncio/tornado/twisted processes in production as well. I don't see it as a big deal, or misleading to compare them in this sense.
It doesn't matter anyway, with one thread per core it would be pretty straightforward to scale in beefier machines.
> We use Python 3.5, and all servers are single-threaded. Additionally, we use GOMAXPROCS=1 for Go code, nodejs does not use cluster, and all Python servers are single-process.