aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing/performance/rubinius.rb
blob: fafa782e8d0c8633cab1f7f48709aaf6897471fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'rubinius/agent'

module ActiveSupport
  module Testing
    module Performance
      DEFAULTS.merge!(
        if ARGV.include?('--benchmark')
          {:metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time]}
        else
          { :metrics => [:wall_time],
            :formats => [:flat, :graph] }
        end).freeze
      
      protected
        def run_gc
          GC.run(true)
        end
      
      class Performer; end

      class Profiler < Performer
        def initialize(*args)
          super
          @supported = @metric.is_a?(Metrics::WallTime)
        end
                
        def run
          return unless @supported
          
          @profiler = Rubinius::Profiler::Instrumenter.new
          
          @profiler.profile(false) do
            full_profile_options[:runs].to_i.times { run_test(@metric, :profile) }
          end
          
          @total = @profiler.info[:runtime] / 1000 / 1000 / 1000.0 # seconds
        end
        
        def record
          return unless @supported
          
          if(full_profile_options[:formats].include?(:flat))
            create_path_and_open_file(:flat) do |file|
              @profiler.show(file)
            end
          end
          
          if(full_profile_options[:formats].include?(:graph))
            create_path_and_open_file(:graph) do |file|
              @profiler.show(file)
            end
          end
        end
        
        protected
          def create_path_and_open_file(printer_name)
            fname = "#{output_filename}_#{printer_name}.txt"
            FileUtils.mkdir_p(File.dirname(fname))
            File.open(fname, 'wb') do |file|
              yield(file)
            end
          end
      end

      module Metrics        
        class Base
          attr_reader :loopback
          
          def profile
            yield
          end

          protected
            def with_gc_stats
              @loopback = Rubinius::Agent.loopback
              GC.run(true)
              yield
            end
        end

        class WallTime < Time
          def measure
            super
          end
        end

        class Memory < DigitalInformationUnit          
          def measure
            loopback.get("system.memory.counter.bytes").last
          end
        end

        class Objects < Amount          
          def measure
            loopback.get("system.memory.counter.objects").last
          end
        end

        class GcRuns < Amount          
          def measure
            loopback.get("system.gc.full.count").last + loopback.get("system.gc.young.count").last
          end
        end

        class GcTime < Time
          def measure
            (loopback.get("system.gc.full.wallclock").last + loopback.get("system.gc.young.wallclock").last) / 1000.0
          end
        end
      end
    end
  end
end