Sha256: 766ad7b0ad0cf29c3bb2d4787131a03aec11d9acdb21e1f5f7b934791be3ead0

Contents?: true

Size: 1.46 KB

Versions: 24

Compression:

Stored size: 1.46 KB

Contents

module DeepTest
  module Metrics
    class Gatherer
      def self.setup(options)
        $metrics_gatherer = new(options)

        at_exit do
          $metrics_gatherer.write_file
        end
      end

      def self.enabled?
        return false unless $metrics_gatherer
        $metrics_gatherer.enabled?
      end

      def self.section(title, &block)
        $metrics_gatherer.section(title, &block)
      end

      def initialize(options)
        @options = options
        @sections = []
      end

      def enabled?
        !@options.metrics_file.nil?
      end

      def section(title, &block)
        @sections << Section.new(title, &block) if enabled?
      end

      def render
        @sections.map {|s| s.render}.join("\n")
      end
      
      def write_file
        return unless enabled?
        File.open(@options.metrics_file, "w") do |io|
          io << render
        end
      end

      class Section
        def initialize(title, &generate_measurements_block)
          @title = title
          @generate_measurements_block = generate_measurements_block
        end

        def measurement(name, value)
          @measurements << [name, value]
        end

        def gather_measurements
          @measurements = []
          @generate_measurements_block.call(self)
        end

        def render
          gather_measurements
          "[#{@title}]\n" + @measurements.map {|name, value| "#{name}: #{value}"}.join("\n")
        end
      end
    end
  end
end

Version data entries

24 entries across 24 versions & 4 rubygems

Version Path
jstorimer-deep-test-1.0.0 lib/deep_test/metrics/gatherer.rb
deep_test-1.2.2 lib/deep_test/metrics/gatherer.rb
deep_test-1.2.1 lib/deep_test/metrics/gatherer.rb
deep_test-1.2.0 lib/deep_test/metrics/gatherer.rb