Skip to content

Commit

Permalink
Define profiling feature
Browse files Browse the repository at this point in the history
- Implement a Profiler class which measures performance with RubyProf
  and stdlib's Benchmark.
- Add --profile CLI option which enables the profiler.
  • Loading branch information
skalee committed Mar 14, 2021
1 parent e1508ed commit 734b0a7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/iev.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

# frozen_string_literal: true

require "benchmark"
require "creek"
require "mathml2asciimath"
require "relaton"
require "relaton_bib"
require "ruby-prof"
require "sequel"
require "thor"
require "yaml"
Expand Down
6 changes: 6 additions & 0 deletions lib/iev/termbase/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def self.shared_option(name, methods:, **kwargs)
type: :boolean,
default: false,
methods: %i[xlsx2yaml db2yaml]

shared_option :profile,
desc: "Generates profiler reports for this program, requires ruby-prof",
type: :boolean,
default: false,
methods: %i[xlsx2yaml xlsx2db db2yaml]
end
end
end
1 change: 1 addition & 0 deletions lib/iev/termbase/cli/command_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def collection_file_path(file, output_dir)
# Assigns some global variables accordingly, so these settings are
# available throughout the program.
def handle_generic_options(options)
$TERMBASE_PROFILE = options[:profile]
$TERMBASE_PROGRESS = options.fetch(:progress, !ENV["CI"])
$TERMBASE_DEBUG_TERM_ATTRIBUTES = options[:debug_term_attributes]
end
Expand Down
67 changes: 67 additions & 0 deletions lib/iev/termbase/profiler.rb
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

0 comments on commit 734b0a7

Please sign in to comment.