This repository has been archived by the owner on Aug 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcore.py
160 lines (128 loc) · 4.19 KB
/
core.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
"""Core files for Felt.
Handles browser instances
"""
import os
import time
from threading import Thread
import json
from Queue import Queue, Empty
import subprocess
__license__ = "MIT"
__maintainer__ = "Samuel Vandamme"
__email__ = "[email protected]"
__author__ = "Samuel Vandamme"
__credits__ = ["Stijn Polfliet", "Samuel Vandamme", "Hatem Mostafa"]
__version__ = "alpha"
class Felt:
"""Felt class."""
def __init__(self, options, scenario):
"""Init vars."""
self.scenario = scenario
self.options = options
def run(self):
"""Start Felt run and execute watchdog."""
if self.options.isVerbose():
print "################################"
print "\tFelt (%s)" % __version__
print "################################"
if self.options.getMaximumExectionTime() > 0:
self.initWatchdog()
worker = WebworkerService()
return worker.run(self.scenario, self.options)
def initWatchdog(self):
"""Init watchdog and kill thread after x seconds."""
def watchdog(sec):
""" Stops the process after x seconds."""
time.sleep(sec)
os._exit(0)
Thread(
target=watchdog,
args=(self.options.getMaximumExectionTime(),)
).start()
# Initiate thread and dataqueue
threadQueue = Queue()
dataQueue = Queue()
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.isTest():
self.running = False
except Empty:
pass
time.sleep(0.25)
# Parse data coming from threads
data = []
parsedRows = ""
try:
while True:
rawData = dataQueue.get(False)
parsedRows = json.loads(rawData)
for row in parsedRows:
# We need to decode the step string
row['step'] = json.loads(row['step'])
data.append(parsedRows)
dataQueue.task_done()
except Empty:
pass
except ValueError:
if options.isDebug():
raise ValueError(
"Unable to parse data coming from worker: " + rawData
)
else:
raise ValueError("Unable to parse data coming from worker")
return data
def startRun(self, scenario, options):
"""Initiate run."""
if (options.getThreads() > self.threadcount):
# Start threads
for x in range(options.getThreads() - self.threadcount):
self.threadcount += 1
self.threadstarted += 1
Thread(
target=WebworkerService.execute,
args=(self.threadstarted, scenario, options, )
).start()
@staticmethod
def execute(threadId, scenario, options):
"""Execute browser thread with options."""
command = [
options.getBrowser(),
'worker.js',
str(threadId),
json.dumps(scenario.preprocessScenario()),
json.dumps(options.getRunnerOptions())
]
print command
process = subprocess.Popen(
command,
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT
)
# Main loop
data = ""
while True:
nextline = process.stdout.readline()
data += nextline
if process.poll() is not None:
dataQueue.put(data)
threadQueue.put("Something")
break
return None