Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --per-file option #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion lib/xcprofiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
14 changes: 14 additions & 0 deletions lib/xcprofiler/reporters/abstract_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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", '')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bit strange to pass 0 as line number or column...

}
end

def limit
options[:limit]
end
Expand All @@ -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
32 changes: 32 additions & 0 deletions spec/reporters/abstract_reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down Expand Up @@ -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) }

Expand Down