成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

通過添加一些gems來提升Rails應用的性能

大數據
使用Rails一段時間之后,你可能就會開始吹毛求疵的想要提高它性能。這是一系列文章中第一次考慮如何提高(即使微不足道的)Rails的性能。

使用Rails一段時間之后,你可能就會開始吹毛求疵的想要提高它性能。這是一系列文章中第一次考慮如何提高(即使微不足道的)Rails的性能。

我將會關注在一些gem的提速上面,在某些情況下,可能是一小部分的Rails,如html轉義,String.blank?和JSON工具類。

基準原則

原則,對于僅僅在控制臺wrk運行幾次來講,是一個與其過強的詞語,但是我這里不是來尋找“圣杯”的,而是提供一些初始的想法。

我將從舊的apache ab切換到wrk

wrk是現代的 HTTP 基準工具,當在一個單一的多核 CPU 上運行時,能夠產生巨大的負載。

  1. wrk -t10 -c10 -d10s http://localhost:3000 

這條指令運行基準問題10s,使用10個線程,并且保持打開50個HTTP鏈接,也就是說,這樣就足夠了。記得將這些基準測試在你實際的應用中跑一下,看一下實際上的性能提高有多少。

escape_utils gem

通過可愛的escape_utils gem可以加快HTML的轉義。為了使其能夠在Rails中使用,需要添加一個初始值設定來解決:

  1. begin  
  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  
  6. rescue LoadError  
  7.   Rails.logger.info 'Escape_utils is not in the gemfile' 
  8. end 

對該邏輯進行測試的用例:

  1. def escape_utils  
  2.   @escape_me = <<-HTML  
  3.     <body class="application articles_show">  
  4.       <!-- Responsive navigation  
  5.       ==================================================== -->  
  6.       <div class="container">  
  7.         <nav id="nav">  
  8.       <ul>  
  9.         <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 %>" 
  21. end  

使用標準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    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  
  7. Requests/sec:    283.61  
  8. Transfer/sec:    503.34KB  

使用escape_utils 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    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  
  7. Requests/sec:    295.98  
  8. Transfer/sec:    558.72KB 

fast_blank gem

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

僅需要在你的Gemfile中添加gem 'fast_blank',這應該就可以非常漂亮的提高像這篇文章中提到的String.black?方法的速度。為了測試,我僅添加下倆代碼:

fast_blank是一個簡單的擴展,提供了一個支持String.blank?功能的快速實現。

  1. def fast_blank_test  
  2.    n = 1000  
  3.  
  4.    strings = [  
  5.      "",  
  6.      "\r\n\r\n  ",  
  7.      "this is a test",  
  8.      "   this is a longer test",  
  9.      "   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  

#p#

使用標準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     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  
  7. Requests/sec:      6.90  
  8. Transfer/sec:      3.31KB 

使用fast_blank 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     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  
  7. Requests/sec:      7.20  
  8. Transfer/sec:      3.45KB 

oj gem

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

這個測試用例非常簡單,僅僅將所有的article序列化為JSON格式:

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

使用標準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  
  7. Requests/sec:     92.17  
  8. Transfer/sec:      5.74MB 

使用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  
  7. Requests/sec:    127.65  
  8. Transfer/sec:      7.98MB 

使用jemalloc

好吧,這其實不是一個真正的gem,如果你想深入探究它,可以看我的這篇文章。在初始測試時,jemalloc并沒有產生太多性能的提升,至少對我使用的測試用例是這樣的。

提示:某些情況下,可能會默認包含在Ruby中。

更新:請一定嘗試一下kzk的jemalloc gem

  1. gem install jemalloc  
  2. je -v rails s 

深入探究你的Rails應用

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

結語

鑒于你的應用要做什么,你可能想為你的Gemfile添加上述的一些gem。通常我會把他們都添加上,當然是出于一個好的估量(你可能會想檢查你的RAM利用率,然后在添加之前,進行一個完整的測試)。

oj gem基于JSON API,對Rails來說是非常不錯的,使用oj gem,你可以刪除視圖并僅使用代言人或者你選擇的模式進行序列化。

英文原文:Improve Rails performance by adding a few gems

責任編輯:林師授 來源: 開源中國社區 編譯
相關推薦

2018-02-06 11:10:27

iOS開發Xcode快捷鍵

2021-04-09 10:26:43

Python編程技術

2011-03-15 17:46:43

2014-03-26 10:00:06

RailsRails性能

2015-01-21 15:40:44

GoRuby

2018-02-04 22:29:21

iOS開發

2011-03-11 09:27:11

Java性能監控

2019-09-17 09:21:01

2018-06-14 09:35:35

2021-06-10 10:02:19

優化緩存性能

2016-01-04 11:04:17

Web開發Ruby

2017-02-21 13:36:11

iosAPP性能

2011-08-31 10:54:25

Java性能

2010-03-24 18:27:27

無線mesh網絡應用

2021-02-24 15:16:45

微服務架構數據

2009-07-01 16:20:34

Flex垃圾回收性能優化

2013-08-27 13:24:46

App Store應用上傳應用截圖ASO應用商店優化

2022-04-02 14:43:59

Promethues監控

2023-10-09 08:14:10

Helm管理應用

2014-09-18 09:50:32

Ruby on Rai
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 天堂av免费观看 | 色五月激情五月 | 精品自拍视频在线观看 | 国产精品久久久久久妇女6080 | 精品欧美一区免费观看α√ | 五月激情婷婷六月 | 91精品久久久久久久久久入口 | 色综合网站 | 国产精品大全 | 国产日韩久久 | 亚洲国产一区二区在线 | 人人九九精 | 日韩电影一区 | 欧美一级全黄 | 日韩精品一区二区三区四区 | 99在线免费观看 | 久草新在线 | 亚洲欧洲一区二区 | 亚洲欧洲一区二区 | 欧美成人精品一区二区男人看 | 亚洲成人精品在线 | 国产91在线 | 欧美 | 九九精品影院 | 高清国产一区二区 | 一区二区三区国产 | 成人欧美一区二区三区在线观看 | 中文字幕av第一页 | 国产精品视频一 | 中文字幕丁香5月 | 天天看天天摸天天操 | 久久99国产精品 | 天天干视频在线 | 久久久噜噜噜久久中文字幕色伊伊 | 99亚洲精品| 色播99| 久久一区二区三区四区 | 91九色视频在线 | 欧美日韩国产传媒 | 奇米四色影视 | 欧美久久天堂 | 色就干 |