设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

通过添加一些gems来提升Rails应用的性能

2014-7-4 11:26| 发布者: joejoe0332| 查看: 2938| 评论: 0|原作者: 0x0bject|来自: oschina

摘要: 使用Rails一段时间之后,你可能就会开始吹毛求疵的想要提高它性能。这是一系列文章中第一次考虑如何提高(即使微不足道的)Rails的性能。

  使用Rails一段时间之后,你可能就会开始吹毛求疵的想要提高它性能。这是一系列文章中第一次考虑如何提高(即使微不足道的)Rails的性能。


  我将会关注在一些gem的提速上面,在某些情况下,可能是一小部分的Rails,如html转义,String.blank?和JSON工具类。


  基准原则


  原则,对于仅仅在控制台wrk运行几次来讲,是一个与其过强的词语,但是我这里不是来寻找“圣杯”的,而是提供一些初始的想法。


  我将从旧的apache ab切换到wrk

wrk是现代的 HTTP 基准工具,当在一个单一的多核 CPU 上运行时,能够产生巨大的负载。
1wrk -t10 -c10 -d10s http://localhost:3000

这条指令运行基准问题10s,使用10个线程,并且保持打开50个HTTP链接,也就是说,这样就足够了。记得将这些基准测试在你实际的应用中跑一下,看一下实际上的性能提高有多少。

escape_utils gem

通过可爱的escape_utils gem可以加快HTML的转义。为了使其能够在Rails中使用,需要添加一个初始值设定来解决:

1begin
2  require 'escape_utils/html/rack' # to patch Rack::Utils
3  require 'escape_utils/html/erb' # to patch ERB::Util
4  require 'escape_utils/html/cgi' # to patch CGI
5  require 'escape_utils/html/haml' # to patch Haml::Helpers
6rescue LoadError
7  Rails.logger.info 'Escape_utils is not in the gemfile'
8end

对该逻辑进行测试的用例:

01def escape_utils
02  @escape_me = <<-HTML
03    <body class="application articles_show">
04      <!-- Responsive navigation
05      ==================================================== -->
06      <div class="container">
07        <nav id="nav">
08      <ul>
09        <li><a href="/"><i class="ss-standard ss-home"></i>home</a></li>
10        <li><a href="/home/about"><i class="ss-standard ss-info"></i>about</a></li>
11        <li><a href="/contact"><i class="ss-standard ss-ellipsischat"></i>contact</a></li>
12        <li><a href="/home/projects"><i class="ss-standard ss-fork"></i>projects</a></li>
13        <li><a href="/tags"><i class="ss-standard ss-tag"></i>tags</a></li>
14        <li><a href="/articles?query=code"><i class="ss-standard ss-search"></i>search</a></li>
15      </ul>
16    </nav>
17    <a href="#" class="ss-standard ss-list" id="nav-toggle" aria-hidden="true"></a>
18  HTML
19 
20  render inline: "Hello  world <%= @escape_me %>"
21end

使用标准Rails:

1Running 10s test @ http://localhost:3000/sidechannels/bench
2  2 threads and 10 connections
3  Thread Stats   Avg      Stdev     Max   +/- Stdev
4    Latency    35.40ms    3.55ms  64.70ms   91.98%
5    Req/Sec   142.19     11.68   164.00     83.12%
6  2837 requests in 10.00s, 4.92MB read
7Requests/sec:    283.61
8Transfer/sec:    503.34KB

使用escape_utils gem:

1Running 10s test @ http://localhost:3000/sidechannels/bench
2  2 threads and 10 connections
3  Thread Stats   Avg      Stdev     Max   +/- Stdev
4    Latency    34.06ms    3.89ms  63.92ms   89.10%
5    Req/Sec   148.65     13.36   180.00     75.94%
6  2960 requests in 10.00s, 5.46MB read
7Requests/sec:    295.98
8Transfer/sec:    558.72KB

fast_blank gem

是否在印象里,blank?方法太慢?不用多说,试一下fast_blank gem!

仅需要在你的Gemfile中添加gem 'fast_blank',这应该就可以非常漂亮的提高像这篇文章中提到的String.black?方法的速度。为了测试,我仅添加下俩代码:

fast_blank是一个简单的扩展,提供了一个支持String.blank?功能的快速实现。
01 def fast_blank_test
02    n = 1000
03 
04    strings = [
05      "",
06      "\r\n\r\n  ",
07      "this is a test",
08      "   this is a longer test",
09      "   this is a longer test
10      this is a longer test
11      this is a longer test
12      this is a longer test
13      this is a longer test"
14    ]
15 
16    Benchmark.bmbm  do |x|
17      strings.each do |s|
18        x.report("Fast Blank #{s.length}    :"do
19          n.times { s.blank? }
20        end
21      end
22    end
23 
24    render nothing: true
25  end

使用标准Rails:

1Running 10s test @ http://localhost:3000/sidechannels/bench
2  2 threads and 10 connections
3  Thread Stats   Avg      Stdev     Max   +/- Stdev
4    Latency     1.40s   207.72ms   1.58s    92.68%
5    Req/Sec     3.10      2.11     6.00     53.66%
6  69 requests in 10.01s, 33.08KB read
7Requests/sec:      6.90
8Transfer/sec:      3.31KB

使用fast_blank gem:

1Running 10s test @ http://localhost:3000/sidechannels/bench
2  2 threads and 10 connections
3  Thread Stats   Avg      Stdev     Max   +/- Stdev
4    Latency     1.33s   179.56ms   1.41s    93.33%
5    Req/Sec     3.07      0.80     4.00     40.00%
6  72 requests in 10.00s, 34.52KB read
7Requests/sec:      7.20
8Transfer/sec:      3.45KB




oj gem

1# oj gem
2gem 'oj'
3gem 'oj_mimic_json' # we need this for Rails 4.1.x

这个测试用例非常简单,仅仅将所有的article序列化为JSON格式:

1class SidechannelsController < ApplicationController
2  def oj
3    render json: Article.all
4  end
5end

使用标准Rails序列化器:

1Running 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
7Requests/sec:     92.17
8Transfer/sec:      5.74MB

使用oj gem:

1Running 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
7Requests/sec:    127.65
8Transfer/sec:      7.98MB


  使用jemalloc

  好吧,这其实不是一个真正的gem,如果你想深入探究它,可以看我的这篇文章。在初始测试时,jemalloc并没有产生太多性能的提升,至少对我使用的测试用例是这样的。

  提示:某些情况下,可能会默认包含在Ruby中。

  更新:请一定尝试一下kzk的jemalloc gem

1gem install jemalloc
2 
3je -v rails s

深入探究你的Rails应用

不要担心,去用一下Sam Saffron的带有非常棒的FlameGraphsMiniProfiler吧!


  结语

  于你的应用要做什么,你可能想为你的Gemfile添加上述的一些gem。通常我会把他们都添加上,当然是出于一个好的估量(你可能会想检查你的RAM利用率,然后在添加之前,进行一个完整的测试)。

oj gem基于JSON API,对Rails来说是非常不错的,使用oj gem,你可以删除视图并仅使用代言人或者你选择的模式进行序列化。


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部