-
Notifications
You must be signed in to change notification settings - Fork 11
/
perf_test.rb
89 lines (80 loc) · 2.35 KB
/
perf_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'nokogiri'
require 'open-uri'
require 'net/http'
require 'json'
require 'peach'
SERVER_URL = 'http://localhost:3000'
# SERVER_URL = 'https://twitarr.rylath.net'
requests = [
Proc.new do |http|
response = http.request Net::HTTP::Get.new(SERVER_URL + '/posts/all')
puts "ALL: #{response.msg}"
data = JSON.parse(response.body)
photos = data['list'].map { |x| x['data']['photos'] }.flatten.compact!
photos.each do |photo|
photo_response = http.request(Net::HTTP::Get.new("#{SERVER_URL}/img/photos/sm_#{photo}"))
puts "PHOTO #{photo}: #{photo_response.msg}"
end
1 + photos.size
end,
Proc.new do |http|
response = http.request Net::HTTP::Get.new(SERVER_URL + '/posts/popular')
puts "POPULAR: #{response.msg}"
data = JSON.parse(response.body)
photos = data['list'].map { |x| x['data']['photos'] }.flatten.compact!
photos.each do |photo|
photo_response = http.request(Net::HTTP::Get.new("https://twitarr.rylath.net/img/photos/sm_#{photo}"))
puts "PHOTO #{photo}: #{photo_response.msg}"
end
1 + photos.size
end,
Proc.new do |http|
response = http.request Net::HTTP::Get.new(SERVER_URL + '/user/autocomplete?string=g')
puts "AUTOCOMPLETE: #{response.msg}"
1
end,
Proc.new do |http|
response = http.request Net::HTTP::Get.new(SERVER_URL + '/api/v1/user/auth?username=kvort&password=foobar')
puts "AUTH: #{response.msg}"
1
end
]
class Array
def average
reduce(:+) / size.to_f
end
end
if __FILE__ == $0
signal = false
lock = Mutex.new
total_count = 0
start_time = Time.now
puts 'Starting requests'
threads = 10.times.map do
Thread.new do
count = 0
while (!signal) do
Net::HTTP.start('twitarr.rylath.net', 443, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
count += requests[Random.rand requests.size].call http
end
end
lock.synchronize { total_count += count }
end
end
trap('INT') do
puts 'Shutting down'
signal = true
threads.each do |thread|
begin
thread.join
puts 'Ended correctly'
rescue => err
puts err.inspect
end
end
end_time = Time.now
puts "TOTAL COUNT: #{total_count} in time: #{end_time - start_time} seconds"
exit
end
sleep
end