-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement a Profiler class which measures performance with RubyProf and stdlib's Benchmark. - Add --profile CLI option which enables the profiler.
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# frozen_string_literal: true | ||
|
||
# (c) Copyright 2020 Ribose Inc. | ||
# | ||
|
||
module IEV | ||
module Termbase | ||
class Profiler | ||
attr_reader :bench, :dir, :prefix, :profile | ||
|
||
def self.measure(prefix = nil, &block) | ||
new(prefix).run(&block) | ||
end | ||
|
||
def initialize(prefix, dir: "profile") | ||
@prefix = prefix | ||
@dir = dir | ||
end | ||
|
||
def run(&block) | ||
profiler_enabled? ? run!(&block) : block.call | ||
end | ||
|
||
def run!(&block) | ||
retval = nil | ||
@profile = RubyProf.profile do | ||
@bench = Benchmark.measure do | ||
retval = block.call | ||
end | ||
end | ||
retval | ||
ensure | ||
print_reports | ||
end | ||
|
||
def profiler_enabled? | ||
$TERMBASE_PROFILE | ||
end | ||
|
||
private | ||
|
||
def print_reports | ||
FileUtils.mkdir_p(dir) | ||
print_benchmark("bench.txt") | ||
print_profile("flat.txt", RubyProf::FlatPrinter) | ||
print_profile("graph.html", RubyProf::GraphHtmlPrinter) | ||
print_profile("calls.html", RubyProf::CallStackPrinter) | ||
end | ||
|
||
def print_benchmark(suffix) | ||
contents = [Benchmark::CAPTION, bench.to_s].join("\n") | ||
File.write(report_file_name(suffix), contents) | ||
end | ||
|
||
def print_profile(suffix, printer) | ||
File.open(report_file_name(suffix), "w") do |file| | ||
printer.new(profile).print(file) | ||
end | ||
end | ||
|
||
def report_file_name(suffix) | ||
base_name = [prefix, suffix].compact.join("-") | ||
File.expand_path(base_name, dir) | ||
end | ||
end | ||
end | ||
end |