Skip to content

Instantly share code, notes, and snippets.

@sakirtemel
Last active December 22, 2022 02:09
Show Gist options
  • Save sakirtemel/1816d607ef0ee409bdc36935a95986c6 to your computer and use it in GitHub Desktop.
Save sakirtemel/1816d607ef0ee409bdc36935a95986c6 to your computer and use it in GitHub Desktop.
# wget https://dlcdn.apache.org//jmeter/binaries/apache-jmeter-5.5.zip
# unzip apache-jmeter-5.5.zip
# ruby load_testing_for_uploading.rb
# results(takes some time to generate html output): apache-jmeter-5.5/bin/jmeter -g ./results.jtl -o ./results
# npx http-server results
require 'ruby-jmeter'
require 'uri'
module URI
def self.encode(url)
url
end
def self.decode(url)
url
end
end
# all options our test plan can use, like filenames__1 is foo1.pdf, filenames_11 is foo1.xls
def user_variables
arr = []
index = 1
(%w(.pdf .xls .docx .zip .jpg .) + ['']).each do |ext|
(1..10).each do |k|
arr << {name: "filenames__#{index}", value: "foo#{k}#{ext}"}
arr << {name: "mimetypes__#{index}", value: "application/#{ext}"}
index = index + 1
end
end
arr
end
FileUtils.rm('jmeter.log') if File.exist?('jmeter.log')
FileUtils.rm('results.jtl') if File.exist?('results.jtl')
test do
threads count: 100, duration: 120 do # if 100 customers are keep uploading new files in 2 minutes
think_time 2000, 5000 # at least 2 seconds between uploads of a customer, up to 5 seconds
variables user_variables # hash with all options our test plan can use
transaction name: 'Getting the folder code' do
post name: 'Get session', url: 'https://api.foldercode.com/api/v1/uploading_sessions' do
duration_assertion duration: 1000 # even 1 second is too much, the response should be less than that
json_path_postprocessor referenceNames: 'id', jsonPathExprs: '.id', match_numbers: 1
json_path_postprocessor referenceNames: 'folder_code', jsonPathExprs: '.folderCode', match_numbers: 1
end
end
exists 'id' do
# this code to record the session ids we're using, can be helpful to delete only the files we uploaded
bsh_post script: <<-EOS.strip_heredoc
artname = vars.get("folder_code");
f = new FileOutputStream("obtained_folder_codes.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(artname);
f.close();
EOS
loops count: "${__Random(1,10)}" do # upload from 1 to 10 files per folder
submit url: "https://api.foldercode.com/upload", fill_in: {"browserSession": "${id}"}, files: [{path: '/tmp/${__V(filenames__${__Random(1,70)})}', paramname: 'file', mimetype: '${__V(mimetypes__${__Random(1,70)})}'}], 'DO_MULTIPART_POST' => true
end
end
end
end.run(
path: './apache-jmeter-5.5/bin/',
file: 'jmeter.jmx',
log: 'jmeter.log',
jtl: 'results.jtl'
)
# ruby code to generate random files in different sizes and extensions
# require 'securerandom'; (%w(.pdf .xls .docx .zip .jpg .) + ['']).each { |ext| 10.times { |i| File.open("/tmp/foo#{i+1}#{ext}", 'wb') { |f| (i<=5 ? rand(0.2..5.3) : rand(5.4..99.3)).to_i.times { f.write( SecureRandom.random_bytes( 1024 * 1024 ) ) } } } }
#
# to see which files are uploaded by this script
# require 'csv'; CSV.read('obtained_folder_codes.csv').flatten.uniq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment