-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplayer.py
executable file
·61 lines (52 loc) · 1.48 KB
/
replayer.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
#!/usr/bin/env python
import argparse
import sys
import dateutil.parser
import requests
from urlparse import urlparse
from twisted.internet import task
from twisted.internet import reactor
description='ELB Log Replayer (ELR)'
parser = argparse.ArgumentParser(description=description)
parser.add_argument('logfile', help='the logfile to replay')
parser.add_argument(
'--host',
help='host to send requests',
default='localhost',
)
parser.add_argument(
'--dry-run',
action='store_true',
help='don\'t actually hit the `host`',
)
script_args = parser.parse_args()
def replay_request(url):
if script_args.dry_run:
sys.stdout.write('{}\n'.format(url))
else:
requests.get(url)
def main():
starting = None
for line in open(script_args.logfile):
bits = line.split()
timestamp = dateutil.parser.parse(bits[0])
if not starting:
starting=timestamp
offset = timestamp - starting
if offset.total_seconds() < 0:
# ignore past requests
continue
method = bits[11].lstrip('"')
url = urlparse(bits[12])
if method != 'GET':
continue
request_path = 'http://{}{}{}'.format(
script_args.host,
url.path,
url.query
)
reactor.callLater(offset.total_seconds(), replay_request, request_path)
reactor.callLater(offset.total_seconds() + 4, reactor.stop)
reactor.run()
if __name__ == "__main__":
main()