oj gem这个测试用例非常简单,仅仅将所有的article序列化为JSON格式: 1 | class SidechannelsController < ApplicationController |
3 | render json: Article.all |
使用标准Rails序列化器: 1 | Running 10s test @ http://localhost:3000/sidechannels/bench |
2 | 2 threads and 10 connections |
3 | Thread Stats Avg Stdev Max +/- Stdev |
4 | Latency 108.37ms 5.12ms 134.90ms 83.33% |
5 | Req/Sec 45.76 3.60 55.00 57.69% |
6 | 922 requests in 10.00s, 57.41MB read |
使用oj gem: 1 | Running 10s test @ http://localhost:3000/sidechannels/bench |
2 | 2 threads and 10 connections |
3 | Thread Stats Avg Stdev Max +/- Stdev |
4 | Latency 78.06ms 4.43ms 92.83ms 81.31% |
5 | Req/Sec 63.64 5.33 71.00 64.49% |
6 | 1277 requests in 10.00s, 79.83MB read |
使用jemalloc 好吧,这其实不是一个真正的gem,如果你想深入探究它,可以看我的这篇文章。在初始测试时,jemalloc并没有产生太多性能的提升,至少对我使用的测试用例是这样的。 提示:某些情况下,可能会默认包含在Ruby中。 更新:请一定尝试一下kzk的jemalloc gem: 深入探究你的Rails应用 不要担心,去用一下Sam Saffron的带有非常棒的FlameGraphs的MiniProfiler吧!
结语 于你的应用要做什么,你可能想为你的Gemfile添加上述的一些gem。通常我会把他们都添加上,当然是出于一个好的估量(你可能会想检查你的RAM利用率,然后在添加之前,进行一个完整的测试)。 oj gem基于JSON API,对Rails来说是非常不错的,使用oj gem,你可以删除视图并仅使用代言人或者你选择的模式进行序列化。 |