Skip to content
This repository
branch: 3-1-stable
Fetching contributors…

Cannot retrieve contributors at this time

file 115 lines (97 sloc) 3.191 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
require 'jruby/profiler'
require 'java'
java_import java.lang.management.ManagementFactory

module ActiveSupport
  module Testing
    module Performance
      DEFAULTS.merge!(
        if ARGV.include?('--benchmark')
          {:metrics => [:wall_time, :user_time, :memory, :gc_runs, :gc_time]}
        else
          { :metrics => [:wall_time],
            :formats => [:flat, :graph] }
        end).freeze

      protected
        def run_gc
          ManagementFactory.memory_mx_bean.gc
        end

      class Profiler < Performer
        def initialize(*args)
          super
          @supported = @metric.is_a?(Metrics::WallTime)
        end

        def run
          return unless @supported

          @total = time_with_block do
            @data = JRuby::Profiler.profile do
              full_profile_options[:runs].to_i.times { run_test(@metric, :profile) }
            end
          end
        end

        def record
          return unless @supported

          klasses = full_profile_options[:formats].map { |f| JRuby::Profiler.const_get("#{f.to_s.camelize}ProfilePrinter") }.compact

          klasses.each do |klass|
            fname = output_filename(klass)
            FileUtils.mkdir_p(File.dirname(fname))
            file = File.open(fname, 'wb') do |file|
              klass.new(@data).printProfile(file)
            end
          end
        end

        protected
          def output_filename(printer_class)
            suffix =
              case printer_class.name.demodulize
                when 'FlatProfilePrinter'; 'flat.txt'
                when 'GraphProfilePrinter'; 'graph.txt'
                else printer_class.name.sub(/ProfilePrinter$/, '').underscore
              end

            "#{super()}_#{suffix}"
          end
      end

      module Metrics
        class Base
          def profile
            yield
          end

          protected
            def with_gc_stats
              ManagementFactory.memory_mx_bean.gc
              yield
            end
        end

        class WallTime < Time
          def measure
            super
          end
        end

        class CpuTime < Time
          def measure
            ManagementFactory.thread_mx_bean.get_current_thread_cpu_time / 1000 / 1000 / 1000.0 # seconds
          end
        end

        class UserTime < Time
          def measure
            ManagementFactory.thread_mx_bean.get_current_thread_user_time / 1000 / 1000 / 1000.0 # seconds
          end
        end

        class Memory < DigitalInformationUnit
          def measure
            ManagementFactory.memory_mx_bean.non_heap_memory_usage.used + ManagementFactory.memory_mx_bean.heap_memory_usage.used
          end
        end

        class GcRuns < Amount
          def measure
            ManagementFactory.garbage_collector_mx_beans.inject(0) { |total_runs, current_gc| total_runs += current_gc.collection_count }
          end
        end

        class GcTime < Time
          def measure
            ManagementFactory.garbage_collector_mx_beans.inject(0) { |total_time, current_gc| total_time += current_gc.collection_time } / 1000.0 # seconds
          end
        end
      end
    end
  end
end
Something went wrong with that request. Please try again.