-
-
Save ohsawa0515/02d96fb7c1d4fac33a14d1080d567438 to your computer and use it in GitHub Desktop.
AWS S3 read/write Benchmark
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 'aws-sdk', '~> 2' |
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 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 |
Author
ohsawa0515
commented
May 24, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment