Skip to content

Instantly share code, notes, and snippets.

@edwardkenfox
Last active January 24, 2024 08:18
Show Gist options
  • Save edwardkenfox/6e53d590ac5a5ee5081dab0d7bd23e4f to your computer and use it in GitHub Desktop.
Save edwardkenfox/6e53d590ac5a5ee5081dab0d7bd23e4f to your computer and use it in GitHub Desktop.
PSI collector
# frozen_string_literal: true
source "https://rubygems.org"
# gem "rails"
gem "faraday"
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
method_source (1.0.0)
net-http (0.4.1)
uri
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
uri (0.13.0)
PLATFORMS
arm64-darwin-22
DEPENDENCIES
faraday
pry
BUNDLED WITH
2.4.10
https://example.com
https://www.wikipedia.org/
url TTFB FCP LCP CLS TBT TTFB field FCP field LCP field CLS field FID field INP field
https://example.com 124 900 900 0 0 AVERAGE FAST FAST FAST FAST FAST
https://www.wikipedia.org/ 203 1288 3118.5 0 2611.5 FAST FAST FAST FAST FAST FAST
require "faraday"
require "csv"
API_KEY = ENV["API_KEY"]
API_ENDPOINT = "https://www.googleapis.com"
API_PATH = "/pagespeedonline/v5/runPagespeed"
file = File.read("input.txt")
urls = file.split("\n")
conn = Faraday.new(
url: API_ENDPOINT,
params: {
key: API_KEY,
locale: "ja",
strategy: "mobile",
},
)
conn.options.timeout = 120
headers = ["url", "TTFB", "FCP", "LCP", "CLS", "TBT", "TTFB field", "FCP field", "LCP field", "CLS field", "FID field", "INP field"]
CSV.open("output.csv", "wb") do |csv|
csv << headers
urls.each do |url|
puts "Getting PSI results for #{url} ..."
begin
res = conn.get(API_PATH, { url: url })
rescue Net::ReadTimeout, Faraday::TimeoutError
puts "Net::ReadTimeout for #{url}, retrying"
retry
end
if res.status != 200
csv << [url] + Array.new(headers.size - 1) { 0 }
next
end
json = JSON.parse(res.body)
lighthouse = json["lighthouseResult"]["audits"]
field = json["originLoadingExperience"]
csv << [
url,
lighthouse["server-response-time"]["numericValue"],
lighthouse["first-contentful-paint"]["numericValue"],
lighthouse["largest-contentful-paint"]["numericValue"],
lighthouse["cumulative-layout-shift"]["numericValue"],
lighthouse["total-blocking-time"]["numericValue"],
field ? field["metrics"]["EXPERIMENTAL_TIME_TO_FIRST_BYTE"]&.send(:[], "category") : 0,
field ? field["metrics"]["FIRST_CONTENTFUL_PAINT_MS"]&.send(:[], "category") : 0,
field ? field["metrics"]["LARGEST_CONTENTFUL_PAINT_MS"]&.send(:[], "category") : 0,
field ? field["metrics"]["CUMULATIVE_LAYOUT_SHIFT_SCORE"]&.send(:[], "category") : 0,
field ? field["metrics"]["FIRST_INPUT_DELAY_MS"]&.send(:[], "category") : 0,
field ? field["metrics"]["INTERACTION_TO_NEXT_PAINT"]&.send(:[], "category") : 0,
]
puts "Finished"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment