Skip to content

Instantly share code, notes, and snippets.

@natewalck
Created December 5, 2012 16:44
Show Gist options
  • Save natewalck/4217275 to your computer and use it in GitHub Desktop.
Save natewalck/4217275 to your computer and use it in GitHub Desktop.
Script to test server speed
#!/usr/bin/ruby
require 'rubygems'
require 'mac-network/interface'
require 'mac-network/location'
include Mac::Network
require 'osx/cocoa'
require 'socket'
require 'time'
file="test1000mb"
timeout=15
servers_plist = File.expand_path("/Library/Preferences/com.company.servers.plist")
replica_list = OSX::NSMutableDictionary.dictionaryWithContentsOfFile(servers_plist)
results_plist = File.expand_path("/Library/Preferences/com.company.serverstats.plist")
server_results = OSX::NSMutableDictionary.dictionaryWithContentsOfFile(results_plist)
if server_results == nil then
server_results = OSX::NSMutableDictionary.alloc.init
end
if not system('/usr/local/bin/on_network.sh') then
puts "Not on the company network"
exit
end
i = Location.current.services.first.interface
local_ip = OSX::SCDynamicStoreCopyValue(Interface.dynamic_store, "State:/Network/Interface/#{i.bsd_name}/IPv4")["Addresses"].first
def check_server_speed(test_server, test_file, timeout_value)
if File.exists?("/tmp/#{test_file}") then
File.delete("/tmp/#{test_file}")
end
timer = 0
start = Time.now
download_pid = fork { `curl -s http://#{test_server}/#{test_file} -o /tmp/#{test_file}` }
while Time.now - start <= timeout_value do
begin
Process.getpgid(download_pid)
rescue Errno::ESRCH
break
end
timer += 1
sleep 1
end
return File.size("/tmp/#{test_file}") / timer
end
benchmark_results = replica_list['servers'].map do |server|
download_speed = check_server_speed(server, file, timeout)
interface_type = :wireess_ip
interface_type = :wired_ip if Location.current.services.first.interface.wired?
{:name => server, :speed => download_speed, interface_type => local_ip, :run_date => Time.now.utc.iso8601}
end
server_results["serverstats"] = benchmark_results
server_results["serverstats"].sort! {|a,b| b[:speed] <=> a[:speed] }
server_results.writeToFile_atomically(results_plist,true)
if File.exists?("/tmp/#{file}") then
File.delete("/tmp/#{file}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment