-
Notifications
You must be signed in to change notification settings - Fork 12
/
jmeter.py
204 lines (168 loc) · 7.63 KB
/
jmeter.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
"""
*******************
*Copyright 2017, MapleLabs, All Rights Reserved.
*
********************
"""
import json
import time
import collectd
import re
import signal
import os
import traceback
import lxml.etree as et
# user imports
import utils
from constants import *
class jmeterStats(object):
"""Plugin object will be created only once and collects utils
and available CPU/RAM usage info every interval."""
def __init__(self, interval=1, utilize_type="CPU", maximum_grep=5, process_name='*'):
"""Initializes interval."""
self.interval = DEFAULT_INTERVAL
self.buf = ''
self.path = ''
self.length = 0
def config(self, cfg):
"""Initializes variables from conf files."""
for children in cfg.children:
if children.key == INTERVAL:
self.interval = children.values[0]
if children.key == "listener_path":
self.path = children.values[0]
def get_sample(self, elem, children=()):
try:
sample = {}
#sample['allThreads'] = int(elem.get('na', 0))
sample['bytesRec'] = int(elem.get('by', 0))
#sample['dataEncoding'] = elem.get('de', '')
#sample['dataType'] = elem.get('dt', '')
sample['elapsedTime'] = int(elem.get('t', 0))
sample['errorCount'] = int(elem.get('ec', 0))
sample['groupThreads'] = int(elem.get('ng', 0))
#sample['hostname'] = elem.get('hn', '')
sample['idleTime'] = int(elem.get('it', 0))
#sample['label'] = elem.get('lb', '')
sample['latencyTime'] = int(elem.get('lt', 0))
sample['method'] = elem.findtext('method', '')
sample['queryString'] = elem.findtext('queryString', '')
sample['responseCode'] = elem.get('rc', '')
#sample['responseData'] = elem.findtext('responseData', '')
#sample['responseFilename'] = elem.findtext('responseFile', '')
#sample['responseMessage'] = elem.get('rm', '')
#sample['sampleCount'] = int(elem.get('sc', 0))
sample['success'] = bool(elem.get('s') == 'true')
sample['threadName'] = elem.get('tn', '')
sample['time_stamp'] = int(elem.get('ts', 0))
sample['url'] = elem.findtext('java.net.URL', '')
if re.match("http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[: ][0-9]*\/opportunity\/[0-9]+", sample['url']):
url_id = re.sub("/opportunity/[0-9]+", "/opportunity/[id]", sample['url'])
sample['api'] = url_id
elif re.match("http:\/\/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+[: ][0-9]*\/userDetails\/[0-9]+", sample['url']):
url_id = re.sub("/userDetails/[0-9]+", "/userDetails/[id]", sample['url'])
sample['api'] = url_id
else:
sample['api'] = sample['url']
return sample
except Exception as err:
collectd.error("Plugin jmeter: Exception in get_sample due to %s" % err)
def get_jmeter_data(self):
try:
sample_started = False
sample_children = []
parser = et.XMLPullParser(['start', 'end'])
parser.feed(self.buf)
for event, elem in parser.read_events():
if event == 'start' and elem.tag == 'sample':
sample_started = True
sample_children = []
elif event == 'end' and elem.tag == 'httpSample':
sample = self.get_sample(elem)
if sample_started:
sample_children.append(sample)
else:
return sample
elif event == 'end' and elem.tag == 'sample':
sample = self.get_sample(elem, sample_children)
sample_started = False
return sample
except Exception as err:
collectd.error("Plugin jmeter: Exception in get_jmeter_data due to %s" % err)
def add_common_params(self, jmeter_stats):
"""Adds TIMESTAMP, PLUGIN, PLUGITYPE to dictionary."""
timestamp = int(round(time.time() * 1000))
jmeter_stats[TIMESTAMP] = timestamp
jmeter_stats[PLUGIN] = "jmeter"
jmeter_stats[PLUGINTYPE] = "jmeter"
jmeter_stats[ACTUALPLUGINTYPE] = "jmeter"
# dict_jmx[PLUGIN_INS] = doc
collectd.info("Plugin jmeter: Added common parameters successfully")
def collect_dispatch_data(self):
"""Collect data for jmeter"""
try:
while(1):
if not os.path.exists(self.path):
collectd.info("Plugin jmeter: Waiting for log file")
time.sleep(5)
else:
break
fp = open(self.path, "r")
while True:
if os.path.getsize(self.path) < self.length:
fp.seek(0)
self.length = os.path.getsize(self.path)
line = fp.readline()
if line:
# collectd.info("Plugin jmeter: Line is -- %s" % line)
if "</testResults>" in line:
collectd.info("Plugin jmeter: Reached EOF")
fp.close()
os.remove(self.path)
return
if not re.search("xml version", line) and not re.search("testResults version", line):
self.buf = self.buf + line
else:
continue
if not re.search("</httpSample>", line):
continue
jmeter_stats = self.get_jmeter_data()
# collectd.info("Plugin jmeter: Stats are = %s" % str(jmeter_stats))
# self.add_common_params(jmeter_stats)
self.buf = ''
if not jmeter_stats:
collectd.error("Plugin jmeter: Unable to fetch data for jmeter")
return
else:
self.add_common_params(jmeter_stats)
self.dispatch_data(jmeter_stats)
#time.sleep(1)
else:
time.sleep(1)
#collectd.info("Plugin jmeter: waiting for line")
continue
except Exception as err:
collectd.error("Plugin jmeter: Exception in collect_dispatch_data due to %s in %s" % (err, traceback.format_exc()))
def read(self):
"""Collects all data."""
try:
self.collect_dispatch_data()
except Exception as err:
collectd.error("Plugin jmeter: Couldn't read and gather the metrics due to the exception %s in %s" % (err, traceback.format_exc()))
def dispatch_data(self, result):
"""Dispatch data to collectd."""
collectd.info("Plugin jmeter: Values dispatched =%s" % json.dumps(result))
utils.dispatch(result)
def read_temp(self):
"""Collectd first calls register_read. At that time default interval is taken,
hence temporary function is made to call, the read callback is unregistered
and read() is called again with interval obtained from conf by register_config callback."""
collectd.unregister_read(self.read_temp)
collectd.register_read(self.read, interval=int(self.interval))
def init():
"""When new process is formed, action to SIGCHLD is reset to default behavior."""
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
OBJ = jmeterStats()
collectd.register_init(init)
collectd.register_config(OBJ.config)
collectd.register_read(OBJ.read_temp)