คุณรู้ว่า performance เป็น feature และ performance problems เยอะแก้ได้ระหว่าง development
แตเรื่อง slowdowns ที่โผล่แค่ใน production ล่ะ? ต้อง加 log messages ทุกบรรทัด? นั่นจะช้าลง! หรือ commit เยอะ "maybe this fixes it" ดูว่าติดไหน?
ไม่ต้องทำลายโค้ดเพื่อ analyze ลอง rbtrace แทน
Trace Running Ruby App
ด้วย rbtrace detect performance problems, รันโค้ดใน Ruby process อื่น, log method calls โดยไม่加 code 加 gem "rbtrace" ใน Gemfile
รู้จัก rbtrace จากโพสต์ Sam Saffron เรื่อง debug memory leaks ใน Ruby (อ่านบังคับถ้ายังไม่อ่าน)
Sam 用 rbtrace ดู objects ทั้งหมดที่ process 用:
bundle exec rbtrace -p $SIDEKIQ_PID -e 'Thread.new{GC.start;require "objspace";io=File.open("/tmp/ruby-heap.dump", "w"); ObjectSpace.dump_all(output: io); io.close}'
Awesome แตทำได้เยอะกว่านี้
rbtrace ทำอะไรได้บ้าง?
อยากดู SQL statements ใน production (และใช้เวลานานแค่ไหน)?
~/Source/testapps/rbtrace jweiss$ rbtrace -p $RAILS_PID --methods "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#execute_and_clear(sql)" *** attached to process 7897 ActiveRecord::ConnectionAdapters::PostgreSQLAdapter#execute_and_clear(sql="SELECT \"articles\".* FROM \"articles\" WHERE \"articles\".\"id\" = $1 LIMIT 1") <0.002631>
Method calls ที่นานกว่า 2 วินาที?
~/Source/testapps/rbtrace jweiss$ rbtrace -p $RAILS_PID --slow 2000 *** attached to process 8154 Integer#times <2.463761> ArticlesController#create <2.558673>
อยากรู้ทุกครั้งที่ method 某 เรียก?
~/Source/testapps/rbtrace jweiss$ rbtrace -p $RAILS_PID --methods "ActiveRecord::Persistence#save" *** attached to process 8154 ActiveRecord::Persistence#save <0.010964>
ดู threads ที่ app รัน?
~/Source/testapps/rbtrace jweiss$ rbtrace -p $RAILS_PID -e "Thread.list" *** attached to process 8154 >> Thread.list => [#<Thread:0x007ff4fcc9a8a8@/usr/local/lib/ruby/gems/2.2.0/gems/puma-2.6.0/lib/puma/server.rb:269 sleep>, #<Thread:0x007ff4fcc9aa10@/usr/local/lib/ruby/gems/2.2.0/gems/puma-2.6.0/lib/puma/thread_pool.rb:148 sleep>, #<Thread:0x007ff4fcc9ab50@/usr/local/lib/ruby/gems/2.2.0/gems/puma-2.6.0/lib/puma/reactor.rb:104 sleep>, #<Thread:0x007ff4f98c0410 sleep>]
ใช่ -e รัน Ruby code ใน server:
~/Source/testapps/rbtrace jweiss$ rbtrace -p $RAILS_PID -e "ActiveRecord::Base.connection_config" *** attached to process 8154 >> ActiveRecord::Base.connection_config => {:adapter=>"postgresql", :pool=>5, :timeout=>5000, :database=>"rbtrace_test"}
โอเค ตอนนี้ผมกลัวนิดหน่อย แตยัง very cool (และแค่ users ที่มี permission รบกวน process rbtrace ได้ ดังนั้นคง OK)
rbtrace ให้ tools 多 ใส่ Ruby processes ใน staging และ production เห็น processes ใช้ (หรือ abuse) memory ยังไง trace slow function calls และรัน Ruby code ได้
ไม่ต้องสร้าง test commits และ log messages เยอะ แค่ hop onto server, ได้ data, hop กลับ และแม้ผมไม่ 完全 สบายใจ用ใน production ยังช่วยใน test environments
คุณล่ะ? 用 rbtrace ทำอะไรได้บ้าง?