Skip to content

Instantly share code, notes, and snippets.

@mosdragon
Created March 23, 2014 02:30
Show Gist options
  • Save mosdragon/9717777 to your computer and use it in GitHub Desktop.
Save mosdragon/9717777 to your computer and use it in GitHub Desktop.
Script to open HAR file passed as argument and display the fastest and slowest resources along with their respective loading times. Currently optimized to follow Object Oriented design principles.
# This version of har_script.rb adheres more strictly to
# OOP principles because it enables code reusability without code duplication
require 'json'
require 'open-uri'
# Helper Method nested_search goes through nested Hashes, finds message in
# desired key.
# @param obj, the hash
# @param key, the desired key to be found, nested within obj
# @globalVar, the global variable to be modified
# @return the value associated with the desired key
def nested_search(obj,key, globalVar)
if obj.respond_to?(:key) && obj.key?(key)
globalVar.push(obj[key])
return obj[key]
elsif obj.respond_to?(:each)
r = nil
obj.find_all { |*a| r=nested_search(a.last,key, globalVar) }
return r
end
end
# example arg: https://s3.amazonaws.com/rigor-public/2d304a7acd6cc35e530bcdb7289b93ac55b6863c.har
# First argument must pass a source URL, otherwise script terminates
if ARGV[0] != nil
url = ARGV[0]
# Opens URL as a file and parses JSON from file into a Ruby Hash
file = open(url, "rb") {|doc| doc.read}
begin
data = JSON.load(file)
# Global vars for helper functions
$time = []
$urls = []
#Modifies $time array with timed responses in seconds
nested_search(data, "time", $time)
#Modifies $urls array with each file's url
nested_search(data, "url", $urls)
# Demonstrates that both arrays are of the same length
# puts $time.length
# puts $urls.length
# Finds the minimum and maximum times in seconds
maxTime = $time.max
minTime = $time.min
# Finding indeces from $time array to match with $urls array
maxInd = $time.index(maxTime)
minInd = $time.index(minTime)
# Finds the resource that corresponds with the indeces
slowURL = $urls[maxInd]
fastURL = $urls[minInd]
# Output to terminal
puts "fastest: #{fastURL} #{minTime/1000.0} seconds"
puts "slowest: #{slowURL} #{maxTime/1000.0} seconds"
rescue JSON::ParserError => e
puts "I'm sorry, that isn't a proper HAR file"
end
else
# Only prints if no argument passed when script called
puts "No argument passed. Goodbye."
end
@mosdragon
Copy link
Author

The script has been refactored to follow Object Oriented design principles. The primary change was making the code DRY. There is now code reuse rather than code duplication. The original script created two separate helper methods with the same implementation to modify two separate variables. In the updated script, only one helper method is used, and it achieves the same effect by applying the necessary changes to global variables when it is called. This in effect provides a layer of abstraction for the method, allowing it to be independent of any concrete variable in the script. Additionally, the script no longer downloads a copy of the script from the URL that is passed as an argument. It instead reads the file from the source, parses the JSON, and continues processing the data from the Hash to retrieve the names and times of the fastest and slowest resources. If the argument passes cannot be parsed as a JSON, the program terminates and indicates that the input was invalid. This portion effectively follows the KISS principle, at least in comparison to the original script.

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