Speeding Up the Web

Web frameworks have been a stronghold for dynamic languages like Ruby, Python, and PHP. How does Crystal compare?

The Crystal standard library web server is itself very performant. In a test comparing web servers implemented in Node.js, Nim, Rust, and Scala, the Crystal HTTP::server handled more requests per second[14] than any other server. Tests come and go, but Crystal continues to do well despite being limited to one processor for now.

What about more fully fledged web frameworks? Right now, one framework in Crystal stands out: Kemal. This small and flexible web framework supports a RESTful interface, very much like Sinatra in Ruby. For a more complete discussion, see Build Web Applications with the Kemal and Amber Frameworks. Kemal and Sinatra were compared using wrk, a modern HTTP benchmarking tool for tests using a single core and a web form of the Hello-world app. Here’s the Crystal source code:

 require ​"kemal"
 
 get ​"/"​ ​do
 "Hello from Kemal!"
 end
 
 Kemal.​run

Servers built on both frameworks were tested with 100 connections for a duration of 30 seconds:

$ wrk -c 100 -d 30 http://localhost:3000

The results are shown in the figure—the y-axis shows the number of processed requests per second.

Another similar benchmark showed Kemal handling up to 64986 requests per second with an average response time of 170μs per request, while Sinatra answered 2274 requests per second with an average time of 43.82ms. Both tests show that Kemal processes around 28x more requests per second than Sinatra. (This is from Serdar Dogruyol’s talk at the Poly Conf 2016 conference: “Kemal: Building lightning fast web applications with simplicity.”)

images/why_crystal/Kemal_versus_Sinatra.png

(Higher is better: left = with logging, right = without logging).

What about web frameworks in other languages? Taichiro Suzuki made a similar kind of benchmark[15] in March 2017 by comparing Sinatra, Roda, and Rails (all in Ruby), and Echo (written in Go) with Kemal and router_cr (both Crystal). This showed that Kemal and router_cr both outperform all other frameworks. Serdar Dogruyol made the following benchmark:[16]

images/why_crystal/kemal_benchmark.png

(Higher is better: Sinatra/Rails in Ruby, Phoenix in Elixir, Martini/Gin in Go.)

Considering these results, we can safely conclude that Crystal is an excellent performance choice for web servers and web frameworks.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.69.163