performance.rb 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. require 'rubygems'
  2. require 'active_resource'
  3. require 'benchmark'
  4. TIMES = (ENV['N'] || 10_000).to_i
  5. # deep nested resource
  6. attrs = {
  7. :id => 1,
  8. :name => 'Luis',
  9. :age => 21,
  10. :friends => [
  11. {
  12. :name => 'JK',
  13. :age => 24,
  14. :colors => ['red', 'green', 'blue'],
  15. :brothers => [
  16. {
  17. :name => 'Mateo',
  18. :age => 35,
  19. :children => [{ :name => 'Edith', :age => 5 }, { :name => 'Martha', :age => 4 }]
  20. },
  21. {
  22. :name => 'Felipe',
  23. :age => 33,
  24. :children => [{ :name => 'Bryan', :age => 1 }, { :name => 'Luke', :age => 0 }]
  25. }
  26. ]
  27. },
  28. {
  29. :name => 'Eduardo',
  30. :age => 20,
  31. :colors => [],
  32. :brothers => [
  33. {
  34. :name => 'Sebas',
  35. :age => 23,
  36. :children => [{ :name => 'Andres', :age => 0 }, { :name => 'Jorge', :age => 2 }]
  37. },
  38. {
  39. :name => 'Elsa',
  40. :age => 19,
  41. :children => [{ :name => 'Natacha', :age => 1 }]
  42. },
  43. {
  44. :name => 'Milena',
  45. :age => 16,
  46. :children => []
  47. }
  48. ]
  49. }
  50. ]
  51. }
  52. class Customer < ActiveResource::Base
  53. self.site = "http://37s.sunrise.i:3000"
  54. end
  55. module Nested
  56. class Customer < ActiveResource::Base
  57. self.site = "http://37s.sunrise.i:3000"
  58. end
  59. end
  60. Benchmark.bm(40) do |x|
  61. x.report('Model.new (instantiation)') { TIMES.times { Customer.new } }
  62. x.report('Nested::Model.new (instantiation)') { TIMES.times { Nested::Customer.new } }
  63. x.report('Model.new (setting attributes)') { TIMES.times { Customer.new attrs } }
  64. x.report('Nested::Model.new (setting attributes)') { TIMES.times { Nested::Customer.new attrs } }
  65. end