Last active
November 29, 2018 20:12
-
-
Save Bobochka/5a5e4dfcc3f6ce209954e188cec69f33 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| require 'aws-sdk-s3' | |
| require 'benchmark' | |
| def percentile(values_sorted, percentile) | |
| k = (percentile*(values_sorted.length-1)+1).floor - 1 | |
| f = (percentile*(values_sorted.length-1)+1).modulo(1) | |
| return values_sorted[k] + (f * (values_sorted[k+1] - values_sorted[k])) | |
| end | |
| def parse_params | |
| if ARGV.size != 3 && ARGV.size != 4 | |
| p 'usage: bundle exec ruby s3.rb <region> <bucket> <path> <optional_sample_size>' | |
| exit(1) | |
| end | |
| region = ARGV.shift | |
| p "Region: #{region}" | |
| bucket = ARGV.shift | |
| p "Bucket: #{bucket}" | |
| path = ARGV.shift | |
| p "Path: #{path}" | |
| size = ARGV.size == 1 && ARGV.shift.to_i || 30 | |
| p "Sample size: #{size}" | |
| [region, bucket, path, size] | |
| end | |
| region, bucket, path, size = parse_params | |
| logger = Logger.new(STDOUT) | |
| logger.level = Logger::DEBUG | |
| options = { region: region, http_open_timeout: 3, http_read_timeout: 15, log_level: 'error', credentials: Aws::InstanceProfileCredentials.new(retries: 5) } | |
| s3 = ::Aws::S3::Resource.new(options) | |
| obj = s3.bucket(bucket) | |
| def f(value) | |
| sprintf('%.3fms', value) | |
| end | |
| 10.times.map do | |
| Thread.new do | |
| samples = [] | |
| i = 0 | |
| loop do | |
| i += 1 | |
| samples << Benchmark.realtime do | |
| begin | |
| obj.object(path).get | |
| rescue StandardError => e | |
| p "#{Time.now} Exception: #{e}" | |
| 0 | |
| end | |
| end * 1000 | |
| if i % size == 0 | |
| samples = samples.select{|e| e.is_a?(Float) && e > 0 }.sort | |
| str = "#{Time.now} Sampled #{samples.size}: " | |
| { | |
| 'min' => f(samples.first), | |
| 'max' => f(samples.last), | |
| '50p' => f(percentile(samples, 0.5)), | |
| '75p' => f(percentile(samples, 0.75)), | |
| '95p' => f(percentile(samples, 0.95)), | |
| }.each do |k, v| | |
| str += "#{k}: #{v}; " | |
| end | |
| p str | |
| samples = [] | |
| i = 0 | |
| end | |
| end | |
| end | |
| end.map(&:join) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment