test-prof-autopilot 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da180b01be948feec684ca0b80005809678834cec159dc8af26b8dcc8549d31f
4
- data.tar.gz: e42323a7e60dbd7198cf4772153f6c42e3544a4b60fc55f66c1fb335055e0aad
3
+ metadata.gz: 9053434f1ede969d5dc60e8f0ed6bada6ba628220fa599c7700ab9fa4bfeec85
4
+ data.tar.gz: ba2428c6baff4d0b0c437f01b3e3f4522f1d9a3f528689bb9de317e54e94e661
5
5
  SHA512:
6
- metadata.gz: 18ec8fffa21d68373ff74f32da3ff1f08c9146a0e74c22e9fd420dc469f61fec1ef7808f06f614caea0ca8c11967cf159163724248a9411fb2ace7f081671e2f
7
- data.tar.gz: 5db488c8dd13b78fc623eddf05ca69465f212e1bb8f6492dd62370f9415e061cc455448d7067f9e05bfce8ef8aac048e471487a61c32c983927dd6ba0fcaaf63
6
+ metadata.gz: d95aeca28faec87d052dbb5ce90dd953753acc8d4026be7b8b9b270e495008405de10672cfc6dee680d3839f90db6c7564faf7655614cee6346b63c0384707c9
7
+ data.tar.gz: ade585ced221e23276f229db9742896e9161ecda7455a66d24f4a81f7184cbb25e4e7d061393062ad3579d9ddf0409861e54939a41ba9dc2b0cd8bbad0657b00
@@ -10,31 +10,10 @@ module TestProf
10
10
  class PrinterError < StandardError; end
11
11
 
12
12
  def print_report(report)
13
- result = report.raw_report
13
+ result = report.result
14
14
 
15
- raise PrinterError, result["error"] if result["error"]
16
-
17
- msgs = []
18
-
19
- msgs <<
20
- <<~MSG
21
- Factories usage
22
-
23
- Total: #{result["total_count"]}
24
- Total top-level: #{result["total_top_level_count"]}
25
- Total time: #{format("%.4f", result["total_time"])}s
26
- Total uniq factories: #{result["total_uniq_factories"]}
27
-
28
- total top-level total time time per call top-level time name
29
- MSG
30
-
31
- result["stats"].each do |stat|
32
- time_per_call = stat["total_time"] / stat["total_count"]
33
-
34
- msgs << format("%8d %11d %13.4fs %17.4fs %18.4fs %18s", stat["total_count"], stat["top_level_count"], stat["total_time"], time_per_call, stat["top_level_time"], stat["name"])
35
- end
36
-
37
- Logging.log msgs.join("\n")
15
+ # TODO: Move total_time to the report
16
+ TestProf::FactoryProf::Printers::Simple.dump(result, start_time: TestProf.now)
38
17
  end
39
18
 
40
19
  module_function :print_report
@@ -21,6 +21,36 @@ module TestProf
21
21
  @type = :factory_prof
22
22
  @raw_report = raw_report
23
23
  end
24
+
25
+ def result
26
+ @result ||= TestProf::FactoryProf::Result.new(
27
+ raw_report["stacks"],
28
+ raw_report["raw_stats"].transform_values! { |stats| stats.transform_keys!(&:to_sym) }
29
+ )
30
+ end
31
+
32
+ def merge(other)
33
+ report = raw_report.dup
34
+
35
+ report["stacks"] += other.raw_report["stacks"] if report["stacks"]
36
+
37
+ (report["raw_stats"].values + other.raw_report["raw_stats"].values).each_with_object({}) do |data, acc|
38
+ if acc.key?(data["name"])
39
+ current = acc[data["name"]]
40
+
41
+ current["total_count"] += data["total_count"]
42
+ current["top_level_count"] += data["top_level_count"]
43
+ current["total_time"] += data["total_time"]
44
+ current["top_level_time"] += data["top_level_time"]
45
+ else
46
+ acc[data["name"]] = data.dup
47
+ end
48
+ end.then do |stats|
49
+ report["raw_stats"] = stats
50
+ end
51
+
52
+ Report.new(report)
53
+ end
24
54
  end
25
55
  end
26
56
  end
@@ -3,22 +3,36 @@
3
3
  module TestProf
4
4
  module Autopilot
5
5
  module FactoryProf
6
- # Module is used for writing :factory_prof report in different formats
7
- module Writer
6
+ # Class is used for writing :factory_prof report in different formats
7
+ class Writer < ReportWriter
8
8
  Registry.register(:factory_prof_writer, self)
9
9
 
10
10
  ARTIFACT_FILE = "factory_prof_report"
11
11
 
12
- def write_report(report, file_name: ARTIFACT_FILE)
13
- dir_path = FileUtils.mkdir_p(Autopilot.config.artifacts_dir)[0]
14
- file_path = File.join(dir_path, file_name + ".json")
12
+ def generate_json
13
+ report.result.to_json
14
+ end
15
15
 
16
- File.write(file_path, JSON.generate(report.raw_report))
16
+ def generate_html
17
+ result = report.result
17
18
 
18
- Logging.log "FactoryProf report saved: #{file_path}"
19
- end
19
+ report_data = {
20
+ total_stacks: result.stacks.size,
21
+ total: result.total_count
22
+ }
23
+
24
+ report_data[:roots] = TestProf::FactoryProf::Printers::Flamegraph.convert_stacks(result)
20
25
 
21
- module_function :write_report
26
+ path = TestProf::Utils::HTMLBuilder.generate(
27
+ data: report_data,
28
+ template: TestProf::FactoryProf::Printers::Flamegraph::TEMPLATE,
29
+ output: TestProf::FactoryProf::Printers::Flamegraph::OUTPUT_NAME
30
+ )
31
+
32
+ File.read(path).tap do
33
+ FileUtils.rm(path)
34
+ end
35
+ end
22
36
  end
23
37
  end
24
38
  end
@@ -1,6 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestProf
4
+ using(Module.new do
5
+ refine FactoryProf::Result do
6
+ def to_json
7
+ {
8
+ stacks: stacks,
9
+ raw_stats: raw_stats
10
+ }.to_json
11
+ end
12
+ end
13
+ end)
14
+
4
15
  module Autopilot
5
16
  module Patches
6
17
  # Monkey-patch for 'TestProf::FactoryProf::Printers::Simple'.
@@ -9,52 +20,18 @@ module TestProf
9
20
  module FactoryProfPatch
10
21
  ARTIFACT_FILE = "factory_prof_report.json"
11
22
 
12
- def patch
13
- TestProf::FactoryProf::Printers::Simple.module_eval do
14
- def self.dump(result, **)
15
- profiler_hash =
16
- if result.raw_stats == {}
17
- {
18
- error: "No factories detected"
19
- }
20
- else
21
- {
22
- total_count: result.stats.sum { |stat| stat[:total_count] },
23
- total_top_level_count: result.stats.sum { |stat| stat[:top_level_count] },
24
- total_time: result.stats.sum { |stat| stat[:top_level_time] },
25
- total_uniq_factories: result.stats.uniq { |stat| stat[:name] }.count,
26
- stats: result.stats
27
- }
28
- end
29
-
30
- dir_path = FileUtils.mkdir_p(Autopilot.config.tmp_dir)[0]
31
- file_path = File.join(dir_path, ARTIFACT_FILE)
23
+ module PrinterExt
24
+ def dump(result, **)
25
+ dir_path = FileUtils.mkdir_p(Autopilot.config.tmp_dir)[0]
26
+ file_path = File.join(dir_path, ARTIFACT_FILE)
32
27
 
33
- File.write(file_path, JSON.generate(profiler_hash))
34
- end
28
+ File.write(file_path, result.to_json)
35
29
  end
30
+ end
36
31
 
37
- TestProf::FactoryProf::Printers::Flamegraph.module_eval do
38
- def self.dump(result, **)
39
- profiler_hash =
40
- if result.raw_stats == {}
41
- {
42
- error: "No factories detected"
43
- }
44
- else
45
- {
46
- total_stacks: result.stacks.size,
47
- total: result.total_count,
48
- roots: convert_stacks(result)
49
- }
50
- end
51
-
52
- dir_path = FileUtils.mkdir_p(Autopilot.config.tmp_dir)[0]
53
- file_path = File.join(dir_path, ARTIFACT_FILE)
54
-
55
- File.write(file_path, JSON.generate(profiler_hash))
56
- end
57
- end
32
+ def patch
33
+ TestProf::FactoryProf::Printers::Simple.singleton_class.prepend(PrinterExt)
34
+ TestProf::FactoryProf::Printers::Flamegraph.singleton_class.prepend(PrinterExt)
58
35
  end
59
36
 
60
37
  module_function :patch
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TestProf
4
4
  module Autopilot # :nodoc:
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: test-prof-autopilot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruslan Shakirov