-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.rb
60 lines (49 loc) · 1.84 KB
/
reader.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
def import(file)
# Проверям имя файла на корректность
return puts 'Invalid name of file!' unless File.exist?(file)
# Открываем файл и читаем построчно
File.open(file).each do |io|
# Получяем код ответа для текущей строки
code_status = io.scan(/up_status="([\d]*)"/)[0] * " "
# Заполняем массив временем ответа на успешные запросы
if code_status.to_i == 200
resp_time = io.scan(/x_resp_time="([\d]*.[\d]*)ms"/)[0] * " "
$good_time.push(resp_time.to_f)
end
# Заполняем массив кодами ответов от неуспешных запросов
if code_status.to_i != 200
$bad_status.push(code_status.to_i)
end
end
end
def print
# Создаем хэш для хранения кол-ва неупешных запросов
bad_h = Hash.new
$bad_status.uniq.each do |i|
bad_h.store(i, 0)
end
# Заполняем хэш
$bad_status.each do |i|
bad_h.each do |key, value|
if i == key
bad_h[key] = value + 1
end
end
end
good_counter = $good_time.size
bad_counter = $bad_status.size
all_counter = good_counter + bad_counter
avg_time = ($good_time.inject(0){ |sum, i| sum + i }.to_f) / good_counter
# Выводим отчет по запросам
puts "#{bad_counter} out of #{all_counter} requests returned non 200 code:"
bad_h.each do |key, value|
puts "#{key} - #{value}"
end
puts "Average response with 200 code: #{avg_time.to_i}ms from #{good_counter} requests."
end
$bad_status = Array.new
$good_time = Array.new
# Если не аргумент из консоли - берем значение по-умолчанию
ARGV[0] = 'nginx.txt' if ARGV[0].nil?
import(ARGV[0].to_s)
print_res