Skip to content

Instantly share code, notes, and snippets.

@ohsawa0515
Forked from mirakui/Gemfile
Last active May 24, 2018 02:47
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 ohsawa0515/02d96fb7c1d4fac33a14d1080d567438 to your computer and use it in GitHub Desktop.
Save ohsawa0515/02d96fb7c1d4fac33a14d1080d567438 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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