Ruby performanceprofilingMinsk, SaM Solutions, 2013Presented by Alexey Tulia@AlexeyTulia, github.com/crible
Ruby. A programmer’s best friend
Ruby implementations
Reasons?
Garbage collectionSlow methods call
one iteration takes ~ 100msGarbage collectionNeed one Gb?Expect 128 GC calls!You lose 128*0,1 = 12,8 secAllocated memory n...
Garbage collectionMore objects allocation -> more GC calls -> slow code
RUBY_HEAP_MIN_SLOTS=8000000RUBY_GC_MALLOC_LIMIT=10000Garbage collection tuningperformance patches
Ruby String performancerequire benchmarkITERATIONS = 1000000def run(str, bench)bench.report("#{str.length + 1} chars") doI...
Ruby String performanceHeap stringsShared stringsEmbedded strings
struct RString {long len;char *ptr;};Ruby String performancestruct RString {long len;char *ptr;VALUE shared;};struct RStri...
Slow methods call
What can I do to improve performance?Use C extensions or gemsUse plain SQL instead of frameworksUse CPU and memory profilin...
STRACEtrace system calls and signalsstrace -cp <pid>strace -ttTp <pid> -o <file>
% time seconds uses/call calls errors syscall50,39 0,00064 0 1197 592 read34,56 0,00044 0 609 writev14,96 0,000019 0 1226 ...
LTRACEtrace dynamic library callsltrace -F <conf_file> -bg -x <symbol> -p pid
ltrace -F <conf_file> -bg -x <symbol> -p pid-F <conf_file>int mysql_real_query(addr,string,ulong);void garbage_collect(void)...
ltrace -x garbage_collectltrace -x mysql_real_querymysql_real_query(0x1c9e0500, "SET NAMES UTF8", 16) = 0 <0.000324>mysql_...
RUBY-PROFfast code profiler for Ruby
%self total self child calls name------------------------------------------------------------------------8.39 0.54 0.23 0....
KCachegrinda tool for visualisationhttp://kcachegrind.sourceforge.net
KCachegrind
KCachegrind
PERFTOOLS.RBgoogle’s performance tools for ruby codeCPUPROFILE=/tmp/myprof pprof --calgrind ./myapp /tmp/myprofgem install...
PERFTOOLS.RBCPUPROFILE_REALTIME = 1CPU/wall timeCPUPROFILE_OBJECTS = 1CPUPROFILE_METHODS = 1
When should I stop performance optimisation?Premature optimization is the root of all evil(c) Donald Knuth
Make it workMake it rightMake it fast
What should I remeber before profiling?Turn GC off (GC.disable)class Foodef do_smthreturn "x" * 1024 # take one Kb of memor...
Questions?
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Профилирование и оптимизация производительности Ruby-кода
Upcoming SlideShare
Loading in...5
×

Профилирование и оптимизация производительности Ruby-кода

925

Published on

Лекция посвящена профилированию Ruby-кода, анализу результатов работы профилировщика и использованию этих данных для оптимизации производительности.

Published in: Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
925
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
4
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Профилирование и оптимизация производительности Ruby-кода"

  1. 1. Ruby performanceprofilingMinsk, SaM Solutions, 2013Presented by Alexey Tulia@AlexeyTulia, github.com/crible
  2. 2. Ruby. A programmer’s best friend
  3. 3. Ruby implementations
  4. 4. Reasons?
  5. 5. Garbage collectionSlow methods call
  6. 6. one iteration takes ~ 100msGarbage collectionNeed one Gb?Expect 128 GC calls!You lose 128*0,1 = 12,8 secAllocated memory never returns to the system!runs on every 8Mb of allocated memory
  7. 7. Garbage collectionMore objects allocation -> more GC calls -> slow code
  8. 8. RUBY_HEAP_MIN_SLOTS=8000000RUBY_GC_MALLOC_LIMIT=10000Garbage collection tuningperformance patches
  9. 9. Ruby String performancerequire benchmarkITERATIONS = 1000000def run(str, bench)bench.report("#{str.length + 1} chars") doITERATIONS.times donew_string = str + xendendendBenchmark.bm do |bench|run("12345678901234567890", bench)run("123456789012345678901", bench)run("1234567890123456789012", bench)run("12345678901234567890123", bench)run("123456789012345678901234", bench)run("1234567890123456789012345", bench)run("12345678901234567890123456", bench)enduser system total real21 chars 0.250000 0.000000 0.250000 ( 0.247459)22 chars 0.250000 0.000000 0.250000 ( 0.246954)23 chars 0.250000 0.000000 0.250000 ( 0.248440)24 chars 0.480000 0.000000 0.480000 ( 0.478391)25 chars 0.480000 0.000000 0.480000 ( 0.479662)26 chars 0.480000 0.000000 0.480000 ( 0.481211)27 chars 0.490000 0.000000 0.490000 ( 0.490404)
  10. 10. Ruby String performanceHeap stringsShared stringsEmbedded strings
  11. 11. struct RString {long len;char *ptr;};Ruby String performancestruct RString {long len;char *ptr;VALUE shared;};struct RString {char ary[RSTRING_EMBED_LEN_MAX +1];}RSTRING_EMBED_LEN_MAX = 23
  12. 12. Slow methods call
  13. 13. What can I do to improve performance?Use C extensions or gemsUse plain SQL instead of frameworksUse CPU and memory profilingUse Rubinius or JRuby
  14. 14. STRACEtrace system calls and signalsstrace -cp <pid>strace -ttTp <pid> -o <file>
  15. 15. % time seconds uses/call calls errors syscall50,39 0,00064 0 1197 592 read34,56 0,00044 0 609 writev14,96 0,000019 0 1226 epoll_ctl0,00 0,000000 0 4 close0,00 0,000000 0 1 select0,00 0,000000 0 4 socket0,00 0,000000 0 4 4 connect0,00 0,000000 0 1057 epoll_wait100,0 0,000127 4134 596 totalstrace -cp <pid>
  16. 16. LTRACEtrace dynamic library callsltrace -F <conf_file> -bg -x <symbol> -p pid
  17. 17. ltrace -F <conf_file> -bg -x <symbol> -p pid-F <conf_file>int mysql_real_query(addr,string,ulong);void garbage_collect(void);int memcached_set(addr,string,ulong,string,ulong);
  18. 18. ltrace -x garbage_collectltrace -x mysql_real_querymysql_real_query(0x1c9e0500, "SET NAMES UTF8", 16) = 0 <0.000324>mysql_real_query(0x1c9e0500, "SET SQL_AUTO_IS_NULL=0", 22) = 0 <0.000322>mysql_real_query(0x19c7a500, "SELECT * FROM `users`", 21) = 0 <1.206506>mysql_real_query(0x1c9e0500, "COMMIT", 6) = 0 <0.000181>
  19. 19. RUBY-PROFfast code profiler for Ruby
  20. 20. %self total self child calls name------------------------------------------------------------------------8.39 0.54 0.23 0.31 602 Array#each_index7.30 0.41 0.20 0.21 1227 Integer#gcd6.20 0.49 0.17 0.32 5760 Timecell#date5.11 0.15 0.14 0.01 1 Magick::Image#to_blobRUBY-PROF
  21. 21. KCachegrinda tool for visualisationhttp://kcachegrind.sourceforge.net
  22. 22. KCachegrind
  23. 23. KCachegrind
  24. 24. PERFTOOLS.RBgoogle’s performance tools for ruby codeCPUPROFILE=/tmp/myprof pprof --calgrind ./myapp /tmp/myprofgem install perftools.rbRUBYOPT="-r`gem which perftools | tail -1`" ruby my_app.rb
  25. 25. PERFTOOLS.RBCPUPROFILE_REALTIME = 1CPU/wall timeCPUPROFILE_OBJECTS = 1CPUPROFILE_METHODS = 1
  26. 26. When should I stop performance optimisation?Premature optimization is the root of all evil(c) Donald Knuth
  27. 27. Make it workMake it rightMake it fast
  28. 28. What should I remeber before profiling?Turn GC off (GC.disable)class Foodef do_smthreturn "x" * 1024 # take one Kb of memoryendendsmth = "x" * 7999999 # alloc almost 8MbFoo.new.do_smth # here GC will be calleddo_smth is so slow!!!
  29. 29. Questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×