Last active
June 24, 2019 11:18
-
-
Save hkilis/7023e44de955243cefab709e2a551b41 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 'net/http' | |
require 'thread' | |
require 'thwait' | |
class LoaderIoWorker | |
BASE_URL = 'https://api.loader.io/v2' | |
APP_ID = 'xxx' | |
AUTH_KEY = 'yyy' | |
AUTH_HEADER = {'loaderio-auth' => AUTH_KEY} | |
MAX_RETRIES = 10 | |
attr_accessor :domain, :tests | |
def initialize(domain = nil) | |
@tests = make_request(:get, "#{BASE_URL}/tests", {per_page: 300, page: 1}) | |
filter_domain!(domain) if domain | |
end | |
def test_ids | |
tests.map {|test| test['test_id']} | |
end | |
def domains | |
tests.map {|test| test['domain']}.uniq | |
end | |
def filter_domain!(domain) | |
@domain = domain | |
tests.select! {|test| test['domain'] == domain} | |
end | |
def run_test(test_id) | |
apply_test(test_id, "#{BASE_URL}/tests/#{test_id}/run") | |
end | |
def stop_test(test_id) | |
apply_test(test_id, "#{BASE_URL}/tests/#{test_id}/stop") | |
end | |
def run_all_for(no_times = 1) | |
start_time = Time.now | |
no_times.times do |num| | |
init_time = Time.now | |
puts "Running #{(num + 1).ordinalize} Iteration.." | |
puts run_all | |
puts "Took #{Time.now-init_time} Seconds." | |
sleep(100.seconds) unless (num == no_times-1) | |
end | |
puts "Finished all #{no_times} times in #{(Time.now-start_time)/60.0} Minutes!" | |
end | |
def run_all | |
apply_all(:run_test) | |
end | |
def stop_all | |
apply_all(:stop_test) | |
end | |
def verify_app | |
make_request(:post, "#{BASE_URL}/apps/#{APP_ID}/verify") | |
end | |
private | |
def make_request(method, url, data = {}, headers = AUTH_HEADER) | |
endpoint = URI.parse(url) | |
http = Net::HTTP.new(endpoint.host, endpoint.port) | |
http.use_ssl = true | |
case method | |
when :post | |
req = Net::HTTP::Post.new(endpoint.path, headers) | |
when :put | |
req = Net::HTTP::Put.new(endpoint.path, headers) | |
when :get | |
req = Net::HTTP::Get.new(endpoint.path, headers) | |
req.set_form_data(data) | |
end | |
response = http.request(req) | |
JSON.parse(response.body) | |
end | |
def apply_all(method, certain_test_ids = test_ids, previous_success = [], iteration = 0) | |
thread_requests = [] | |
results = [] | |
certain_test_ids.each do |certain_test_id| | |
thread = Thread.new {results << send(method, certain_test_id)} | |
thread.abort_on_exception = false | |
thread_requests << thread | |
end | |
ThreadsWait.all_waits(*thread_requests) | |
success_tests = results.select {|res| res['message'] == 'success'}.map {|res| res['test_id']} + previous_success | |
verification_needed_tests = results.select {|res| res['errors'] == ['Verification failed']} | |
already_running_tests = results.select {|res| res['errors'] == ['This test seems to be running already']} | |
to_be_retried_tests = (verification_needed_tests + already_running_tests).map {|res| res['test_id']} | |
if to_be_retried_tests.present? && (iteration < MAX_RETRIES) | |
verify_app if verification_needed_tests.present? | |
sleep(10.seconds) if already_running_tests.present? | |
return apply_all(method, to_be_retried_tests, success_tests, iteration + 1) | |
else | |
failed_tests = test_ids - success_tests | |
{'success_tests' => success_tests, 'failed_tests' => failed_tests} | |
end | |
end | |
def apply_test(test_id, url) | |
make_request(:put, url).merge({'test_id' => test_id}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment