SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
Ruby Performance
The Low Hanging Fruit
Hello, I’m
Bruce Werdschinski
Senior Lead Developer, Prezentt
!
Spent 5 years as the Technical Director
at GTP iCommerce
!
~2.5 years with Ruby, built hundreds of
ecommerce websites, loves SEO &
CRO
!
Mercedes enthusiast
Agenda
• Introduction to common tools
• Scenarios where we would use
those tools
• What else can we do, low
hanging fruit wise
From experience, look at common
expensive tasks
• Database operations
• Network access
• Incorrect algorithm
• Unnecessary work - Can we cache it?
Ruby benchmark
• Disable Garbage Collection first with: GC.disable
• Part of the Ruby standard library
• I prefer to use it like this:
!
time = Benchmark.realtime do
# The code I’m testing
end
benchmark-ips
• https://github.com/evanphx/benchmark-ips
• Benchmarks a blocks iterations/second.
• For short snippits of code, IPS automatically figures out how many
times to run the code to get interesting data
• Provides a standard deviation measure which is very cool
stackprof
• https://github.com/tmm1/stackprof
• For Ruby 2.1 and above
• Samples what the CPU is spending time on
• https://github.com/alisnic/stackprof-webnav is a Web UI for
stackprof dumps
• https://github.com/quirkey/stackprof-remote is middleware and CLI
for fetching and interacting with stackprof
How do we find expensive things?
• Experience / Knowledge
• Logs
• Ruby's own benchmark module
• External tools
• benchmark-ips, stackprof, time
• SaaS metrics tools
• Librato, Datadog, Skylight, New
Relic, Keen IO
Reduce the impact
• Optimise the application
• Optimise the environment
Optimise the Application
• Reduce heavy I/O
• ActiveRecord tweaks
• Caching
Reduce heavy I/O
Silly example, but it illustrates the point. Writing to the screen is more
expensive than writing to memory
!
1_000_000.times do
puts "Hello world!"
end
!
s = String.new
1_000_000.times do
s << "Hello world!n"
end
puts s
10.5 seconds vs. 6.5 seconds - But 2.28 times more memory
Reduce heavy I/O
• Same applies for database calls,
event processing, etc. One large
call is often much faster than many
little calls.
• I’ve been playing with Keen IO
lately, their demo app is at: http://
keen-gem-example.herokuapp.com/
• 50 calls, 1 item each: 20094ms
• 1 call with 50 items: 284ms
ActiveRecord
• Know your SQL
• Check logs to see what ActiveRecord is actually doing
• Lots of tools to help us
ActiveRecord sum example
I want to get a total of all to the orders in the database
Order.sum(&:total)
Order Load (49.3ms) SELECT "orders".* FROM "orders"
Order.sum(:total)
(4.0ms) SELECT SUM("orders"."total") AS sum_id FROM "orders"
ActiveRecord sum example
ActiveRecord sum example
ActiveRecord Tools
• rack-mini-profiler
• RailsPanel
• rails-footnotes
• Bullet
• Peek
rack-mini-profiler
• https://github.com/MiniProfiler/
rack-mini-profiler
• Middleware that shows a HTML
panel on each page
RailsPanel
• https://github.com/dejan/
rails_panel
• Chrome Extension that provides
information about the Rails
request. Includes Database,
rendering and total times.
Memoization
Memoization is a caching technique of storing a computed value to
avoid duplicated work by future calls.
1. Perform some work
2. Store the result of that work
3. Use the stored data the next time you need the result of that work
Memoization
A common pattern is using the conditional assignment operator: ||=
def current_user
User.find(session[:user_id])
end
Instead store the result in an instance variable by using:
def current_user
@current_user ||= User.find(session[:user_id])
end
identity_cache
https://github.com/Shopify/identity_cache
ActiveRecord cache for model objects
Extracted from Shopify
@user = User.fetch(id) instead of @user = User.find(id)
Uses an after_commit hook to expire the object from cache
Rails Cache
• Use the Dalli gem as the Rails cache store
def fetch(id)
Rails.cache.fetch("product-#{id}", expires_in: 5.minutes) do
Product.find(id)
end
end
Is Ruby the best tool?
• Sometimes…shock, horror…Ruby might not be the best tool for the
job.
• Where Ruby may not be the best fit:
• CPU bound
• Async I/O
Leibniz formula for π
1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 … = π/4
20 million iterations
CPU bound task
• Ruby: 3.78 seconds
• Rust: 0.238 seconds
Optimise the Environment
• Low hanging fruit, faster server, database, more memory
• Ruby versions
• Network topology
• Caching systems
• Web performance
Web performance
• No point shaving 1/10th of a second off your database access time
if you’ve got a 3Mb background image.
• http://gtmetrix.com/
• http://tools.pingdom.com/fpt/
No more low hanging fruit
• “Ruby Under a Microscope”
by Pat Shaughnessy
• Utilization Saturation and
Errors (USE) Method
Thank You!
Twitter: @bwerdschinski
!
Email: bruce@werdschinski.com
!
Blog: http://www.werdschinski.com

Más contenido relacionado

La actualidad más candente

Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootOmri Spector
 
Scaling Twitter
Scaling TwitterScaling Twitter
Scaling TwitterBlaine
 
WordPress Speed & Performance from Pagely's CTO
WordPress Speed & Performance from Pagely's CTOWordPress Speed & Performance from Pagely's CTO
WordPress Speed & Performance from Pagely's CTOLizzie Kardon
 
Ansible Best Practices - July 30
Ansible Best Practices - July 30Ansible Best Practices - July 30
Ansible Best Practices - July 30tylerturk
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKAJohan Edstrom
 
Webinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna MitchellWebinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna MitchellCodemotion
 
Consuming REST services with ActiveResource
Consuming REST services with ActiveResourceConsuming REST services with ActiveResource
Consuming REST services with ActiveResourceWolfram Arnold
 
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 TaiwanAutomating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 TaiwanAdler Hsieh
 
Phoenix for Rubyists
Phoenix for RubyistsPhoenix for Rubyists
Phoenix for RubyistsDoug Goldie
 
User-percieved performance
User-percieved performanceUser-percieved performance
User-percieved performanceMike North
 
Alfresco Development Framework Basic
Alfresco Development Framework BasicAlfresco Development Framework Basic
Alfresco Development Framework BasicMario Romano
 
Internet scaleservice
Internet scaleserviceInternet scaleservice
Internet scaleserviceDaeMyung Kang
 
Ceylon From Here to Infinity: The Big Picture and What's Coming
Ceylon From Here to Infinity: The Big Picture and What's Coming Ceylon From Here to Infinity: The Big Picture and What's Coming
Ceylon From Here to Infinity: The Big Picture and What's Coming Virtual JBoss User Group
 
Rails automatic test driven development
Rails automatic test driven developmentRails automatic test driven development
Rails automatic test driven developmenttyler4long
 
I Don't Test Often ...
I Don't Test Often ...I Don't Test Often ...
I Don't Test Often ...Gareth Bowles
 

La actualidad más candente (20)

Building a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring BootBuilding a REST Service in minutes with Spring Boot
Building a REST Service in minutes with Spring Boot
 
Scaling Twitter
Scaling TwitterScaling Twitter
Scaling Twitter
 
WordPress Speed & Performance from Pagely's CTO
WordPress Speed & Performance from Pagely's CTOWordPress Speed & Performance from Pagely's CTO
WordPress Speed & Performance from Pagely's CTO
 
Ansible Best Practices - July 30
Ansible Best Practices - July 30Ansible Best Practices - July 30
Ansible Best Practices - July 30
 
Nodeconf npm 2011
Nodeconf npm 2011Nodeconf npm 2011
Nodeconf npm 2011
 
Stackato v6
Stackato v6Stackato v6
Stackato v6
 
Using Apache Camel as AKKA
Using Apache Camel as AKKAUsing Apache Camel as AKKA
Using Apache Camel as AKKA
 
Webinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna MitchellWebinar: Queues with RabbitMQ - Lorna Mitchell
Webinar: Queues with RabbitMQ - Lorna Mitchell
 
Consuming REST services with ActiveResource
Consuming REST services with ActiveResourceConsuming REST services with ActiveResource
Consuming REST services with ActiveResource
 
Perl-Critic
Perl-CriticPerl-Critic
Perl-Critic
 
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 TaiwanAutomating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
Automating Your Daily Tasks with Scripting - RubyConf 2015 Taiwan
 
Hello world - intro to node js
Hello world - intro to node jsHello world - intro to node js
Hello world - intro to node js
 
Phoenix for Rubyists
Phoenix for RubyistsPhoenix for Rubyists
Phoenix for Rubyists
 
User-percieved performance
User-percieved performanceUser-percieved performance
User-percieved performance
 
Alfresco Development Framework Basic
Alfresco Development Framework BasicAlfresco Development Framework Basic
Alfresco Development Framework Basic
 
Internet scaleservice
Internet scaleserviceInternet scaleservice
Internet scaleservice
 
Future of Java
Future of JavaFuture of Java
Future of Java
 
Ceylon From Here to Infinity: The Big Picture and What's Coming
Ceylon From Here to Infinity: The Big Picture and What's Coming Ceylon From Here to Infinity: The Big Picture and What's Coming
Ceylon From Here to Infinity: The Big Picture and What's Coming
 
Rails automatic test driven development
Rails automatic test driven developmentRails automatic test driven development
Rails automatic test driven development
 
I Don't Test Often ...
I Don't Test Often ...I Don't Test Often ...
I Don't Test Often ...
 

Similar a Ruby performance - The low hanging fruit

Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
 
2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with BlackfireMarko Mitranić
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)Tech in Asia ID
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyKyle Drake
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbMongoDB APAC
 
Evented Ruby VS Node.js
Evented Ruby VS Node.jsEvented Ruby VS Node.js
Evented Ruby VS Node.jsNitin Gupta
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage SystemsSATOSHI TAGOMORI
 
Puppet Development Workflow
Puppet Development WorkflowPuppet Development Workflow
Puppet Development WorkflowJeffery Smith
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Javamalduarte
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangLyon Yang
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance DrupalJeff Geerling
 
Experiences with Debugging Data Races
Experiences with Debugging Data RacesExperiences with Debugging Data Races
Experiences with Debugging Data RacesAzul Systems Inc.
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Junichi Ishida
 
Performance Benchmarking: Tips, Tricks, and Lessons Learned
Performance Benchmarking: Tips, Tricks, and Lessons LearnedPerformance Benchmarking: Tips, Tricks, and Lessons Learned
Performance Benchmarking: Tips, Tricks, and Lessons LearnedTim Callaghan
 
DrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilityDrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilitycherryhillco
 

Similar a Ruby performance - The low hanging fruit (20)

Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire2019 PHP Serbia - Boosting your performance with Blackfire
2019 PHP Serbia - Boosting your performance with Blackfire
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::SynchronyFast, concurrent ruby web applications with EventMachine and EM::Synchrony
Fast, concurrent ruby web applications with EventMachine and EM::Synchrony
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
Buildingsocialanalyticstoolwithmongodb
 
Top ten-list
Top ten-listTop ten-list
Top ten-list
 
Drupal performance
Drupal performanceDrupal performance
Drupal performance
 
Evented Ruby VS Node.js
Evented Ruby VS Node.jsEvented Ruby VS Node.js
Evented Ruby VS Node.js
 
Ruby and Distributed Storage Systems
Ruby and Distributed Storage SystemsRuby and Distributed Storage Systems
Ruby and Distributed Storage Systems
 
Puppet Development Workflow
Puppet Development WorkflowPuppet Development Workflow
Puppet Development Workflow
 
High Performance With Java
High Performance With JavaHigh Performance With Java
High Performance With Java
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon YangPractical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
Practical IoT Exploitation (DEFCON23 IoTVillage) - Lyon Yang
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance Drupal
 
Experiences with Debugging Data Races
Experiences with Debugging Data RacesExperiences with Debugging Data Races
Experiences with Debugging Data Races
 
Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.Great Tools Heavily Used In Japan, You Don't Know.
Great Tools Heavily Used In Japan, You Don't Know.
 
Performance Benchmarking: Tips, Tricks, and Lessons Learned
Performance Benchmarking: Tips, Tricks, and Lessons LearnedPerformance Benchmarking: Tips, Tricks, and Lessons Learned
Performance Benchmarking: Tips, Tricks, and Lessons Learned
 
Rails Performance
Rails PerformanceRails Performance
Rails Performance
 
Scaling tappsi
Scaling tappsiScaling tappsi
Scaling tappsi
 
DrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalabilityDrupalCampLA 2014 - Drupal backend performance and scalability
DrupalCampLA 2014 - Drupal backend performance and scalability
 

Último

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 

Último (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 

Ruby performance - The low hanging fruit

  • 1. Ruby Performance The Low Hanging Fruit
  • 2. Hello, I’m Bruce Werdschinski Senior Lead Developer, Prezentt ! Spent 5 years as the Technical Director at GTP iCommerce ! ~2.5 years with Ruby, built hundreds of ecommerce websites, loves SEO & CRO ! Mercedes enthusiast
  • 3. Agenda • Introduction to common tools • Scenarios where we would use those tools • What else can we do, low hanging fruit wise
  • 4. From experience, look at common expensive tasks • Database operations • Network access • Incorrect algorithm • Unnecessary work - Can we cache it?
  • 5. Ruby benchmark • Disable Garbage Collection first with: GC.disable • Part of the Ruby standard library • I prefer to use it like this: ! time = Benchmark.realtime do # The code I’m testing end
  • 6. benchmark-ips • https://github.com/evanphx/benchmark-ips • Benchmarks a blocks iterations/second. • For short snippits of code, IPS automatically figures out how many times to run the code to get interesting data • Provides a standard deviation measure which is very cool
  • 7. stackprof • https://github.com/tmm1/stackprof • For Ruby 2.1 and above • Samples what the CPU is spending time on • https://github.com/alisnic/stackprof-webnav is a Web UI for stackprof dumps • https://github.com/quirkey/stackprof-remote is middleware and CLI for fetching and interacting with stackprof
  • 8. How do we find expensive things? • Experience / Knowledge • Logs • Ruby's own benchmark module • External tools • benchmark-ips, stackprof, time • SaaS metrics tools • Librato, Datadog, Skylight, New Relic, Keen IO
  • 9. Reduce the impact • Optimise the application • Optimise the environment
  • 10. Optimise the Application • Reduce heavy I/O • ActiveRecord tweaks • Caching
  • 11. Reduce heavy I/O Silly example, but it illustrates the point. Writing to the screen is more expensive than writing to memory ! 1_000_000.times do puts "Hello world!" end ! s = String.new 1_000_000.times do s << "Hello world!n" end puts s 10.5 seconds vs. 6.5 seconds - But 2.28 times more memory
  • 12. Reduce heavy I/O • Same applies for database calls, event processing, etc. One large call is often much faster than many little calls. • I’ve been playing with Keen IO lately, their demo app is at: http:// keen-gem-example.herokuapp.com/ • 50 calls, 1 item each: 20094ms • 1 call with 50 items: 284ms
  • 13. ActiveRecord • Know your SQL • Check logs to see what ActiveRecord is actually doing • Lots of tools to help us
  • 14. ActiveRecord sum example I want to get a total of all to the orders in the database Order.sum(&:total) Order Load (49.3ms) SELECT "orders".* FROM "orders" Order.sum(:total) (4.0ms) SELECT SUM("orders"."total") AS sum_id FROM "orders"
  • 17. ActiveRecord Tools • rack-mini-profiler • RailsPanel • rails-footnotes • Bullet • Peek
  • 19. RailsPanel • https://github.com/dejan/ rails_panel • Chrome Extension that provides information about the Rails request. Includes Database, rendering and total times.
  • 20. Memoization Memoization is a caching technique of storing a computed value to avoid duplicated work by future calls. 1. Perform some work 2. Store the result of that work 3. Use the stored data the next time you need the result of that work
  • 21. Memoization A common pattern is using the conditional assignment operator: ||= def current_user User.find(session[:user_id]) end Instead store the result in an instance variable by using: def current_user @current_user ||= User.find(session[:user_id]) end
  • 22. identity_cache https://github.com/Shopify/identity_cache ActiveRecord cache for model objects Extracted from Shopify @user = User.fetch(id) instead of @user = User.find(id) Uses an after_commit hook to expire the object from cache
  • 23. Rails Cache • Use the Dalli gem as the Rails cache store def fetch(id) Rails.cache.fetch("product-#{id}", expires_in: 5.minutes) do Product.find(id) end end
  • 24. Is Ruby the best tool? • Sometimes…shock, horror…Ruby might not be the best tool for the job. • Where Ruby may not be the best fit: • CPU bound • Async I/O
  • 25. Leibniz formula for π 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 … = π/4 20 million iterations CPU bound task • Ruby: 3.78 seconds • Rust: 0.238 seconds
  • 26. Optimise the Environment • Low hanging fruit, faster server, database, more memory • Ruby versions • Network topology • Caching systems • Web performance
  • 27. Web performance • No point shaving 1/10th of a second off your database access time if you’ve got a 3Mb background image. • http://gtmetrix.com/ • http://tools.pingdom.com/fpt/
  • 28. No more low hanging fruit • “Ruby Under a Microscope” by Pat Shaughnessy • Utilization Saturation and Errors (USE) Method
  • 29. Thank You! Twitter: @bwerdschinski ! Email: bruce@werdschinski.com ! Blog: http://www.werdschinski.com