Skip to content

Commit

Permalink
feat: Add psi(PageSpeed Insights) utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Leko committed Jul 2, 2017
1 parent 68c81e0 commit 759e772
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ gem 'wdm', '~> 0.1.0', platforms: [:mswin, :mingw]
# Windows does not come with time zone data
gem 'tzinfo-data', platforms: [:mswin, :mingw, :jruby]

gem 'rake'
gem 'typhoeus'

# Middleman Gems
gem 'middleman', '~> 4.1'
gem 'middleman-minify-html'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ GEM
contracts (0.13.0)
dotenv (2.2.1)
erubis (2.7.0)
ethon (0.10.1)
ffi (>= 1.3.0)
execjs (2.7.0)
fast_blank (1.0.0)
fastimage (2.1.0)
Expand Down Expand Up @@ -118,6 +120,7 @@ GEM
parallel (1.11.2)
public_suffix (2.0.5)
rack (2.0.3)
rake (12.0.0)
rb-fsevent (0.9.8)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
Expand All @@ -130,6 +133,8 @@ GEM
thor (0.19.4)
thread_safe (0.3.6)
tilt (2.0.7)
typhoeus (1.1.2)
ethon (>= 0.9.0)
tzinfo (1.2.3)
thread_safe (~> 0.1)
uglifier (3.2.0)
Expand All @@ -149,7 +154,9 @@ DEPENDENCIES
middleman-minify-html
middleman-search_engine_sitemap
middleman-syntax
rake
redcarpet (~> 3.3, >= 3.3.3)
typhoeus
tzinfo-data
wdm (~> 0.1.0)

Expand Down
41 changes: 41 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'uri'
require 'json'
require 'typhoeus'

desc 'Compare Google PageSpeed Insights'
task :psi, ['from', 'to'] do |t, args|
STRATEGIES = %w(mobile desktop)
COMPARE_KEYS = [
%i(ruleGroups SPEED score),
%i(ruleGroups USABILITY score),
%i(pageStats numberResources),
%i(pageStats numberHosts),
%i(pageStats totalRequestBytes),
%i(pageStats htmlResponseBytes),
%i(pageStats cssResponseBytes),
%i(pageStats imageResponseBytes),
%i(pageStats javascriptResponseBytes),
%i(pageStats numberJsResources),
%i(pageStats numberCssResources),
# %i(formattedResults ruleResults AvoidLandingPageRedirects),
]
domain_from = URI.parse(args.from).host
domain_to = URI.parse(args.to).host

STRATEGIES.each do |strategy|
response_from = Typhoeus.get("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=#{args.from}&strategy=#{strategy}")
response_to = Typhoeus.get("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=#{args.to}&strategy=#{strategy}")
result_from = JSON.parse(response_from.body, symbolize_names: true)
result_to = JSON.parse(response_to.body, symbolize_names: true)
puts "### #{strategy}"
puts "|Name|#{domain_from}|#{domain_to}|Diff|"
puts '|---|---|---|---|'
COMPARE_KEYS.each do |path|
score_from = result_from.dig(*path).to_i
score_to = result_to.dig(*path).to_i
score_operator = score_to - score_from > 0 ? '+' : ''
puts "|#{path.join('.')}|#{score_from}|#{score_to}|#{score_operator}#{score_to - score_from}|"
end
puts
end
end

0 comments on commit 759e772

Please sign in to comment.