-
Notifications
You must be signed in to change notification settings - Fork 23
/
jmetergraph.pl
executable file
·638 lines (557 loc) · 16.2 KB
/
jmetergraph.pl
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#!/usr/bin/perl
#---
# Original script by Christoph Meissner (email unknown)
# Taken from http://wiki.apache.org/jakarta-jmeter-data/attachments/LogAnalysis/attachments/jmetergraph.pl
# Fixes by George Barnett ([email protected])
#
# Fixes:
# - Modified to 'use strict'
# - Fixed scoping of various variables
# - Added DEBUG print option
#
# Script Options:
# perl jmetergraph.pl [-alllb] [-stddev] [-range] <jtl file 1> [ ... <jtl file n>]
# -alllb [Script does not draw stacked chart for requests that are < 1% of total. This disables this behaviour]
# -stddev [Will draw the std dev]
# -range [Wil draw request range]
#
#---
#
# This script parses xml jtl files created by jmeter.
# It extracts timestamps, active threads and labels from it.
#
# Further, based on this data
# it builds below chart files (see sample files):
#
# 1. one chart containing the overall response times related to active threads.
# The resulting png file is named 'entire_user.png'.
#
# 2. one chart containing the overall response times related to throughput
# The resulting png file is named 'entire_throughput.png'.
#
# 3. one chart that will contain bars for each label
# which were stacked over response intervals (expressed in msec)
# The resulting png file is named 'ChartStacked.png'
#
# 4. one chart that will contain stacked bars for each label
# which were stacked over response intervals (expressed in msec)
# The resulting png file is named 'ChartStackedPct.png'
#
# 5. one chart per label containing response times related to active threads and throughput
# This chart png is named like the label itself with the non-word characters substituted by '_'
# If it is related to active threads then '_user' is appended to the graph's file name -
# otherwise a '_throughput'.
#
# IMPORTANT:
# ==========
# The script works best with jmeter log format V2.1.
# I never tested it on log's of either older or newer versions.
# Also, I was too sluggish to include an XML parser.
# Thus the script parses the jtl files by using regex.
#
# The graphs are built depending on the names of the labels of your requests.
# Thus group your label names with this in mind
# when you are about to create your jmeter testplan,
#
# Also, only the labels on the 1st level are considered.
# They accumulate the response time, active users and throughput
# for all sub levels.
#
#
# FAQ:
# ====
# How to make this script to create proper charts?
#
# Open your testplan with jmeter and
# 1. insert listener 'Aggregate Report' (unless not present already).
# 2. invoke 'Configure' there
# 3. make sure that below checks are activated at least:
# 'Save as XML'
# 'Save Label'
# 'Save Time Stamp'
# 'Save Thread Name'
# 'Save Active Thread Counts'
# 'Save Elapsed Time'
# 4. tell 'Aggregate Report' to write all data into a file
# 5. when building your testplan
# you should name your critical request samplers in a way that data can be grouped into it
# (eg. 'Login', 'Host request', 'continue page', 'Req Database xyz', ...)
#
# Perl requisites:
# 6. install the perl 'GD' package
# 7. install the perl 'Chart' package
# 8. test perl.
# This means that
# perl -MGD -MChart::Lines -MChart::Composite -MChart::StackedBars
# shouldn't fail
#
# To run the script:
# perl jmetergraph.pl <jtl file1> <jtl file2> ... <jtl file_n>
#
# The png graphs will be created in current working directory
#
# I created this checklist to my best knowledge.
# However, if it fails in your computer environment, ...
#
#---
use strict;
use Chart::StackedBars;
use Chart::Composite;
use Chart::Lines;
#---
# arguments passed?
#---
my @files = grep {!/^-/ && -s "$_"} @ARGV;
our @args = grep {/^-/ && !-f "$_"} @ARGV;
my $DEBUG = 1;
my %entire = ();
my %glabels = ();
my %gthreads = ();
my $atflag = 0; # if active threads found then according graphs will be created
#---
# data received within this intervall will be averaged into one result
#---
my $collectinterval = 180; # 60 seconds
#---
# cusps aggregate response times
#---
our @cusps = (200, 500, 1000, 2000, 5000, 10000, 60000);
#---
# labels determine the name of output charts
#---
our @labels = ();
our @threads = ();
#---
# intermediate values
#---
our %timestamps = ();
our $respcount = 0;
our $measures = 0;
# Some variables. Cunt who wrote this script didn't use strict so it's fucked ITO scoping
my ($entireta,$entirecnt,$entireby);
my ($respcount,$sumresptimes,$sumSQresptimes);
#---
# define colors for stacked bar charts
#---
our %colors = (
dataset0 => "green",
dataset1 => [0, 139, 139], # dark cyan
dataset2 => [255, 215,0], # gold
dataset3 => "DarkOrange",
dataset4 => "red",
dataset5 => [255, 0, 0], # red
dataset6 => [139, 0, 139], # dark magenta
dataset7 => [0, 0, 0], # black
);
#---
# here we go thru all files and collect data
#---
while(my $file = shift(@files)) {
print "Opening file $file\n" if $DEBUG;
open(IN, "<$file") || do {
print $file, " ", $!, "\n";
next;
};
print "Parsing data from $file\n" if $DEBUG;
while(<IN>) {
my ($time,$timestamp,$success,$label,$thread,$latency,$bytes,$DataEncoding,$DataType,$ErrorCount,$Hostname,$NumberOfActiveThreadsAll,$NumberOfActiveThreadsGroup,$ResponseCode,$ResponseMessage,$SampleCount);
if(/^<(sample|httpSample)\s/) {
($time) = (/\st="(\d+)"/o);
($timestamp) = (/\sts="(\d+)"/o);
($success) = (/\ss="(.+?)"/o);
($label) = (/\slb="(.+?)"/o);
($thread) = (/\stn="(.+?)"/o);
($latency) = (/\slt="(\d+)"/o);
($bytes) = (/\sby="(\d+)"/o);
($DataEncoding) = (/\sde="(\d+)"/o);
($DataType) = (/\sdt="(.+?)"/o);
($ErrorCount) = (/\sec="(\d+)"/o);
($Hostname) = (/\shn="(.+?)"/o);
($NumberOfActiveThreadsAll) = (/\sna="(\d+)"/o);
($NumberOfActiveThreadsGroup) = (/\sng="(\d+)"/o);
($ResponseCode) = (/\src="(.+?)"/o);
($ResponseMessage) = (/\srm="(.+?)"/o);
($SampleCount) = (/\ssc="(\d+)"/o);
} elsif(/^<sampleResult/) {
($time) = (/\stime="(\d+)"/o);
($timestamp) = (/timeStamp="(\d+)"/o);
($success) = (/success="(.+?)"/o);
($label) = (/label="(.+?)"/o);
($thread) = (/threadName="(.+?)"/o);
} else {
next;
}
$label =~ s/\s+$//g;
$label =~ s/^\s+//g;
$label =~ s/[\W\s]+/_/g;
next if($label =~ /^garbage/i); # don't count these labels into statistics
#---
# memorize labels
#---
if(!grep(/^$label$/, @labels)) {
push(@labels, $label);
print "Found new label: $label\n" if $DEBUG;
}
$glabels{$label}{'respcount'} += 1;
$entire{'respcount'} += 1;
#---
# memorize timestamps
#---
my $tstmp = int($timestamp / (1000 * $collectinterval)) * $collectinterval;
$timestamps{$tstmp} += 1;
#---
# cusps
#---
for(my $i = 0; $i <= $#cusps; $i++) {
if(($time <= $cusps[$i]) || (($i == $#cusps) && ($time > $cusps[$i]))) {
$glabels{$label}{$cusps[$i]} += 1;
$entire{$cusps[$i]} += 1;
last;
}
}
#---
# stddev
#---
$respcount += 1;
$sumresptimes += $time;
$sumSQresptimes += ($time ** 2);
if($respcount > 1) {
my $stddev = sqrt(($respcount * $sumSQresptimes - $sumresptimes ** 2) /
($respcount * ($respcount - 1)));
$entire{$tstmp, 'stddev'} = $glabels{$label}{$tstmp, 'stddev'} = $stddev;
}
#---
# avg
#---
$entire{$tstmp, 'avg'} = $sumresptimes / $respcount;
$glabels{$label}{$tstmp, 'responsetime'} += $time;
$glabels{$label}{$tstmp, 'respcount'} += 1;
$glabels{$label}{$tstmp, 'avg'} = int($glabels{$label}{$tstmp, 'responsetime'} / $glabels{$label}{$tstmp, 'respcount'});
#---
# active threads
#---
if(!$entire{$tstmp, 'activethreads'}) {
$entireta = 0;
$entirecnt = 0;
$entireby = 0;
}
if($NumberOfActiveThreadsAll > 0) {
$atflag = 1;
}
$entirecnt += 1;
if($atflag == 1) {
$entireta += $NumberOfActiveThreadsAll;
$entire{$tstmp, 'activethreads'} = int($entireta / $entirecnt);
if(!$glabels{$label}{$tstmp, 'activethreads'}) {
$glabels{$label}{$tstmp, 'lbta'} = 0;
$glabels{$label}{$tstmp, 'lbby'} = 0;
}
$glabels{$label}{$tstmp, 'lbta'} += $NumberOfActiveThreadsAll;
$glabels{$label}{$tstmp, 'activethreads'} = sprintf("%.0f", $glabels{$label}{$tstmp, 'lbta'} / $glabels{$label}{$tstmp, 'respcount'});
} else {
#---
# if NumberOfActiveThreads is not available
# use threadname to extrapolate active threads later
#---
if($NumberOfActiveThreadsAll eq '') {
if(!$gthreads{$thread}{'first'}) {
$gthreads{$thread}{'first'} = $tstmp;
push(@threads, $thread);
}
$gthreads{$thread}{'last'} = $tstmp;
}
}
#---
# throughput
#---
if($bytes > 0) {
$entireby += $bytes;
$entire{$tstmp, 'throughput'} = int($entireby / $entirecnt);
$glabels{$label}{$tstmp, 'lbby'} += $bytes;
$glabels{$label}{$tstmp, 'throughput'} = $glabels{$label}{$tstmp, 'lbby'}; # counts per $collectinterval
}
}
print "Closing $file\n" if $DEBUG;
close(IN);
}
print "Found $#labels labels\n" if $DEBUG;
# Sort the labels.
print "Sorting labels\n" if $DEBUG;
my @tmplabels = sort @labels;
@labels = @tmplabels;
#---
# if required (no NumbersOfActiveThreads)
# then extrapolate users
#---
if($atflag == 0) {
print "using timestamps to calculate active threads\n";
my @tstmps = sort { $a <=> $b } keys(%timestamps);
foreach my $label ('entire', @labels) {
print "tracking $label\n";
foreach my $thread (@threads) {
foreach my $tstmp (@tstmps) {
if($gthreads{$thread}{'first'} <= $tstmp && $gthreads{$thread}{'last'} >= $tstmp) {
$glabels{$label}{$tstmp, 'activethreads'} += 1;
}
}
}
}
}
#---
# charts will be created
# if something could be parsed
#---
if($respcount > 0) {
#---
# number of time stamps
#---
$measures = scalar(keys(%timestamps));
print "Generating stacked bars absolute\n" if $DEBUG;
&ChartStackedBars();
print "Generating stacked bars relative\n" if $DEBUG;
&ChartStackedPct();
foreach my $label ('entire', @labels) {
if($entireby > 0) {
&ChartLines($label, 'throughput');
}
&ChartLines($label, 'users');
}
}
#-------------------------------------------------------------------------------
sub ChartStackedPct {
if(scalar(@labels) == 0) {
return undef;
}
my $ChartStacked = Chart::StackedBars->new(1024, 768);
#---
# cusps
#---
my @xaxis = ();
my @xlabels = ();
foreach my $label (@labels) {
print "Attempting to add $label to StackedPCT graph\n" if $DEBUG;
if(($glabels{$label}{'respcount'} > ($respcount / 100)) || grep(/-alllb/i, @args)) {
push(@xaxis, $label);
if (length $label > 25) {
$label = substr($label,0,25) . " " . substr($label,25);
}
push(@xlabels, $label);
print " Added $label\n" if $DEBUG;
}
}
$ChartStacked->add_dataset(@xlabels);
my ($value,$i,$label);
my @data = ();
my @legend_labels = ();
for($i = 0; $i <= $#cusps; $i++) {
@data = ();
foreach my $label (@xaxis) {
$value = $glabels{$label}{$cusps[$i]};
if(!defined $value) {
$value = 0;
}
$value = (100 * $value) / $glabels{$label}{'respcount'};
push(@data, $value);
}
$ChartStacked->add_dataset(@data);
push(@legend_labels, "< " . $cusps[$i] . " msec");
}
my %settings = (
transparent => 'true',
title => 'Response Time %',
y_grid_lines => 'true',
legend => 'right',
legend_labels => \@legend_labels,
precision => 0,
y_label => 'Requests %',
max_val => 100,
include_zero => 'true',
point => 0,
colors => \%colors,
x_ticks => 'vertical',
precision => 0,
);
$ChartStacked->set(%settings);
print "Generated ChartStackedPct.png\n" if $DEBUG;
$ChartStacked->png("ChartStackedPct.png");
}
#-------------------------------------------------------------------------------
sub ChartStackedBars {
if(scalar(@labels) == 0) {
return undef;
}
my $ChartStacked = Chart::StackedBars->new(1024, 768);
#---
# cusps
#---
my @xaxis = ();
my @xlabels = ();
foreach my $label (@labels) {
print "Added $label to StackedPCT graph\n" if $DEBUG;
if(($glabels{$label}{'respcount'} > ($respcount / 100)) || grep(/-alllb/i, @args)) {
push(@xaxis, $label);
push(@xlabels, $label);
}
}
$ChartStacked->add_dataset(@xlabels);
my ($value,$i,$label);
my @data = ();
my @legend_labels = ();
for($i = 0; $i <= $#cusps; $i++) {
@data = ();
foreach my $label (@xaxis) {
$value = $glabels{$label}{$cusps[$i]};
if($value == undef) {
$value = 0;
}
push(@data, $value);
}
$ChartStacked->add_dataset(@data);
push(@legend_labels, "< " . $cusps[$i] . " msec");
}
my %settings = (
transparent => 'true',
title => 'Response Time',
y_grid_lines => 'true',
legend => 'right',
legend_labels => \@legend_labels,
precision => 0,
y_label => 'Requests',
include_zero => 'true',
point => 0,
colors => \%colors,
x_ticks => 'vertical',
precision => 0,
);
$ChartStacked->set(%settings);
print "Generating ChartStacked.png\n" if $DEBUG;
$ChartStacked->png("ChartStacked.png");
}
#-------------------------------------------------------------------------------
sub ChartLines {
my ($label, $mode) = @_;
my %labelmap = (
'entire' => 'total',
);
my $title = $label;
$title = $labelmap{$label} if($labelmap{$label});
my $ChartComposite = Chart::Composite->new(1024, 768);
my @tstmps = sort { $a <=> $b } keys(%timestamps);
my @responsetimes = ();
my @plusstddev = ();
my @minusstddev = ();
my @users = ();
my @throughput = ();
my @xaxis = ();
my $y2label;
#---
# response times
#---
my $tstmp;
my ($pstd, $mstd) = (0, 0);
foreach my $tstmp (@tstmps) {
if($glabels{$label}{$tstmp, 'avg'}) {
push(@xaxis, $tstmp);
push(@responsetimes, $glabels{$label}{$tstmp, 'avg'});
$mstd = $glabels{$label}{$tstmp, 'avg'} - $glabels{$label}{$tstmp, 'stddev'};
$pstd = $glabels{$label}{$tstmp, 'avg'} + $glabels{$label}{$tstmp, 'stddev'};
$mstd = 1 if($mstd < 0); # supress lines below 0
push(@plusstddev, $pstd);
push(@minusstddev, $mstd);
}
}
$ChartComposite->add_dataset(@xaxis);
$ChartComposite->add_dataset(@responsetimes);
my %colors = (
dataset0 => "green",
dataset1 => "red",
);
my @ds1 = (1);
my @ds2 = (2);
if(grep(/-stddev/ || /-range/, @args)) {
$ChartComposite->add_dataset(@plusstddev);
$ChartComposite->add_dataset(@minusstddev);
@ds1 = (1, 2, 3);
@ds2 = (4);
%colors = (
dataset0 => "green",
dataset1 => [189, 183, 107], # dark khaki
dataset2 => [189, 183, 107], # dark khaki
dataset3 => "red",
);
}
if($mode eq 'users') {
#---
# users
#---
foreach my $tstmp (@xaxis) {
push(@users, $glabels{$label}{$tstmp, 'activethreads'});
}
$ChartComposite->add_dataset(@users);
$y2label = "active threads";
} else {
#---
# throughput
#---
foreach my $tstmp (@xaxis) {
push(@throughput, $glabels{$label}{$tstmp, 'throughput'});
}
$ChartComposite->add_dataset(@throughput);
$y2label = "throughput bytes/min";
}
my $skip = 0;
if(scalar(@xaxis) > 40) {
$skip = int(scalar(@xaxis) / 40) + 1;
}
my @labels = ($label, $mode);
if(grep(/-stddev/, @args)) {
@labels = ($label, "+stddev", "-stddev", $mode);
}
my $type = 'Lines';
if(grep(/-range/i, @args)) {
@labels = ($label, "n.a", "n.a", $mode);
$type = 'ErrorBars';
}
my %settings = (
composite_info => [ [$type, \@ds1], ['Lines', \@ds2 ]],
transparent => 'true',
title => 'Response Time ' . $title,
y_grid_lines => 'true',
legend => 'bottom',
y_label => 'Response Time msec',
y_label2 => $y2label,
legend_labels => \@labels,
legend_example_height => 1,
legend_example_height0 => 10,
legend_example_height1 => 2,
legend_example_height2 => 2,
legend_example_height3 => 10,
legend_example_height4 => 10,
include_zero => 'true',
x_ticks => 'vertical',
skip_x_ticks => $skip,
brush_size1 => 3,
brush_size2 => 3,
pt_size => 6,
point => 0,
line => 1,
f_x_tick => \&formatTime,
colors => \%colors,
precision => 0,
);
$ChartComposite->set(%settings);
my $filename = $label;
$filename=~ s/\W/_/g;
$filename .= '_' . $mode . '.png';
print $filename, "\n";
$ChartComposite->png($filename);
}
#-------------------------------------------------------------------------------
sub formatTime {
my ($tstmp) = @_;
my $string = scalar(localtime($tstmp));
my ($rc) = ($string =~ /\s(\d\d:\d\d:\d\d)\s/);
return $rc;
}
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------