Project

General

Profile

Bug #11088 ยป bug_hunt_benchmark.rb

austin (Austin Ziegler), 05/10/2015 02:30 AM

 
1
module SimpleInspect
2
  def inspect
3
    result = if inspected?
4
               "#<%s (%d) ...>" % [ inspected_key, inspected_count ]
5
             else
6
               super
7
             end
8
    inspect!
9
    "\n" + result
10
  end
11

    
12
  private
13
  def inspected?
14
    inspected[inspected_key].nonzero?
15
  end
16

    
17
  def inspect!
18
    inspected[inspected_key] += 1
19
  end
20

    
21
  def inspected_count
22
    inspected[inspected_key]
23
  end
24

    
25
  def inspected
26
    $__inspected__ ||= Hash.new { |h, k| h[k] = 0 }
27
  end
28

    
29
  def inspected_key
30
    "%s:0x%014x" % [ self.class, "0x%014x" % (object_id * 2) ]
31
  end
32
end
33

    
34
class Object
35
  include SimpleInspect
36
end
37

    
38
class Entity; end
39
class Indect; end
40
class Koppler; end
41
class Occupancy; end
42
class Control; end
43
class ScheduleEntry; end
44
class SQLiteStorage; end
45
class UBox; end
46
class USpot; end
47
class UFlag; end
48
class UFlow; end
49
class Sign; end
50
class CounterGroup; end
51

    
52
obj1    = Indect.new
53
_, obj2 = Marshal.load(File.read "ruby_object.dump").first
54

    
55
def call_id(obj)
56
  obj.id
57
rescue => ex
58
  ex.message
59
end
60

    
61
def repeat_call_id(obj, repeats = 100000)
62
  repeats.times { call_id(obj) }
63
end
64

    
65
require 'benchmark'
66

    
67
Benchmark.bm(17) do |mark|
68
  mark.report("Normal #{obj1.class}:") { repeat_call_id(obj1) }
69
  mark.report("Recursive #{obj2.class}:") { repeat_call_id(obj2) }
70
end
71

    
72
$__inspected__.clear
73

    
74
p call_id(obj1)
75
# "undefined method `id' for \n#<Indect:0x007fb2e4029e20>:Indect"
76
p call_id(obj2)
77
# "undefined method `id' for #<Indect:0x007fb2e4029c40>"