Skip to content

Instantly share code, notes, and snippets.

@delaneyparker
Created May 1, 2017 21:14
Show Gist options
  • Save delaneyparker/6a9f00a4f4dc1562960eaf89366aa18d to your computer and use it in GitHub Desktop.
Save delaneyparker/6a9f00a4f4dc1562960eaf89366aa18d to your computer and use it in GitHub Desktop.
Example parser for Network logs from Selenium / Chrome
# Since Ruby preserves hash key insertion order, you can iterate over values.
request_hash = Hash.new { |h, k| h[k] = {} }
# Used to convert event timestamps to epoch time.
wall_time_offset = nil
# Note: Calling logs.get() will clear the log buffer.
performance_logs = page.driver.browser.manage.logs.get('performance')
# Extract request / response data from log messages.
performance_logs.each do |log_entry|
message_raw = log_entry.message
if message_raw =~ /Network\.(requestWillBeSent|responseReceived)/
message_type = $1
message_params = JSON.parse(message_raw)['message']['params']
request_id = message_params['requestId']
if message_type == 'requestWillBeSent'
wall_time_offset ||= message_params['wallTime'] - message_params['timestamp']
request_hash[request_id] = {
method: message_params['request']['method'],
request_time: message_params['timestamp'],
url: message_params['request']['url'],
}
elsif request_hash.has_key? request_id
request_hash[request_id].merge!({
response_time: message_params['timestamp'],
response_status: message_params['response']['status']
})
end
end
end
# Convert relative time deltas to timestamps.
request_hash.values.each do |entry|
entry[:request_time] = Time.at(entry[:request_time] + wall_time_offset).utc if entry[:request_time]
entry[:response_time] = Time.at(entry[:response_time] + wall_time_offset).utc if entry[:response_time]
end
@zedtux
Copy link

zedtux commented Mar 5, 2025

Nice! Thanks for sharing 👍

The only remark I have to keep it working today is that I had to update the regexp because there can be some Network.responseReceivedExtraInfo and so on, so I have updated it with /"Network\.(requestWillBeSent|responseReceived)"/ and all is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment