Created
January 29, 2015 17:53
-
-
Save davingee/63be9dd46f8ed569b5e1 to your computer and use it in GitHub Desktop.
parser
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
require "rubygems" | |
require "time" | |
require 'oj' | |
require "awesome_print" | |
require 'pry' | |
class Parser | |
attr_accessor :info_hash | |
def initialize | |
self.info_hash = Hash.new{|k,v| k[v] = {} } | |
# info_hash[ :average_scan ][ :count ] = json.count | |
info_hash[ :script_time ] = 0 | |
info_hash[ :average_scan ][ :times ] = 0 | |
info_hash[ :fastest_scan ][ :time ] = 1000000000 | |
info_hash[ :slowest_scan ][ :time ] = 0 | |
info_hash[ :fastest_average_scan ][ :time ] = 1000000000 | |
info_hash[ :slowest_average_scan ][ :time ] = 0 | |
info_hash[ :errors ] = [] | |
end | |
def run | |
count = 0 | |
Oj.load( File.read( "#{Dir.pwd}/times.json" ) ).each do |row| | |
begin | |
start_time = Time.parse( row[ 'start' ] ) | |
end_time = Time.parse( row[ 'end' ] ) | |
if start_time >= end_time | |
# not sure what you want to do with start_times that are > end_time; count them?? | |
info_hash[ :errors ] << row.merge!( error: "Start Time is in the future of End Time! Wait is this possible?" ) | |
next | |
end | |
scan_time = end_time - start_time | |
node_id = row[ 'node_id' ] | |
info_hash[ :average_scan ][ :times ] += scan_time | |
if info_hash[ :fastest_scan ][ :time ] > scan_time | |
info_hash[ :fastest_scan ][ :time ] = scan_time | |
info_hash[ :fastest_scan ][ :node_id ] = node_id | |
end | |
if info_hash[ :slowest_scan ][ :time ] < scan_time | |
info_hash[ :slowest_scan ][ :time ] = scan_time | |
info_hash[ :slowest_scan ][ :node_id ] = node_id | |
end | |
ip = row[ 'ip' ] | |
info_hash[ :ips ][ ip ] ||= {} | |
info_hash[ :ips ][ ip ][ :node_ids ] ||= [] | |
info_hash[ :ips ][ ip ][ :node_ids ] << node_id | |
info_hash[ :ips ][ ip ][ :count ] ||= 0 | |
info_hash[ :ips ][ ip ][ :count ] += 1 | |
info_hash[ :ips ][ ip ][ :time ] ||= 0.0 | |
info_hash[ :ips ][ ip ][ :time ] += scan_time | |
info_hash[ :ips ][ ip ][ :average_time ] = info_hash[ :ips ][ ip ][ :time ] / info_hash[ :ips ][ ip ][ :count ] | |
count += 1 | |
rescue => e | |
info_hash[ :errors ] << row.merge!( error: e ) | |
end | |
end | |
info_hash[ :ips ].each do |ip, hash| | |
if info_hash[ :fastest_average_scan ][ :time ] > hash[:average_time] | |
info_hash[ :fastest_average_scan ][ :time ] = hash[:average_time] | |
info_hash[ :fastest_average_scan ][ :ip ] = ip | |
end | |
if info_hash[ :slowest_average_scan ][ :time ] < hash[:average_time] | |
info_hash[ :slowest_average_scan ][ :time ] = hash[:average_time] | |
info_hash[ :slowest_average_scan ][ :ip ] = ip | |
end | |
end | |
info_hash[ :average_scan ][ :time ] = info_hash[ :average_scan ][ :times ] / count | |
info_hash[ :average_scan ].delete(:times) | |
info_hash.delete(:ips) | |
end | |
def benchmark | |
start_time = Time.now | |
yield | |
end_time = Time.now | |
info_hash[ :script_time ] = end_time - start_time | |
ap info_hash | |
end | |
parser = Parser.new | |
parser.benchmark{ | |
# file_path = "#{Dir.pwd}/times.json" | |
# open(file_path).each_line do |line| | |
# Script took 211.501514 seconds to run. | |
# File.open(file_path).each_line {|line| puts line} | |
# Script took 10.391365 seconds to run. | |
# json = Oj.load( File.open( file_path, 'r' ).read ) | |
# open(file_path).each_line {|line| puts line} | |
# Script took 9.590031 seconds to run. | |
# IO.foreach(file_path) {|line | puts line} | |
# Script took 9.819243 seconds to run. | |
# json = Oj.load( open( file_path, 'r' ).read ) | |
# Script took 2.610534 seconds to run. | |
# json = JSON.parse( open( file_path, 'r' ).read ) | |
# Script took 5.371979 seconds to run. | |
# open(file_path).each_line do |line| | |
# Script took 211.501514 seconds to run. | |
# self.json = Oj.load( File.open( file_path, 'r' ).read ) | |
# json.each do |row| | |
# Script took 92.022452 seconds to run. | |
# Script took 94.765778 seconds to run. | |
# Oj.load( File.open( file_path, 'r' ).read ).each { | line | puts line } | |
# Oj.load( File.read( file_path ) ).each { | line | puts line } | |
# tail_command = "tail -n +0 #{ file_path }" | |
# %x[ #{tail_command} ] | |
# %x[ grep -n #{ file_path } ] | |
parser.run | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment