-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
389 additions
and
28 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
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Page | ||
class Tracing | ||
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 | ||
EXCLUDED_CATEGORIES = %w[ | ||
* | ||
].freeze | ||
|
||
def initialize(client:) | ||
@client = client | ||
end | ||
|
||
def record(options = {}, &block) | ||
@options = { | ||
timeout: nil, | ||
screenshots: false, | ||
encoding: :binary, | ||
included_categories: INCLUDED_CATEGORIES, | ||
excluded_categories: EXCLUDED_CATEGORIES, | ||
**options | ||
} | ||
@promise = Concurrent::Promises.resolvable_future | ||
subscribe_on_tracing_event | ||
start | ||
block.call | ||
@client.command("Tracing.end") | ||
@promise.value!(@options[:timeout]) | ||
end | ||
|
||
private | ||
|
||
def start | ||
@client.command( | ||
"Tracing.start", | ||
transferMode: "ReturnAsStream", | ||
traceConfig: { | ||
includedCategories: included_categories, | ||
excludedCategories: @options[:excluded_categories] | ||
} | ||
) | ||
end | ||
|
||
def included_categories | ||
included_categories = @options[:included_categories] | ||
if @options[:screenshots] == true | ||
included_categories = @options[:included_categories] | ["disabled-by-default-devtools.screenshot"] | ||
end | ||
included_categories | ||
end | ||
|
||
def subscribe_on_tracing_event | ||
@client.on("Tracing.tracingComplete") do |event, index| | ||
next if index.to_i != 0 | ||
|
||
@promise.fulfill(stream(event.fetch("stream"))) | ||
rescue StandardError => e | ||
@promise.reject(e) | ||
end | ||
end | ||
|
||
def stream(handle) | ||
Utils::Stream.fetch(encoding: @options[:encoding], path: @options[:path]) do |read_stream| | ||
read_stream.call(client: @client, handle: handle) | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
module Utils | ||
module Stream | ||
STREAM_CHUNK = 128 * 1024 | ||
|
||
module_function | ||
|
||
def fetch(path:, encoding:, &block) | ||
if path.nil? | ||
stream_to_memory(encoding: encoding, &block) | ||
else | ||
stream_to_file(path: path, &block) | ||
end | ||
end | ||
|
||
def stream_to_file(path:, &block) | ||
File.open(path, "wb") { |f| stream_to(f, &block) } | ||
true | ||
end | ||
|
||
def stream_to_memory(encoding:, &block) | ||
data = String.new("") # Mutable string has << and compatible to File | ||
stream_to(data, &block) | ||
encoding == :base64 ? Base64.encode64(data) : data | ||
end | ||
|
||
def stream_to(output, &block) | ||
loop do | ||
read_stream = lambda do |client:, handle:| | ||
client.command("IO.read", handle: handle, size: STREAM_CHUNK) | ||
end | ||
result = block.call(read_stream) | ||
data_chunk = result["data"] | ||
data_chunk = Base64.decode64(data_chunk) if result["base64Encoded"] | ||
output << data_chunk | ||
break if result["eof"] | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.