Module: TestProf::Autopilot::Patches::EventProfPatch

Defined in:
lib/test_prof/autopilot/patches/event_prof_patch.rb

Overview

Monkey-patch for ‘TestProf::EventProf::RSpecListener’. Redefined ‘report’ method provides writing artifact to the directory instead of printing report

Constant Summary collapse

ARTIFACT_FILE =
"event_prof_report.json"

Class Method Summary collapse

Class Method Details

.patchObject



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
# File 'lib/test_prof/autopilot/patches/event_prof_patch.rb', line 12

def patch
  TestProf::EventProf::RSpecListener.class_eval do
    def report(profiler)
      result = profiler.results

      profiler_hash = {
        event: profiler.event,
        total_time: profiler.total_time,
        absolute_run_time: profiler.absolute_run_time,
        total_count: profiler.total_count,
        top_count: profiler.top_count,
        rank_by: profiler.rank_by,
        time_percentage: time_percentage(profiler.total_time, profiler.absolute_run_time)
      }

      profiler_hash[:groups] = result[:groups].map do |group|
        {
          description: group[:id].top_level_description,
          location: group[:id].[:location],
          time: group[:time],
          count: group[:count],
          examples: group[:examples],
          run_time: group[:run_time],
          time_percentage: time_percentage(group[:time], group[:run_time])
        }
      end

      dir_path = FileUtils.mkdir_p(Autopilot.config.tmp_dir)[0]
      file_path = File.join(dir_path, ARTIFACT_FILE)

      File.write(file_path, JSON.generate(profiler_hash))
    end
  end
end