-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
301 lines (240 loc) · 8.54 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""Main file for starting Felt.
Handles input parsing, checking parameters and starting the workload run.
"""
import argparse
import subprocess
import sys
import time
import json
import os.path
import string
import random
import commentjson
import copy
from Queue import Queue, Empty
from threading import Thread
__license__ = "MIT"
__maintainer__ = "Samuel Vandamme"
__email__ = "[email protected]"
__author__ = "Samuel Vandamme"
__credits__ = ["Stijn Polfliet", "Samuel Vandamme", "Hatem Mostafa"]
__version__ = "alpha"
threadQueue = Queue()
def main(args):
"""Main function.
The main function parses the command line arguments, reads the input file
and starts the generator.
"""
# Parse arguments
options = parse_arguments(args)
# Load in scenario
with open(options['scenario'], 'r') as content_file:
content = content_file.read()
scenario = commentjson.loads(content)
# Test option
if options['test']:
options['threads'] = 1
# Output information
if options['verbose']:
print "################################"
print "\tFelt (%s)" % __version__
print "################################"
print
print options
print
print "################################"
# Create watchdog thread
if options['maxTime'] > 0:
import os
def watchdog(sec):
""" Stops the process after x seconds. """
time.sleep(sec)
os._exit(0)
Thread(target=watchdog, args=(options['maxTime'],)).start()
# Start worker
worker = WebworkerService()
worker.run(scenario, options)
def parse_arguments(args):
"""Parse arguments function.
Takes input from commandline and returns an options array.
"""
# Parse arguments
parser = argparse.ArgumentParser(description='Start workload.')
parser.add_argument('--debug', action='store_true',
help="enable debug information")
parser.add_argument('--verbose', action='store_true',
help="makes generator more verbose")
parser.add_argument('--threads', type=int, default=5,
help="number of threads to run simultaneously")
parser.add_argument('--test', action='store_true',
help="run a scenario only once")
parser.add_argument('--slimerjs', action='store_true',
help="use slimerjs instead of phantomjs")
parser.add_argument('--screenshot', action='store_true',
help="provide screenshots after each step")
parser.add_argument('--user-agent', type=str, dest='userAgent',
help="provide a custom User-Agent")
parser.add_argument('--max-time', type=int, default=0, dest='maxTime',
help="provide a maximum runtime")
parser.add_argument('scenario')
args = parser.parse_args()
# Which browser are we using
browser = "phantomjs"
if args.slimerjs:
browser = "slimerjs"
# Check if scenario exists
if not os.path.isfile(args.scenario):
print "scenario '%s' not found" % args.scenario
return
# Run options
options = {
'browser': browser,
'scenario': args.scenario,
'threads': args.threads,
'verbose': args.verbose,
'debug': args.debug,
'test': args.test,
'screenshot': args.screenshot,
'userAgent': args.userAgent,
'maxTime': args.maxTime
}
return options
def which(program):
"""Which function.
Used to find the correct location of the browser executable.
"""
import os
# Authors: Jay, Harmv
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
fpath, fname = os.path.split(program)
# Special case if file is in current dir
if is_exe(fname):
return "./%s" % program
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(os.path.expanduser(path), program)
if is_exe(exe_file):
return exe_file
return None
class WebworkerService:
"""WebworkerService class."""
def run(self, scenario, options):
"""Run function.
Init run of main workload loop
"""
self.threadcount = 0
self.threadstarted = 0
self.running = True
# Start new one every minute
while self.running:
self.startRun(scenario, options)
# Keep track of running threads
try:
while True:
threadQueue.get(False)
self.threadcount -= 1
threadQueue.task_done()
# Test mode
if options["test"]:
self.running = False
except Empty:
pass
time.sleep(0.25)
def startRun(self, scenario, options):
"""Initiate run."""
if (options['threads'] > self.threadcount):
# Start threads
for x in range(options['threads'] - self.threadcount):
self.threadcount += 1
self.threadstarted += 1
Thread(
target=execute,
args=(self.threadstarted, scenario, options, )
).start()
def execute(threadId, scenario, options):
"""Execute browser thread with options."""
command = [
options['browser'],
'worker.js',
str(threadId),
json.dumps(preprocessScenario(scenario)),
json.dumps(options)
]
process = subprocess.Popen(
command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
# Main loop
while True:
nextline = process.stdout.readline()
sys.stdout.write(nextline)
sys.stdout.flush()
if process.poll() is not None:
threadQueue.put("Something")
break
return None
def preprocessScenario(obj):
"""Preprocess the scenario so that the variables are filled in."""
variables = copy.deepcopy(obj['variables'])
steps = copy.deepcopy(obj['steps'])
# Loop all variables
for idx in range(len(variables)):
# Current variable
variable = variables[idx]
# Type
varType = variable['type']
searchForExactMatch = False
# Fetch value of variable
if varType == 'randomString':
value = getRandomString(variable['length'])
elif varType == 'constant':
value = variable['value']
if isinstance(value, list):
# We will be searching for exact match in case of arrays
# because we will replace the whole variable.
searchForExactMatch = True
# Error message in case unknown type
else:
value = None
print 'Unknown variable type `' + variable['type'] + '`'
# If valid variable
if value is not None:
# Replacing the variable name with
# its value in the following variables.
# String to find
vsyntax = '$[' + variable['name'] + ']'
# Loop variables after current one
for idx2 in range(idx + 1, len(variables)):
variable2 = variables[idx2]
if 'value' in variable2:
if searchForExactMatch:
if variable2['value'] == vsyntax:
variable2['value'] = value
else:
variable2['value'] = json.loads(
json.dumps(variable2['value'])
.replace(vsyntax, value))
# Replacing the variable name with its value in the steps.
for step in steps:
if 'value' in step:
if searchForExactMatch:
if step['value'] == vsyntax:
step['value'] = value
else:
step['value'] = json.loads(
json.dumps(step['value'])
.replace(vsyntax, value))
return steps
def getRandomString(size=6, chars=string.ascii_lowercase + string.digits):
"""Generate random string."""
return ''.join(random.choice(chars) for x in range(size))
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))