-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Minitest support to TagProf (#283)
* Add Minitest support to TagProf * Update docs/profilers/tag_prof.md Co-authored-by: Vladimir Dementyev <dementiev.vm@gmail.com> * Fix CI errors * Fix mdl errors * Return __unknown__ when aboslute path cannot be displayed * Limit to last released rails version in railsmaster.gemfile * Fix rspec bug without limiting rails last stable version in railsmaster.gemfile --------- Co-authored-by: Vladimir Dementyev <dementiev.vm@gmail.com>
- Loading branch information
1 parent
02d8f35
commit a98edb5
Showing
10 changed files
with
236 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
module Minitest | ||
module TestProf | ||
class TagProfReporter < BaseReporter # :nodoc: | ||
attr_reader :results | ||
|
||
def initialize(io = $stdout, _options = {}) | ||
super | ||
@results = ::TestProf::TagProf::Result.new("type") | ||
|
||
if event_prof_activated? | ||
require "test_prof/event_prof" | ||
@current_group_id = nil | ||
@events_profiler = configure_profiler | ||
@results = ::TestProf::TagProf::Result.new("type", @events_profiler.events) | ||
end | ||
end | ||
|
||
def prerecord(group, example) | ||
return unless event_prof_activated? | ||
|
||
# enable event profiling | ||
@events_profiler.group_started(true) | ||
end | ||
|
||
def record(result) | ||
results.track(main_folder_path(result), time: result.time, events: fetch_events_data) | ||
@events_profiler.group_started(nil) if event_prof_activated? # reset and disable event profilers | ||
end | ||
|
||
def report | ||
printer = (ENV["TAG_PROF_FORMAT"] == "html") ? ::TestProf::TagProf::Printers::HTML : ::TestProf::TagProf::Printers::Simple | ||
printer.dump(results) | ||
end | ||
|
||
private | ||
|
||
def main_folder_path(result) | ||
return :__unknown__ if absolute_path_from(result).nil? | ||
|
||
absolute_path_from(result) | ||
end | ||
|
||
def absolute_path_from(result) | ||
absolute_path = File.expand_path(result.source_location.first) | ||
absolute_path.slice(/(?<=(?:spec|test)\/)\w*/) | ||
end | ||
|
||
def configure_profiler | ||
::TestProf::EventProf::CustomEvents.activate_all(tag_prof_event) | ||
::TestProf::EventProf.build(tag_prof_event) | ||
end | ||
|
||
def event_prof_activated? | ||
return false if tag_prof_event.nil? | ||
|
||
!tag_prof_event.empty? | ||
end | ||
|
||
def tag_prof_event | ||
ENV["TAG_PROF_EVENT"] | ||
end | ||
|
||
def fetch_events_data | ||
return {} unless @events_profiler | ||
|
||
@events_profiler.profilers.map do |profiler| | ||
[profiler.event, profiler.time || 0.0] | ||
end.to_h | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
$LOAD_PATH.unshift File.expand_path("../../../../../lib", __FILE__) | ||
require "minitest/autorun" | ||
require "active_support" | ||
require "test-prof" | ||
|
||
module Instrumenter | ||
def self.notify(_event, time) | ||
sleep 0.1 | ||
ActiveSupport::Notifications.publish( | ||
"test.event", | ||
0, | ||
time | ||
) | ||
end | ||
end | ||
|
||
describe "Test Class" do | ||
it "succeeds" do | ||
Instrumenter.notify "test.event", 100 | ||
assert(true) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,108 @@ | ||
# frozen_string_literal: true | ||
|
||
describe "TagProf" do | ||
specify "it works", :aggregate_failures do | ||
output = run_rspec("tag_prof", env: {"TAG_PROF" => "type"}) | ||
|
||
expect(output).to include("TagProf report for type") | ||
expect(output).to match(/type\s+time\s+total\s+%total\s+%time\s+avg\n\n/) | ||
expect(output).to match(/fail\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
expect(output).to match(/pass\s+\d{2}:\d{2}\.\d{3}\s+2\s+/) | ||
expect(output).to match(/__unknown__\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
end | ||
context "rspec" do | ||
specify "it works", :aggregate_failures do | ||
output = run_rspec("tag_prof", env: {"TAG_PROF" => "type"}) | ||
|
||
expect(output).to include("TagProf report for type") | ||
expect(output).to match(/type\s+time\s+total\s+%total\s+%time\s+avg\n\n/) | ||
expect(output).to match(/fail\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
expect(output).to match(/pass\s+\d{2}:\d{2}\.\d{3}\s+2\s+/) | ||
expect(output).to match(/__unknown__\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
end | ||
|
||
specify "html report" do | ||
output = run_rspec("tag_prof", env: {"TAG_PROF" => "type", "TAG_PROF_FORMAT" => "html"}) | ||
|
||
specify "html report" do | ||
output = run_rspec("tag_prof", env: {"TAG_PROF" => "type", "TAG_PROF_FORMAT" => "html"}) | ||
expect(output).to include("TagProf report generated:") | ||
|
||
expect(File.exist?("tmp/test_prof/tag-prof.html")).to eq true | ||
end | ||
|
||
expect(output).to include("TagProf report generated:") | ||
context "with events" do | ||
specify "it works", :aggregate_failures do | ||
output = run_rspec( | ||
"tag_prof", | ||
env: {"TAG_PROF" => "type", "TAG_PROF_EVENT" => "test.event,test.event2"} | ||
) | ||
|
||
expect(File.exist?("tmp/test_prof/tag-prof.html")).to eq true | ||
expect(output).to include("TagProf report for type") | ||
expect(output).to match(/type\s+time\s+test\.event\s+test.event2\s+total\s+%total\s+%time\s+avg\n\n/) | ||
expect(output).to match(/fail\s+\d{2}:\d{2}\.\d{3}\s+00:23.000\s+00:00.000\s+1\s+/) | ||
expect(output).to match(/pass\s+\d{2}:\d{2}\.\d{3}\s+00:12.420\s+00:14.041\s+2\s+/) | ||
expect(output).to match(/__unknown__\s+\d{2}:\d{2}\.\d{3}\s+00:00.000\s+00:00.000\s+1\s+/) | ||
end | ||
end | ||
end | ||
|
||
context "with events" do | ||
specify "it works", :aggregate_failures do | ||
output = run_rspec( | ||
"tag_prof", | ||
env: {"TAG_PROF" => "type", "TAG_PROF_EVENT" => "test.event,test.event2"} | ||
) | ||
context "minitest" do | ||
subject(:output) { run_minitest(path, env: env, chdir: chdir) } | ||
let(:path) { "tag_prof" } | ||
let(:env) { {"TAG_PROF" => "type"} } | ||
let(:chdir) { nil } | ||
|
||
it "includes tag prof report" do | ||
expect(output).to include("TagProf report for type") | ||
expect(output).to match(/type\s+time\s+test\.event\s+test.event2\s+total\s+%total\s+%time\s+avg\n\n/) | ||
expect(output).to match(/fail\s+\d{2}:\d{2}\.\d{3}\s+00:23.000\s+00:00.000\s+1\s+/) | ||
expect(output).to match(/pass\s+\d{2}:\d{2}\.\d{3}\s+00:12.420\s+00:14.041\s+2\s+/) | ||
expect(output).to match(/__unknown__\s+\d{2}:\d{2}\.\d{3}\s+00:00.000\s+00:00.000\s+1\s+/) | ||
end | ||
|
||
it "includes tag prof report headers" do | ||
expect(output).to match(/type\s+time\s+total\s+%total\s+%time\s+avg\n\n/) | ||
end | ||
|
||
context "when test suite is run from test file directory" do | ||
it "includes total time spent and number of files tested for integrations directory" do | ||
expect(output).to match(/integrations\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
end | ||
end | ||
|
||
context "when test suite is run from app root directory" do | ||
let(:chdir) { File.expand_path("") } | ||
let(:path) { "spec/integrations/fixtures/minitest/tag_prof" } | ||
|
||
it "includes total time spent and number of files tested for integrations directory" do | ||
expect(output).to match(/integrations\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
end | ||
end | ||
|
||
context "when test suite is run with event_prof" do | ||
let(:env) { {"TAG_PROF" => "type", "TAG_PROF_EVENT" => "test.event"} } | ||
it "includes event name in tag prof report headers" do | ||
expect(output).to match(/test.event/) | ||
end | ||
|
||
it "includes event time in data reported for integrations directory " do | ||
expect(output).to match(/integrations\s+\d{2}:\d{2}\.\d{3}\s+\d{2}:\d{2}\.\d{3}\s+1\s+/) | ||
end | ||
end | ||
|
||
context "when report format is HTML" do | ||
let(:env) { {"TAG_PROF" => "type", "TAG_PROF_FORMAT" => "html"} } | ||
|
||
it "generates an html report and gives its location" do | ||
output = run_rspec("tag_prof", env: env) | ||
|
||
expect(output).to include("TagProf report generated:") | ||
expect(File.exist?("tmp/test_prof/tag-prof.html")).to eq true | ||
end | ||
end | ||
|
||
context "when root test directory is not named 'test' or 'spec'" do | ||
let(:path) { "tmp/subdirectory_not_found" } | ||
let(:chdir) { File.expand_path("") } | ||
|
||
before do | ||
test_content = File.read("spec/integrations/fixtures/minitest/tag_prof_fixture.rb") | ||
File.write("tmp/subdirectory_not_found_fixture.rb", test_content) | ||
end | ||
|
||
it "reports the statistic for the test result with an explicit error message" do | ||
expect(output).to match(/__unknown__/) | ||
end | ||
|
||
after do | ||
File.delete("tmp/subdirectory_not_found_fixture.rb") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters