diff --git a/README.md b/README.md index b10a258..e4abc3e 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Sample output is here |--derived-data-path||Root path of DerivedData directory| |--truncate-at|-t|Truncate the method name with specified length| |--no-unique||Show the duplicated results| +|--per-file||Show sum time per file| ## Use custom reporters diff --git a/lib/xcprofiler.rb b/lib/xcprofiler.rb index 94f13f0..1bde845 100644 --- a/lib/xcprofiler.rb +++ b/lib/xcprofiler.rb @@ -29,6 +29,7 @@ def execute(args) opts.on("-t", "--truncate-at [TRUNCATE_AT]", Integer, "Truncate the method name with specified length") { |v| options.truncate_at = v } opts.on("--[no-]unique", "Reject duplicated location results or not") { |v| options.unique = v } opts.on("--output [PATH]", String, "File path to output reporters' result") { |v| options.output = v } + opts.on("--per-file", "Show sum time per file") { |v| options.per_file = v } opts.on_tail("-h", "--help", "Show this message") do puts opts exit @@ -58,7 +59,8 @@ def execute(args) order: order, show_invalid_locations: options[:show_invalid_locations], truncate_at: options[:truncate_at], - unique: options[:unique] + unique: options[:unique], + per_file: options[:per_file] } reporters = [StandardOutputReporter.new(reporter_args)] if options[:output] diff --git a/lib/xcprofiler/reporters/abstract_reporter.rb b/lib/xcprofiler/reporters/abstract_reporter.rb index 3edb8f9..8fe07d3 100644 --- a/lib/xcprofiler/reporters/abstract_reporter.rb +++ b/lib/xcprofiler/reporters/abstract_reporter.rb @@ -15,6 +15,7 @@ def report!(executions) def filter_executions(executions) executions = executions.delete_if(&:invalid?) unless show_invalid_locations? executions = delete_duplicated(executions) if unique + executions = group_by_executions_per_file(executions) if per_file executions = executions.delete_if { |v| v.time < threshold } if threshold executions = sort_executions(executions, order) executions = executions[0...limit] if limit @@ -42,6 +43,15 @@ def delete_duplicated(executions) } end + def group_by_executions_per_file(executions) + executions.group_by { |execution| + execution.path + }.map { |path, executions| + time = executions.map(&:time).reduce(:+) + Execution.new(time, "#{path}:0:0", '') + } + end + def limit options[:limit] end @@ -66,5 +76,9 @@ def unique return options[:unique] unless options[:unique].nil? true end + + def per_file + options[:per_file] || false + end end end diff --git a/spec/reporters/abstract_reporter_spec.rb b/spec/reporters/abstract_reporter_spec.rb index de91f36..3a99aac 100644 --- a/spec/reporters/abstract_reporter_spec.rb +++ b/spec/reporters/abstract_reporter_spec.rb @@ -146,12 +146,26 @@ end end + context 'with per_file' do + let(:reporter) { AbstractReporter.new(per_file: true) } + + it 'returns filtered executions' do + expect(filtered_executions.size).to eql(1) + expect(filtered_executions.first.path).to eql('/path/to/Source.swift') + expect(filtered_executions.first.line).to eql(0) + expect(filtered_executions.first.column).to eql(0) + expect(filtered_executions.first.time).to eql(45.0) + expect(filtered_executions.first.method_name).to eql('') + end + end + context 'with multi_options' do let(:order) { :file } let(:limit) { 5 } let(:threshold) { 1 } let(:show_invalid_locations) { true } let(:unique) { true } + let(:per_file) { true } context 'with order and limit' do let(:reporter) { AbstractReporter.new(order: order, limit: limit) } @@ -224,6 +238,24 @@ end end + context 'with per_file and show_invalid_locations' do + let(:reporter) { AbstractReporter.new(per_file: per_file, show_invalid_locations: show_invalid_locations) } + + it 'returns filtered executions' do + expect(filtered_executions.size).to eql(2) + expect(filtered_executions.first.path).to eql('/path/to/Source.swift') + expect(filtered_executions.first.line).to eql(0) + expect(filtered_executions.first.column).to eql(0) + expect(filtered_executions.first.time).to eql(45.0) + expect(filtered_executions.first.method_name).to eql('') + expect(filtered_executions.last.path).to eql('') + expect(filtered_executions.last.line).to eql(0) + expect(filtered_executions.last.column).to eql(0) + expect(filtered_executions.last.time).to eql(9.0) + expect(filtered_executions.last.method_name).to eql('') + end + end + context 'with order, limit and threshold' do let(:reporter) { AbstractReporter.new(order: order, limit: limit, threshold: threshold) }