Skip to content
This repository
tree: e6cb3962bb
Fetching contributors…

Cannot retrieve contributors at this time

file 120 lines (99 sloc) 2.909 kb
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 114 115 116 117 118 119 120
require 'rubinius/agent'

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

      class Profiler < Performer
        def run
          @profiler = Rubinius::Profiler::Instrumenter.new
          
          @profiler.profile(false) do
            profile_options[:runs].to_i.times { run_test(@metric, :profile) }
          end
          
          @total = @profiler.info[:runtime] / 1000 / 1000 / 1000.0 # seconds
        end
        
        def report
          super
        end
        
        def record
          if(profile_options[:formats].include?(:flat))
            create_path_and_open_file(:flat) do |file|
              @profiler.show(file)
            end
          end
          
          if(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(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 Time < Base; end
        
        class ProcessTime < Time
          # unsupported
          def measure; 0; end
        end

        class WallTime < Time
          def measure
            super
          end
        end

        class CpuTime < Time
          # unsupported
          def measure; 0; end
        end

        class Memory < Base
          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
Something went wrong with that request. Please try again.