Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add profiler and version of seed_from_disk using it
including specs
- Loading branch information
Showing
5 changed files
with
78 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# adapted from argo | ||
# takes a block of code, starts the profiler, runs the code, then | ||
# stops the profiler and returns the results of the profiling. | ||
# example usage: | ||
# profiler = Profiler.new | ||
# profiler.prof { MoabToCatalog.seed_catalog(storage_dir) } | ||
# profiler.print_results_flat(out_file_id) | ||
class Profiler | ||
|
||
attr_accessor :results | ||
|
||
def prof | ||
RubyProf.start | ||
yield | ||
@results = RubyProf.stop | ||
end | ||
|
||
def print_results_flat(out_file_id) | ||
File.open "log/#{out_file_id}#{Time.current.localtime.strftime('%FT%T')}-flat.txt", 'w' do |file| | ||
RubyProf::FlatPrinterWithLineNumbers.new(@results).print(file) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require_relative '../../lib/profiler.rb' | ||
require 'ruby-prof' | ||
|
||
RSpec.describe Profiler do | ||
let(:profiler) { described_class.new } | ||
|
||
describe '#prof' do | ||
it 'starts, yields, stops, and returns results' do | ||
rp_profile = instance_double(RubyProf::Profile) | ||
expect(RubyProf).to receive(:start) | ||
expect(RubyProf).to receive(:stop).and_return(rp_profile) | ||
test_value = false | ||
profiler.prof { test_value = true } | ||
expect(test_value).to be true | ||
expect(profiler.results).to eq rp_profile | ||
end | ||
end | ||
|
||
describe '#print_results_flat' do | ||
it 'returns the printer and prints to the path we pass in' do | ||
printer = instance_double(RubyProf::FlatPrinterWithLineNumbers) | ||
profiler.prof { 'we just want #prof to run' } | ||
expected_filepath = "log/test#{Time.current.localtime.strftime('%FT%T')}-flat.txt" | ||
expect(RubyProf::FlatPrinterWithLineNumbers).to receive(:new).with(profiler.results).and_return(printer) | ||
expect(printer).to receive(:print) do |file| | ||
# this expectation might need to relax | ||
# if enough time lags between the timestamps | ||
# in expected_filepath and the file name itself | ||
expect(file.path).to eq expected_filepath | ||
end | ||
profiler.print_results_flat('test') | ||
end | ||
end | ||
end |