Class: Ferrum::Page::Tracing

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/page/tracing.rb

Overview

Records a Chrome performance trace for the page via the CDP Tracing domain, optionally including screenshots, and streams the resulting trace data to disk or memory once recording stops.

Constant Summary collapse

EXCLUDED_CATEGORIES =
%w[*].freeze
SCREENSHOT_CATEGORIES =
%w[disabled-by-default-devtools.screenshot].freeze
INCLUDED_CATEGORIES =
%w[devtools.timeline v8.execute disabled-by-default-devtools.timeline
disabled-by-default-devtools.timeline.frame toplevel blink.console
blink.user_timing latencyInfo disabled-by-default-devtools.timeline.stack
disabled-by-default-v8.cpu_profiler disabled-by-default-v8.cpu_profiler.hires].freeze
DEFAULT_TRACE_CONFIG =
{
  includedCategories: INCLUDED_CATEGORIES,
  excludedCategories: EXCLUDED_CATEGORIES
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Tracing

Returns a new instance of Tracing.



23
24
25
26
# File 'lib/ferrum/page/tracing.rb', line 23

def initialize(page)
  @page = page
  @subscribed_tracing_complete = false
end

Instance Method Details

#record(path: nil, encoding: :binary, timeout: nil, trace_config: nil, screenshots: false) ⇒ String, true

Accepts block, records trace and by default returns trace data from Tracing.tracingComplete event as output.

Parameters:

  • path (String, nil) (defaults to: nil)

    Save data on the disk.

  • encoding (:binary, :base64) (defaults to: :binary)

    Encode output as Base64 or plain text.

  • timeout (Float, nil) (defaults to: nil)

    Wait until file streaming finishes in the specified time or raise error.

  • screenshots (Boolean) (defaults to: false)

    capture screenshots in the trace.

  • trace_config (Hash{String => Object}) (defaults to: nil)

    config for trace, for categories see getCategories, only one trace config can be active at a time per browser.

Returns:

  • (String, true)

    The trace data from the Tracing.tracingComplete event. When path is specified returns true and stores trace data into file.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ferrum/page/tracing.rb', line 54

def record(path: nil, encoding: :binary, timeout: nil, trace_config: nil, screenshots: false)
  @path = path
  @encoding = encoding
  @pending = Concurrent::IVar.new
  trace_config ||= DEFAULT_TRACE_CONFIG.dup

  if screenshots
    included = trace_config.fetch(:includedCategories, [])
    trace_config.merge!(includedCategories: included | SCREENSHOT_CATEGORIES)
  end

  subscribe_tracing_complete

  start(trace_config)
  yield
  stop

  @pending.value!(timeout || @page.timeout)
end