Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby China 背后的故事

Jason Lee
October 10, 2015

Ruby China 背后的故事

Jason Lee

October 10, 2015
Tweet

More Decks by Jason Lee

Other Decks in Programming

Transcript

  1. ᮱ᗟᯈᗝ • UCloud VPS Ԇ๢Ҕ • 1 ݣ 4 Core

    CPU VPS; • 4GB ٖਂҔ • MongoDB̵Cache ࣁݶӞݣ๢࢏ӤҔ • UpYun ಓᓕӤփ෈կ҅Assets҅զ݊൉׀ Assets CDN ๐ۓҔ • 5M ଃ਼ᇿՁኪמ + ᘶ᭗ᗑᕶҔ
  2. ᩻ᬦ 20K տާ̵27K ᓤૼৼզ݊ 270K ࢧૼ 0 2500 5000 7500

    10000 12500 15000 17500 20000 22500 25000 27500 30000 2011 2012 2013 2014 Now տާᰁ 0 5000 10000 15000 20000 25000 30000 2011 2012 2013 2014 Now ᦾ᷌ 0 50000 100000 150000 200000 250000 300000 2011 2012 2013 2014 Now ࢧ॔
  3. 3953 Ոᛗ੝ݎᬦӞེᩂ Topic.pluck(:user_id).uniq.count Reply.pluck(:user_id).uniq.count 7802 Ոᛗ੝ࢧᬦӞེᩂ 0 50 100 150

    200 250 300 350 400 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000 rei huacnlee lgn21st luikore kgen bhuztez blacktulip chairy11 ibachue hooooopo ࢧૼᰁ ݎૼᰁ
  4. ଘ࣐ྯॠ 13K ӻᇿᒈᦢਮ҅զ݊ 30K ེᶭᶎᦢᳯᰁ Google ړຉᕹᦇᦢᳯᰁ 0 4,000 8,000

    12,000 16,000 20,000 24,000 28,000 32,000 36,000 40,000 8์25෭ 8์30෭ 9์4෭ 9์9෭ 9์14෭ 9์19෭ 9์24෭ ᦢਮ ၨᥦᰁ
  5. UpYun ਂؙྯ์᧗࿢ᕹᦇ ၞᰁ(GB) 0 30 60 90 120 150 180

    210 240 ᧗࿢ᰁ(ེ) 0 800000 1600000 2400000 3200000 4000000 4800000 5600000 6400000 7200000 8000000 2013-05 2013-11 2014-05 2014-11 2015-05 2015-09 ᑮᳵܛአ: 1.11 GB
  6. - ୏তԭ 2010-8҅ᒫӞӻᇇ๜ฎ Ruby 1.9.2̵Rails 3.0.0.rc2; - ๋ڡฎ MySQL ପ;

    - ᬮဌํአ Markdown҅ݝฎᓌܔጱ simple_format ໒ୗ۸Ҕ - ဌํ᭗ᎣۑᚆҔ - ݝฎᓌܔጱړᔄ̵ᦾ̵᷌ࢧ̵॔ᦕԪ๜پӻۑᚆҔ - ᶋଉᓌܔጱݸݣኴᶎҔ - ᛔګᓌܔጱአಁᔮᕹҔ
  7. source 'http://rubygems.org'
 
 gem 'rails', ‘3.0.0.rc2'
 gem 'mysql2'
 gem ‘paperclip'

    gem 'authlogic'
 gem 'will_paginate', '3.0.pre' 
 gem 'memcache-client'

  8. def index
 @topics = Topic.last_actived.includes(:user,:node).limit(10)
 @sections = Section.all.includes(:nodes)
 end
 


    def show
 @topic = Topic.find(params[:id])
 if current_user
 Node.set_user_last_visited(current_user.id, @topic.node_id)
 @topic.user_readed(current_user.id)
 end
 @node = @topic.node
 @replies = @topic.replies.all
 end
 
 def reply
 @topic = Topic.find(params[:id])
 @reply = @topic.replies.build(params[:reply]) 
 @reply.user_id = current_user.id
 if @reply.save
 flash[:notice] = "ࢧ॔౮ۑ"
 else
 flash[:notice] = @reply.errors.full_messages.join("<br />")
 end
 redirect_to topic_path(params[:id],:anchor => 'reply')
 end
  9. source 'http://rubygems.org'
 
 gem "rails", "3.1.1"
 gem "mongoid", "2.2.4"
 gem

    'carrierwave', '0.5.6'
 gem 'mini_magick','3.3'
 gem 'mongoid_auto_increment_id', "0.2.2"
 gem "devise", "1.4.5"
 gem 'will_paginate', '3.0.pre2'
 gem 'omniauth', '0.3.0'
 gem "oa-openid", '0.3.0'
 gem 'dalli', '1.1.1' … gem "rails-i18n","0.1.8"
 gem 'redis-namespace','~> 1.0.2'
 gem "redis-objects", "0.5.2"
 gem "rdiscount","1.6.8"
 gem "settingslogic", "~> 2.0.6"
 gem "cells", "3.6.7"
 gem "resque", "~> 1.19.0", :require => "resque/server"
 gem "aws-ses", "~> 0.4.3"
 gem 'mail_view', :git => 'git://github.com/37signals/mail_view.git'
 gem "daemon-spawn", "~> 0.4.2"
 #
 gem "sprite-factory", "1.4.1"
 group :development do
 gem 'capistrano', '2.9.0'
 gem 'chunky_png', “1.2.5" gem 'factory_girl_rails'
 end
  10. def index
 @topics = Topic.last_actived.limit(10)
 @sections = Section.all
 render :stream

    => true
 end
 
 def show
 @topic = Topic.find(params[:id])
 @topic.hits.incr(1)
 @node = @topic.node
 @replies = @topic.replies.asc(:_id).all.cache
 if current_user
 @topic.user_readed(current_user.id)
 current_user.notifications.where(:reply_id.in => @replies.map(&:id), :read => false).update_all(:read => true)
 end
 render :stream => true
 end
  11. • ruby-china.org ऒݷҔ • Linode ᮱ᗟҔ • ෛጱ UIҔ •

    ᦾ᷌ྋ෈ MarkdownҔ • ीے Wiki ۑᚆ̵ඪ೮դᎱṛՄҔ • Devise ๊դአಁᔮᕹ; • Mongoid, Assets Pipeline, Carrierwave, redis-objects, cellsҔ • ीے Notification ۑᚆҔ • Ծڊ mongoid_auto_increment_idҔ
  12. ӞԶݎ઀ොݻ • വۖᕟᕢ RubyConf China ၚۖҔ • Ӥၹ̵๺૞̵۹Ղӣࣈ᷇ᔺጱ Ruby Tuesday

    ᕚӥၚۖҔ • Ruby ᆽঅᘏ T ௫Ҕ • ෆቘෛಋفᳪᩒා WikiҔ • ڠᒈ GitHub Ruby China ୏რੜᕟҔ • ᘉᦲ Ruby ᐒ܄ᅾᳪದ๞ᩒාҔ • ᤉኞ RailsCast China̵TeaHour;
  13. 2400 ེ Commit, 105 ᨯሠᘏ ᩻ᬦ 5 ེ Commit ጱํ

    21 Ո 0 175 350 525 700 875 1050 1225 1400 huacnlee xdite chloerei ashchan forresty doitian qichunren xiaods fredwu bachue 10 11 19 23 37 40 83 88 185 1,394 Commits
  14. > rake stats +----------------------+-------+-------+---------+---------+-----+-------+ | Name | Lines | LOC

    | Classes | Methods | M/C | LOC/M | +----------------------+-------+-------+---------+---------+-----+-------+ | Controllers | 1793 | 1471 | 37 | 208 | 5 | 5 | | Helpers | 489 | 414 | 0 | 54 | 0 | 5 | | Models | 1841 | 1424 | 27 | 146 | 5 | 7 | | Mailers | 16 | 15 | 2 | 1 | 0 | 13 | | Javascripts | 7633 | 5866 | 1 | 694 | 694 | 6 | | Libraries | 708 | 500 | 8 | 46 | 5 | 8 | | Api specs | 1024 | 886 | 0 | 1 | 0 | 884 | | Controller specs | 720 | 613 | 0 | 0 | 0 | 0 | | Feature specs | 37 | 33 | 0 | 0 | 0 | 0 | | Helper specs | 461 | 378 | 0 | 0 | 0 | 0 | | Lib specs | 620 | 470 | 3 | 0 | 0 | 0 | | Model specs | 1405 | 1185 | 3 | 0 | 0 | 0 | | Request specs | 51 | 40 | 0 | 0 | 0 | 0 | | Routing specs | 21 | 17 | 0 | 0 | 0 | 0 | | View specs | 35 | 27 | 0 | 0 | 0 | 0 | +----------------------+-------+-------+---------+---------+-----+-------+ | Total | 16854 | 13339 | 81 | 1150 | 14 | 9 | +----------------------+-------+-------+---------+---------+-----+-------+ Code LOC: 9690 Test LOC: 3649 Code to Test Ratio: 1:0.4
  15. GitHub Issues 2015 Open Issue 30 Closed Issue 230 Closed

    PR 54 Merged PR 127 Open PR 3 Open PR Merged PR Closed PR Closed Issue Open Issue
  16. ኧᗑ݋Ծኞ • य़ᰁጱ Ruby դᎱ᯿຅Ҕ • ୚فၥᦶҔ • @ ӥ೉൉ᐏҔ

    • Markdown ᶼᥦҔ • ग़᧍᥺ඪ೮Ҕ • APIҔ • ᕷय़ग़හฎ Bug ץྋզ݊ੜදᬰҔ
  17. ᮎԶᤩ೏ᕷധጱۑᚆ • ᐺמ • ஙמړՁ • Internet Explorer ඪ೮ •

    ىဳՈҁ๋ڡጱ෸ײ҂ • ଆۗڦՈᖫᬋૼৼ • Vote Up / Down
  18. Ruby China ᶱፓݱӻᴤྦྷىᲫᶭᶎߥଫ෸ᳵ੒ྲ Response Time (ms) 50 100 150 200

    250 300 350 400 450 500 550 600 650 700 750 800 / /topics /topics/:id /topics/node:id /:login Homeland (Rails 3.0) 2011-11 (Rails 3.1) 2013-05 (Rails 3.2) 2013-12 (Rails 4.0) 2014-05 (Rails 4.1) Now (Rails 4.2)
  19. • ᓌ۸᭦ᬋ֦҅፥ጱᵱᥝᮎԍ॔๥ጱۑᚆҘ • ӧᥝ஺ኼԧڹᒒጱපሲҔ • य़᮱ړ෸ᳵ᮷ᜰࣁ View Ӥᶎ҅Fragment CacheҔ •

    ᭿ع॔๥᭦ᬋ҅՗ۑᚆ޾ຝ຅Ӥᶎכ೮᪃ड़ጱᓌܔҔ • ຤ԶԪఘݢզԻᕳ JavaScript ๶॒ቘҔ • ग़፡ Rails API ෈໩҅ݎሿྋᏟጱֵአොୗ҅ྲই render collection: itemsҔ • MRI Ruby ࣁ IO ጱ᮱ړٌਫݢզአ Thread ਫሿଚᤈጱҔ • Gem ጱෛᇇ๜ݢզտํ௔ᚆ޾ٖਂጱץ॔҅ىဳ Changelog, PR; • ഩൎྋᏟጱ᮱ᗟොୗ҅GC ᧣ս; • ဳ఺֦አጱ Gem ٖਂ୏ᲀҔ
  20. - evanphx/benchmark-ips - ӧᥝ௛ฎ“౯ᥧ஑”֦҅ଫᧆᖫٟ Benchmark ၥᦶ੒ྲҔ - MiniProfiler/rack-mini-profiler - ᕳଫአ಑ᅩ҅ړຉᘙ෸Ҕ

    - rails/rails-perftest - ᕳଫአᖫٟ௔ᚆၥᦶአֺҔ - SamSaffron/memory_profiler - ړຉᑕଧݱӻ᧣አ຾ٖਂ୏ᲀ҅ allocated ੒᨝ེහ҅ݎሿᩇٖਂጱ GemҔ - ruby-prof/ruby-prof - चԭդᎱྦྷጱړຉҔ
  21. ଚݎ IO ᧗࿢ def index
 @threads = []
 @threads <<

    Thread.new do
 @suggest_topics = Topic.suggest.limit(3)
 end
 
 @threads << Thread.new do
 @topics = Topic.last_actived
 if current_user
 @topics = @topics.without_nodes(current_user.blocked_node_ids)
 @topics = @topics.without_users(current_user.blocked_user_ids)
 else
 @topics = @topics.without_hide_nodes
 end
 @topics = @topics.paginate(page: params[:page], per_page: 22)
 end
 @threads.each(&:join)
 end
  22. ํපڥአ updated_at ਫሿᖨਂ <% cache([reply]) do %>
 <div class="reply">
 ...


    </div>
 <% end %> irb> reply.cache_key
 "replies/256261-20150825031859640000000"
  23. Now

  24. • ੱݢᚆ᭽஗ Rails ๋֯ਫ᪢๶ਫሿ֦ጱݱӻۑᚆҁLess SQL҅ Router URL҅Helper҅ຽٵ CURD҅ERB)Ҕ • ࠺አ

    ActiveModel ጱ ScopeҁTopic.last_actived.without_hide_nodes҂ • ࠺አ Helper ᓌ۸ Viewҁuser_name_tag, user_avatar_tag҂ • ෸ڰىဳ Rails ۖா᭄҅ᇇ๜҅ᛗ੝Ӿ֖හҁ4.x.0҂ᇇ๜Ҕ • ىဳᶱፓ໐ஞۑᚆጱᔄପࣁ GitHub Ӥጱๅෛۖா҅ํᚆێݢզ݇ Өਠ࠺Ҕ • ੱݢᚆጱٺ੝ᛔ૩ጱդᎱᰁ҅ᚆአପጱࣈොአପᥴ٬ᳯ᷌҅ྲইӤ փ̵ړᶭ҅๦ᴴ̵ᴚڜᬯԶ Ruby ᐒ܄૪ᕪᶋଉ౮ᆧጱᶱፓᔄࣳҔ
  25. END