diff options
| -rw-r--r-- | service/test/reactor/concurrent_logins.rb | 63 | 
1 files changed, 50 insertions, 13 deletions
| diff --git a/service/test/reactor/concurrent_logins.rb b/service/test/reactor/concurrent_logins.rb index 0af2c5b7..38bdac5f 100644 --- a/service/test/reactor/concurrent_logins.rb +++ b/service/test/reactor/concurrent_logins.rb @@ -5,31 +5,68 @@  # -ruby  # -pre-created users  # -# This can be run with `ruby blocking_spawner <x>` +# This can be run with `ruby concurrent_logins.rb <x>`  # where x is the number of users you want to login.  #  # It was created to measure login times internally on the application with  # varying number of users +require 'pry'  USER_PATTERN = "loadtest%d"  PASSWORD_PATTERN = "password_%d"  COUNT = ARGV[0].to_i -def curl_command(index) -  username = USER_PATTERN % [index] -  password = PASSWORD_PATTERN % [index] -  "curl --silent -X POST --data 'username=#{username}&password=#{password}' --cookie 'XSRF-TOKEN: blablabla' --header 'X-Requested-With: XMLHttpRequest' --header 'X-XSRF-TOKEN: blablabla' http://localhost:3333/login" +def header_options +  "--header 'X-Requested-With: XMLHttpRequest' --header 'X-XSRF-TOKEN: blablabla'"  end -ts = (1...(1+COUNT)).map do |ix| -  t = Thread.new do -    time = Time.now() -    `#{curl_command(ix)}` -    puts "Request time: #{Time.now() - time}" +def cookies +  "--cookie 'XSRF-TOKEN: blablabla'"  +end + +def curl_command +  "curl --silent" +end + +def login_command(user_index, username, password) +  FileUtils.rm "/tmp/user#{user_index}.cookie", force: true +  "#{curl_command} -X POST --data 'username=#{username}&password=#{password}' --cookie-jar /tmp/user#{user_index}.cookie -w '%{http_code}|%{time_total}' --output '/dev/null' #{cookies} #{header_options} http://localhost:3333/login" +end + +def check_inbox(user_index) +  "#{curl_command} --cookie /tmp/user#{user_index}.cookie http://localhost:3333" +end + +def complete_login(user_index, user, password) +  start = Time.now +  login_response = `#{login_command(user_index, user, password)}` +  status_code, total_time = login_response.split("|") +  puts "Login request #{total_time}" +  if status_code.to_i == 200 +    interstitial = Time.now +    begin +      inbox = `#{check_inbox(user_index)}` +    end until /compose-trigger/.match(inbox) +    puts "Login loading #{sprintf('%.3f',Time.now - interstitial)}" +    puts "Login total #{sprintf('%.3f',Time.now - start)}" +  else +    puts "Login failed with #{status_code} #{sprintf('%.3f',Time.now - start)}" +  end +end + +def multi_login +  ts = (1...(1+COUNT)).map do |user_index| +    t = Thread.new do +      username = USER_PATTERN % [user_index] +      password = PASSWORD_PATTERN % [user_index] +      complete_login(user_index, username, password) +    end +    sleep 1 +    t    end -  sleep 1 -  t + +  ts.each(&:join)  end -ts.each(&:join) +multi_login | 
