Instantly share code, notes, and snippets.

@ohsawa0515 /Gemfile forked from mirakui/Gemfile
Last active May 24, 2018

Embed
What would you like to do?
AWS S3 read/write Benchmark
source :gemcutter
gem 'aws-sdk', '~> 2'
# s3bench.rb
#
# USAGE:
# $ bundle install
# $ ruby s3bench.rb S3_BUCKET [N] [REGION] [SIZE]
#
require "rubygems"
require "benchmark"
require "bundler"
Bundler.setup
require 'aws-sdk'
BUCKET_NAME = ARGV.shift
N = (ARGV.shift || 1).to_i
REGION = ARGV.shift || "ap-northeast-1"
SIZE = ARGV.shift || "1M"
def dummy_filename
"./s3bench-dummy"
end
def filename(i)
"s3bench-#{i}"
end
def local_filename(i)
"./#{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
bucket = Aws::S3::Resource.new(region: REGION).bucket(BUCKET_NAME)
`dd if=/dev/zero of=#{dummy_filename} count=1 bs=#{SIZE}`
bench("upload") do |i|
bucket.object(remote_filename(i)).put(:body => File.open(dummy_filename))
end
bench("download") do |i|
File.open(local_filename(i), 'w') do |file|
bucket.object(remote_filename(i)) do |chunk|
file.write chunk
end
end
File.delete local_filename(i)
end
bench("exists?") do |i|
bucket.object(remote_filename(i)).exists?
end
bench("delete") do |i|
bucket.object(remote_filename(i)).delete
end
File.delete dummy_filename
@ohsawa0515

This comment has been minimized.

Owner

ohsawa0515 commented May 24, 2018

$ bundle install --path vendor/bundle
$ bundle exec ruby s3bench.rb S3_BUCKET [N] [REGION] [SIZE(e.g. 10M)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment