-
-
Save mrkvm/5759039 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
source :gemcutter | |
gem 'pit' | |
gem 'sauberia-aws-s3' |
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
# s3bench.rb | |
# | |
# USAGE: | |
# $ bundle install | |
# $ ruby s3bench.rb [N] | |
# | |
require "rubygems" | |
require "benchmark" | |
require "bundler" | |
Bundler.setup | |
require "aws/s3" | |
require "pit" | |
include AWS::S3 | |
CONFIG = Pit.get('s3bench', :require => { | |
'access_key_id' => '', | |
'secret_access_key' => '', | |
'bucket_name' => '' | |
}) | |
BUCKET_NAME = CONFIG['bucket_name'] | |
N = (ARGV.shift || 1).to_i | |
def dummy_filename | |
"/tmp/s3bench-dummy" | |
end | |
def filename(i) | |
"s3bench-#{i}" | |
end | |
def local_filename(i) | |
"/tmp/#{filename(i)}" | |
end | |
def remote_filename(i) | |
"s3bench/#{filename(i)}" | |
end | |
def bench(title) | |
puts "\nBenchmark: #{title} (#{N} times)" | |
Benchmark.bm(7, ">total:", ">avg:") do |bm| | |
results = [] | |
N.times do |i| | |
results << bm.report(i.to_s) do | |
yield i | |
end | |
end | |
total = results.inject {|sum, t| sum+=t} | |
[total, total/N] | |
end | |
end | |
Base.establish_connection!( | |
:access_key_id => CONFIG['access_key_id'], | |
:secret_access_key => CONFIG['secret_access_key'], | |
:server => "#{BUCKET_NAME}.s3.amazonaws.com" | |
) | |
`dd if=/dev/zero of=#{dummy_filename} count=1 bs=5m` | |
bench("upload") do |i| | |
S3Object.store( | |
remote_filename(i), | |
open(dummy_filename), | |
BUCKET_NAME | |
) | |
end | |
bench("download") do |i| | |
open(local_filename(i), 'w') do |file| | |
S3Object.stream(remote_filename(i), BUCKET_NAME) do |chunk| | |
file.write chunk | |
end | |
end | |
File.delete local_filename(i) | |
end | |
bench("exists?") do |i| | |
S3Object.exists? remote_filename(i), BUCKET_NAME | |
end | |
bench("delete") do |i| | |
S3Object.delete remote_filename(i), BUCKET_NAME | |
end | |
File.delete dummy_filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment