rails performance
play

Rails Performance Michael Koziarski michael@koziarski.com Rails - PowerPoint PPT Presentation

Rails Performance Michael Koziarski michael@koziarski.com Rails Performance Relax Programmers Love Optimisation Science Based Objective Provable Opportunity Cost Is this optimisation really the best use of your time? Two Justifications


  1. Rails Performance Michael Koziarski michael@koziarski.com

  2. Rails Performance

  3. Relax

  4. Programmers Love Optimisation

  5. Science Based

  6. Objective

  7. Provable

  8. Opportunity Cost

  9. Is this optimisation really the best use of your time?

  10. Two Justifications

  11. Hardware Costs

  12. Responsiveness

  13. Hardware Costs

  14. Profits = Revenue - Costs

  15. Hardware isn’t all of your costs

  16. Hardware costs are not continuous

  17. Hardware costs are not continuous Unless you’re huge

  18. Hardware costs are not continuous Unless you’re huge You’re not huge

  19. Hardware is basically free

  20. Hardware is basically free Compared to staff

  21. Hardware Cost Driven Optimisation

  22. Example.com • 4 Servers at $299/mo • Total hosting of ~ $1,200/mo

  23. Example.com • 10% performance improvement across the board! • Leads to a $120/mo saving...

  24. Example.com • Staff costing 60-80k/yr • $28-$38/hr

  25. It almost certainly wasn’t worth it

  26. Responsiveness

  27. Much more Important

  28. Slow applications are no fun to use

  29. No Users

  30. No Revenue

  31. No Business

  32. Not about requests per second

  33. Seconds per request

  34. Perceived Performance

  35. 853ms

  36. 3:00

  37. PHP is 0.4% of the total

  38. Assume it gets 50% faster

  39. The net saving is 0.2%

  40. Not Noticeable

  41. Frontend Performance should be your Priority

  42. YSlow

  43. Fix all of these

  44. I’ve done all that!

  45. Optimisation Procedure

  46. Optimisation Procedure 1. Figure out what’s slow

  47. Optimisation Procedure 1. Figure out what’s slow 2. Figure out why it’s slow

  48. Optimisation Procedure 1. Figure out what’s slow 2. Figure out why it’s slow 3. Make it faster

  49. 1. Find What’s Slow

  50. Choose your target

  51. Choose your target • Slower than the rest

  52. Choose your target • Slower than the rest • Heavily used

  53. Grep your Logs

  54. Grep your Logs Completed in 220ms (View: 72, DB: 5)

  55. Grep your Logs Completed in 220ms (View: 72, DB: 5) X-Runtime: 0.00783

  56. PL Analyze

  57. 1b. Cheat

  58. Caching!

  59. HTTP Caching

  60. ETags

  61. ETags ETag: b4c2fedde6926a5ee6ed8cd5cc995592

  62. ETags ETag: b4c2fedde6926a5ee6ed8cd5cc995592 If-None-Match: b4c2fedde6926a5ee6ed8cd5cc995592

  63. ETags ETag: b4c2fedde6926a5ee6ed8cd5cc995592 If-None-Match: b4c2fedde6926a5ee6ed8cd5cc995592 



def
handle_etag 





etag!
[@user.id,
@user.updated_at] 



end

  64. Last-Modified

  65. Last-Modified Last-Modified: Sun, 28 Sep 2008 19:38:05 GMT

  66. Last-Modified Last-Modified: Sun, 28 Sep 2008 19:38:05 GMT If-Not-Modified-Since: Sun, 28 Sep 2008 19:38:05 GMT

  67. Last-Modified Last-Modified: Sun, 28 Sep 2008 19:38:05 GMT If-Not-Modified-Since: Sun, 28 Sep 2008 19:38:05 GMT 



def
handle_last_modified 





last_modified!
@user.updated_at 



end

  68. Expires 



def
add_expires 





response.headers["Expires"]
=
@user.updated_at
+
1.hour 



end

  69. Rails Caching

  70. Fragment Caching <%
cache
[@user,
"dashboard"]
do
%> 

<%=
render
:partial=>"complex_expensive_dashboard"
%> <%
end
%>

  71. Action Caching caches_action
:index

  72. Model Caching class
Comment
<
ActiveRecord::Base 

acts_as_cached
:ttl=>1.hour end

  73. Memcache • Seriously Fast • Seriously Scalable • LRU Cache Expunging

  74. Cache Generations There are only two hard things in Computer Science: cache invalidation and naming things

  75. Cache Generations There are only two hard things in Computer Science: cache invalidation and naming things So let’s not explicitly invalidate anything

  76. Cache Generations cache
["user",
@user.id] cache
["user_profile",
@user.id]


  77. Cache Generations cache
["user",
@user.id,
@user.generation] cache
["user_profile",
@user.id,
@user.generation]


  78. Cache Generations class
User
<
ActiveRecord::Base 

before_save
:increment_generation 

 

def
increment_generation 



self.generation
+=
1
 

end end

  79. Cache Generations class
Friendship
<
ActiveRecord::Base 

belongs_to
:owner,
:class_name=>"User" 

belongs_to
:friend,
:class_name=>"User" 

before_save
:increment_both_users 

 

def
increment_both_users 



[owner,
friend].map
&:increment_generation 

end end

  80. 2. Figure out why it’s slow

  81. Never Guess

  82. You’re never right

  83. Slow Trac

  84. Slow Trac • Use a new PostgreSQL driver

  85. Slow Trac • Use a new PostgreSQL driver • Use mod_python, not tracd

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend