Created
December 22, 2011 09:32
-
-
Save tagomoris/1509687 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'net/http' | |
(host,port,username,path) = ARGV | |
port = port.to_i | |
data = '============================================================================================================================================================================================================================================================================================================' | |
$i = 0 | |
def gen_head(format) | |
$i += 1 | |
sprintf(format, $i) | |
end | |
pid = fork | |
format = if pid | |
'parent%08d' | |
else | |
'child%08d' | |
end | |
conn = Net::HTTP.start(host, port) | |
res = conn.request_get("/?op=status&user.name=#{username}") | |
conn.finish | |
unless res.code.to_i < 300 and res['Set-Cookie'] | |
raise RuntimeError, "failed to accces Hoop Server #{host}:#{port} with username #{username}" | |
end | |
authorized_header = {'Cookie' => res['Set-Cookie'].split(';')[0], 'Content-Type' => 'application/octet-stream'} | |
end_at = Time.now.to_i + 60 | |
while Time.now.to_i < end_at | |
chunk = (0..100).map{|i| gen_head(format) + "\t" + data + "\n"}.join | |
conn = Net::HTTP.start(host, port) | |
conn.read_timeout = 5 | |
res = conn.request_put(path + "?op=append", chunk, authorized_header) | |
if res.code == '404' | |
res = conn.request_post(path + "?op=create&overwrite=false", chunk, authorized_header) | |
end | |
while res.code == '500' | |
sleep 0.5 | |
puts "failing request... at " + (pid ? 'parent' : 'child') | |
res = conn.request_put(path + "?op=append", chunk, authorized_header) | |
end | |
if res.code != '200' and res.code != '201' | |
raise RuntimeError, "failed to send data to Hoop Server, with code:#{res.code}" | |
end | |
conn.finish | |
sleep 0.05 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment