-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_load_tester.py
100 lines (84 loc) · 4.2 KB
/
test_load_tester.py
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
90
91
92
93
94
95
96
97
98
99
100
# run with
# $ source activate ipykernel_py37
# $/anaconda3/envs/ipykernel_py37/bin/pytest
import datetime
import dateutil
from unittest.mock import patch, Mock
from load_tester import ElasticsearchRequestLogParser, LoadTester, Request
@patch('load_tester.ElasticsearchRequestLogParser.lines_of_log_file')
def test_ElasticsearchRequestLogParser(mock_lines_of_log_file):
log_lines = r"""[2018-08-31 09:23:03,245][TRACE][index.search.slowlog.query] [discovery_events_v_20171116]took[33.1ms], took_millis[33], types[], stats[], search_type[QUERY_AND_FETCH], total_shards[1], source[{"a": "apple", "b": "banana"}], extra_source[{"b": "bread", "c": "coconut"}],
[2018-08-31 09:23:04,245][TRACE][index.search.slowlog.query] [discovery_events_v_20171116]took[33.1ms], took_millis[33], types[], stats[], search_type[QUERY_AND_FETCH], total_shards[1], source[{"a": "apple", "b": "banana"}], extra_source[{"b": "bread", "c": "coconut"}], """
def lines():
for line in log_lines.split('\n'):
yield line
mock_lines_of_log_file.side_effect = lines
es_request_log_parser = ElasticsearchRequestLogParser.__new__(ElasticsearchRequestLogParser)
requests = []
for request in es_request_log_parser:
assert request.method == 'POST'
assert request.url_path == '/discovery_events_v_20171116/_search'
assert request.json_body == {'a': 'apple', 'b': 'bread', 'c': 'coconut'}
requests.append(request)
assert requests[0].timestamp == datetime.datetime(2018, 8, 31, 9, 23, 3)
assert requests[1].timestamp == datetime.datetime(2018, 8, 31, 9, 23, 4)
@patch('load_tester.datetime')
def test_ElasticsearchRequestLogParser_timing(mock_datetime):
"""
Each request is exactly two minutes apart for simplicity.
run the request simulation run at speed_multiplier 2
we start the simulation at 2017-10-17 01:00:00 so the correct times to receive the first 3 requests are
2017-10-17 01:00:00
2017-10-17 01:01:00
2017-10-17 01:02:00
the 4th request is generated by restarting the request generator and we just assume that it occurs at the same
time as the 3rd request
so the 4th and 5th correct times should be
2017-10-17 01:02:00
2017-10-17 01:03:00
What we test here is that the sleep_time is correct, we do this by mocking out the datetime.now() function.
"""
mock_datetime.timedelta.return_value = datetime.timedelta(0)
mock_datetime.datetime.min = datetime.datetime.min
def requests_generator():
requests = [
Request(
timestamp=dateutil.parser.parse('1980-10-05 00:08:00'),
method='POST',
url_path='/things/_search',
json_body={'a': 'b'},
),
Request(
timestamp=dateutil.parser.parse('1980-10-05 00:10:00'),
method='POST',
url_path='/things/_search',
json_body={'a': 'b'},
),
Request(
timestamp=dateutil.parser.parse('1980-10-05 00:12:00'),
method='POST',
url_path='/things/_search',
json_body={'a': 'b'},
)
]
for request in requests:
yield request
load_tester = LoadTester(
loop=None,
request_generator_factory=lambda :requests_generator(),
response_accumulator=Mock(),
host='elasticsearch',
port='9200',
speed_multiplier=2,
)
requests = load_tester._requests()
mock_datetime.datetime.now.return_value = dateutil.parser.parse('2017-10-17 01:00:00')
assert next(requests)['sleep_time'] == 0.0
mock_datetime.datetime.now.return_value = dateutil.parser.parse('2017-10-17 01:00:30')
assert next(requests)['sleep_time'] == 30.0
mock_datetime.datetime.now.return_value = dateutil.parser.parse('2017-10-17 01:02:00')
assert next(requests)['sleep_time'] == 0.0
mock_datetime.datetime.now.return_value = dateutil.parser.parse('2017-10-17 01:02:30')
assert next(requests)['sleep_time'] == -30.0
mock_datetime.datetime.now.return_value = dateutil.parser.parse('2017-10-17 01:03:00')
assert next(requests)['sleep_time'] == 0.0