Module: Pf2
- Defined in:
- ext/pf2/pf2.c,
lib/pf2.rb,
lib/pf2/cli.rb,
lib/pf2/serve.rb,
lib/pf2/version.rb,
lib/pf2/reporter/pprof.rb,
lib/pf2/reporter/annotate.rb,
lib/pf2/reporter/stack_weaver.rb,
lib/pf2/reporter/firefox_profiler_ser2.rb
Defined Under Namespace
Modules: Reporter Classes: CLI, Error, Serve, Session
Constant Summary collapse
- VERSION =
'0.14.0'
Class Method Summary collapse
-
.profile(interval_ms: 9, time_mode: :cpu, out: nil, format: :firefox, &block) ⇒ Object
Profiles the given block of code.
-
.start ⇒ Object
Start a profiling session.
-
.stop ⇒ Object
Stop the current profiling session and return the profile data.
Class Method Details
.profile(interval_ms: 9, time_mode: :cpu, out: nil, format: :firefox, &block) ⇒ Object
Profiles the given block of code.
Parameters: interval_ms - Sampling interval in milliseconds (default: 9) time_mode - :cpu or :wall (default: :cpu) out - String or IO-like object specifying where to write profile data. - nil (default): do not write to file - String: file path to write the profile data - IO-like object: an object responding to #write format - Output format. Possible values are: - :pf2prof: Raw profile dump loadable by 'pf2 report' - :firefox (default): JSON for profiler.firefox.com - :pprof: .pb.gz compatible with Golang's pprof tool
Example:
profile = Pf2.profile(interval_ms: 42) do
your_code_here
end
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/pf2.rb', line 47 def self.profile(interval_ms: 9, time_mode: :cpu, out: nil, format: :firefox, &block) raise ArgumentError, "block required" unless block_given? raise ArgumentError, "Unknown format: #{format}" unless KNOWN_FORMATS.include?(format) if !(out.nil? || out.is_a?(String) || (out.respond_to?(:write) && out.respond_to?(:close))) raise ArgumentError, "'out' must be an IO-like object" end start(interval_ms:, time_mode:) yield result = stop() @@session = nil # let GC clean up the session if out is_path_passed = out.is_a?(String) io = is_path_passed ? File.open(out, "wb") : out case format in :pf2prof io.write(Marshal.dump(result)) in :firefox require 'pf2/reporter' reporter = Reporter::FirefoxProfilerSer2.new(result) io.write(reporter.emit) in :pprof require 'pf2/reporter' reporter = Reporter::Pprof.new(result) io.write(reporter.emit) end io.close if is_path_passed end result ensure if defined?(@@session) && @@session != nil stop @@session = nil end end |
.start ⇒ Object
Start a profiling session.
Parameters: interval_ms - Sampling interval in milliseconds (default: 9) time_mode - :cpu or :wall (default: :cpu)
17 18 19 20 |
# File 'lib/pf2.rb', line 17 def self.start(...) @@session = Session.new(...) @@session.start end |
.stop ⇒ Object
Stop the current profiling session and return the profile data.
23 24 25 |
# File 'lib/pf2.rb', line 23 def self.stop @@session.stop end |