Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created October 31, 2011 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tagomoris/1327240 to your computer and use it in GitHub Desktop.
Save tagomoris/1327240 to your computer and use it in GitHub Desktop.
continuous write over hoop
#!/usr/bin/env ruby
source_file = ARGV.shift
target_host = ARGV.shift
target_port = ARGV.shift
target_path = ARGV.shift
username = ARGV.shift
lasting_seconds = ARGV.shift.to_i
# create
# curl -X POST -s 'http://deliver101.att.scribe.admin:14000/tmp/put7.txt?op=create&user.name=edge-dev'
# --data-binary @10mdata.txt --header 'content-type: application/octet-stream'
# append
# curl -X PUT -s 'http://deliver101.att.scribe.admin:14000/tmp/put7.txt?op=append&user.name=edge-dev'
# --data-binary @10mdata.txt --header 'content-type: application/octet-stream'
chunk = open(source_file){|f| f.read(10*1024*1024)} # 10MB or filesize
chunk.force_encoding("ASCII-8BIT")
header = {'Content-Type' => 'application/octet-stream'}
require 'net/http'
client = Net::HTTP.new(target_host, target_port.to_i)
start_at = Time.now
response = client.request_post(target_path + '?op=create&user.name=' + username, chunk, header)
if response.code.to_i >= 400
raise RuntimeError, "failed to post new file with code #{response.code}: #{target_host}:#{target_port}#{target_path}\n#{response.message}"
end
counter = 1 # 1 means 'create'
failed_count = 0
while Time.now - start_at < lasting_seconds
response = client.request_put(target_path + '?op=append&user.name=' + username, chunk, header)
counter += 1
if response.code.to_i >= 400
failed_count += 1
end
end
warn "written chunk:#{counter}, failed:#{failed_count}"
rate = (chunk.length * 8 * (counter - failed_count) / (1000 * lasting_seconds)).floor / 1000
warn "rate: #{rate} Mbps"
exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment