-
Notifications
You must be signed in to change notification settings - Fork 2k
/
CSVSaveService.java
1097 lines (991 loc) · 41.4 KB
/
CSVSaveService.java
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.jmeter.save;
import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.collections.map.LinkedMap;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.SampleSaveConfiguration;
import org.apache.jmeter.samplers.StatisticalSampleResult;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.Visualizer;
import org.apache.jorphan.reflect.Functor;
import org.apache.jorphan.util.JMeterError;
import org.apache.oro.text.regex.Pattern;
import org.apache.oro.text.regex.PatternMatcherInput;
import org.apache.oro.text.regex.Perl5Compiler;
import org.apache.oro.text.regex.Perl5Matcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class provides a means for saving/reading test results as CSV files.
*/
// For unit tests, @see TestCSVSaveService
public final class CSVSaveService {
private static final Logger log = LoggerFactory.getLogger(CSVSaveService.class);
// ---------------------------------------------------------------------
// XML RESULT FILE CONSTANTS AND FIELD NAME CONSTANTS
// ---------------------------------------------------------------------
public static final String DATA_TYPE = "dataType"; // $NON-NLS-1$
public static final String FAILURE_MESSAGE = "failureMessage"; // $NON-NLS-1$
public static final String LABEL = "label"; // $NON-NLS-1$
public static final String RESPONSE_CODE = "responseCode"; // $NON-NLS-1$
public static final String RESPONSE_MESSAGE = "responseMessage"; // $NON-NLS-1$
public static final String SUCCESSFUL = "success"; // $NON-NLS-1$
public static final String THREAD_NAME = "threadName"; // $NON-NLS-1$
public static final String TIME_STAMP = "timeStamp"; // $NON-NLS-1$
// ---------------------------------------------------------------------
// ADDITIONAL CSV RESULT FILE CONSTANTS AND FIELD NAME CONSTANTS
// ---------------------------------------------------------------------
public static final String CSV_ELAPSED = "elapsed"; // $NON-NLS-1$
public static final String CSV_BYTES = "bytes"; // $NON-NLS-1$
public static final String CSV_SENT_BYTES = "sentBytes"; // $NON-NLS-1$
public static final String CSV_THREAD_COUNT1 = "grpThreads"; // $NON-NLS-1$
public static final String CSV_THREAD_COUNT2 = "allThreads"; // $NON-NLS-1$
public static final String CSV_SAMPLE_COUNT = "SampleCount"; // $NON-NLS-1$
public static final String CSV_ERROR_COUNT = "ErrorCount"; // $NON-NLS-1$
public static final String CSV_URL = "URL"; // $NON-NLS-1$
public static final String CSV_FILENAME = "Filename"; // $NON-NLS-1$
public static final String CSV_LATENCY = "Latency"; // $NON-NLS-1$
public static final String CSV_CONNECT_TIME = "Connect"; // $NON-NLS-1$
public static final String CSV_ENCODING = "Encoding"; // $NON-NLS-1$
public static final String CSV_HOSTNAME = "Hostname"; // $NON-NLS-1$
public static final String CSV_IDLETIME = "IdleTime"; // $NON-NLS-1$
// Used to enclose variable name labels, to distinguish from any of the
// above labels
private static final String VARIABLE_NAME_QUOTE_CHAR = "\""; // $NON-NLS-1$
// Initial config from properties
private static final SampleSaveConfiguration _saveConfig = SampleSaveConfiguration
.staticConfig();
// Date formats to try if the time format does not parse as milliseconds
private static final String[] DATE_FORMAT_STRINGS = {
"yyyy/MM/dd HH:mm:ss.SSS", // $NON-NLS-1$
"yyyy/MM/dd HH:mm:ss", // $NON-NLS-1$
"yyyy-MM-dd HH:mm:ss.SSS", // $NON-NLS-1$
"yyyy-MM-dd HH:mm:ss", // $NON-NLS-1$
"MM/dd/yy HH:mm:ss" // $NON-NLS-1$ (for compatibility, this is the original default)
};
private static final String LINE_SEP = System.getProperty("line.separator"); // $NON-NLS-1$
/**
* Private constructor to prevent instantiation.
*/
private CSVSaveService() {
}
/**
* Read Samples from a file; handles quoted strings.
*
* @param filename
* input file
* @param visualizer
* where to send the results
* @param resultCollector
* the parent collector
* @throws IOException
* when the file referenced by <code>filename</code> can't be
* read correctly
*/
public static void processSamples(String filename, Visualizer visualizer,
ResultCollector resultCollector) throws IOException {
final boolean errorsOnly = resultCollector.isErrorLogging();
final boolean successOnly = resultCollector.isSuccessOnlyLogging();
try (InputStream inStream = new FileInputStream(filename);
Reader inReader = new InputStreamReader(inStream,
SaveService.getFileEncoding(StandardCharsets.UTF_8.name()));
BufferedReader dataReader = new BufferedReader(inReader)) {
dataReader.mark(400);// Enough to read the header column names
// Get the first line, and see if it is the header
String line = dataReader.readLine();
if (line == null) {
throw new IOException(filename + ": unable to read header line");
}
long lineNumber = 1;
SampleSaveConfiguration saveConfig = CSVSaveService
.getSampleSaveConfiguration(line, filename);
if (saveConfig == null) {// not a valid header
log.info("{} does not appear to have a valid header. Using default configuration.", filename);
saveConfig = (SampleSaveConfiguration) resultCollector
.getSaveConfig().clone(); // may change the format later
dataReader.reset(); // restart from beginning
lineNumber = 0;
}
String[] parts;
final char delim = saveConfig.getDelimiter().charAt(0);
// TODO: does it matter that an empty line will terminate the loop?
// CSV output files should never contain empty lines, so probably
// not
// If so, then need to check whether the reader is at EOF
while ((parts = csvReadFile(dataReader, delim)).length != 0) {
lineNumber++;
SampleEvent event = CSVSaveService.makeResultFromDelimitedString(parts, saveConfig, lineNumber);
if (event != null) {
final SampleResult result = event.getResult();
if (ResultCollector.isSampleWanted(result.isSuccessful(),
errorsOnly, successOnly)) {
visualizer.add(result);
}
}
}
}
}
/**
* Make a SampleResult given a set of tokens
*
* @param parts
* tokens parsed from the input
* @param saveConfig
* the save configuration (may be updated)
* @param lineNumber the line number (for error reporting)
* @return the sample result
*
* @throws JMeterError
*/
private static SampleEvent makeResultFromDelimitedString(
final String[] parts,
final SampleSaveConfiguration saveConfig, // may be updated
final long lineNumber) {
SampleResult result = null;
String hostname = "";// $NON-NLS-1$
long timeStamp = 0;
long elapsed = 0;
String text = null;
String field = null; // Save the name for error reporting
int i = 0;
try {
if (saveConfig.saveTimestamp()) {
field = TIME_STAMP;
text = parts[i++];
if (saveConfig.printMilliseconds()) {
try {
timeStamp = Long.parseLong(text); // see if this works
} catch (NumberFormatException e) { // it did not, let's try some other formats
log.warn("Cannot parse timestamp: '{}', will try following formats {}", text,
Arrays.asList(DATE_FORMAT_STRINGS));
boolean foundMatch = false;
for(String fmt : DATE_FORMAT_STRINGS) {
SimpleDateFormat dateFormat = new SimpleDateFormat(fmt);
dateFormat.setLenient(false);
try {
Date stamp = dateFormat.parse(text);
timeStamp = stamp.getTime();
log.warn("Setting date format to: {}", fmt);
saveConfig.setDateFormat(fmt);
foundMatch = true;
break;
} catch (ParseException pe) {
log.info("{} did not match {}, trying next date format", text, fmt);
}
}
if (!foundMatch) {
throw new ParseException("No date-time format found matching "+text,-1);
}
}
} else if (saveConfig.strictDateFormatter() != null) {
Date stamp = saveConfig.strictDateFormatter().parse(text);
timeStamp = stamp.getTime();
} else { // can this happen?
final String msg = "Unknown timestamp format";
log.warn(msg);
throw new JMeterError(msg);
}
}
if (saveConfig.saveTime()) {
field = CSV_ELAPSED;
text = parts[i++];
elapsed = Long.parseLong(text);
}
if (saveConfig.saveSampleCount()) {
result = new StatisticalSampleResult(timeStamp, elapsed);
} else {
result = new SampleResult(timeStamp, elapsed);
}
if (saveConfig.saveLabel()) {
field = LABEL;
text = parts[i++];
result.setSampleLabel(text);
}
if (saveConfig.saveCode()) {
field = RESPONSE_CODE;
text = parts[i++];
result.setResponseCode(text);
}
if (saveConfig.saveMessage()) {
field = RESPONSE_MESSAGE;
text = parts[i++];
result.setResponseMessage(text);
}
if (saveConfig.saveThreadName()) {
field = THREAD_NAME;
text = parts[i++];
result.setThreadName(text);
}
if (saveConfig.saveDataType()) {
field = DATA_TYPE;
text = parts[i++];
result.setDataType(text);
}
if (saveConfig.saveSuccess()) {
field = SUCCESSFUL;
text = parts[i++];
result.setSuccessful(Boolean.valueOf(text).booleanValue());
}
if (saveConfig.saveAssertionResultsFailureMessage()) {
i++;
// TODO - should this be restored?
}
if (saveConfig.saveBytes()) {
field = CSV_BYTES;
text = parts[i++];
result.setBytes(Long.parseLong(text));
}
if (saveConfig.saveSentBytes()) {
field = CSV_SENT_BYTES;
text = parts[i++];
result.setSentBytes(Long.parseLong(text));
}
if (saveConfig.saveThreadCounts()) {
field = CSV_THREAD_COUNT1;
text = parts[i++];
result.setGroupThreads(Integer.parseInt(text));
field = CSV_THREAD_COUNT2;
text = parts[i++];
result.setAllThreads(Integer.parseInt(text));
}
if (saveConfig.saveUrl()) {
i++;
// TODO: should this be restored?
}
if (saveConfig.saveFileName()) {
field = CSV_FILENAME;
text = parts[i++];
result.setResultFileName(text);
}
if (saveConfig.saveLatency()) {
field = CSV_LATENCY;
text = parts[i++];
result.setLatency(Long.parseLong(text));
}
if (saveConfig.saveEncoding()) {
field = CSV_ENCODING;
text = parts[i++];
result.setEncodingAndType(text);
}
if (saveConfig.saveSampleCount()) {
field = CSV_SAMPLE_COUNT;
text = parts[i++];
result.setSampleCount(Integer.parseInt(text));
field = CSV_ERROR_COUNT;
text = parts[i++];
result.setErrorCount(Integer.parseInt(text));
}
if (saveConfig.saveHostname()) {
field = CSV_HOSTNAME;
hostname = parts[i++];
}
if (saveConfig.saveIdleTime()) {
field = CSV_IDLETIME;
text = parts[i++];
result.setIdleTime(Long.parseLong(text));
}
if (saveConfig.saveConnectTime()) {
field = CSV_CONNECT_TIME;
text = parts[i++];
result.setConnectTime(Long.parseLong(text));
}
if (i + saveConfig.getVarCount() < parts.length) {
log.warn("Line: {}. Found {} fields, expected {}. Extra fields have been ignored.", lineNumber,
parts.length, i);
}
} catch (NumberFormatException | ParseException e) {
if (log.isWarnEnabled()) {
log.warn("Error parsing field '{}' at line {}. {}", field, lineNumber, e.toString());
}
throw new JMeterError(e);
} catch (ArrayIndexOutOfBoundsException e) {
log.warn("Insufficient columns to parse field '{}' at line {}", field, lineNumber);
throw new JMeterError(e);
}
return new SampleEvent(result, "", hostname);
}
/**
* Generates the field names for the output file
*
* @return the field names as a string
*/
public static String printableFieldNamesToString() {
return printableFieldNamesToString(_saveConfig);
}
/**
* Generates the field names for the output file
*
* @param saveConfig
* the configuration of what is to be saved
* @return the field names as a string
*/
public static String printableFieldNamesToString(
SampleSaveConfiguration saveConfig) {
StringBuilder text = new StringBuilder();
String delim = saveConfig.getDelimiter();
appendFields(saveConfig.saveTimestamp(), text, delim, TIME_STAMP);
appendFields(saveConfig.saveTime(), text, delim, CSV_ELAPSED);
appendFields(saveConfig.saveLabel(), text, delim, LABEL);
appendFields(saveConfig.saveCode(), text, delim, RESPONSE_CODE);
appendFields(saveConfig.saveMessage(), text, delim, RESPONSE_MESSAGE);
appendFields(saveConfig.saveThreadName(), text, delim, THREAD_NAME);
appendFields(saveConfig.saveDataType(), text, delim, DATA_TYPE);
appendFields(saveConfig.saveSuccess(), text, delim, SUCCESSFUL);
appendFields(saveConfig.saveAssertionResultsFailureMessage(), text, delim, FAILURE_MESSAGE);
appendFields(saveConfig.saveBytes(), text, delim, CSV_BYTES);
appendFields(saveConfig.saveSentBytes(), text, delim, CSV_SENT_BYTES);
appendFields(saveConfig.saveThreadCounts(), text, delim, CSV_THREAD_COUNT1, CSV_THREAD_COUNT2);
appendFields(saveConfig.saveUrl(), text, delim, CSV_URL);
appendFields(saveConfig.saveFileName(), text, delim, CSV_FILENAME);
appendFields(saveConfig.saveLatency(), text, delim, CSV_LATENCY);
appendFields(saveConfig.saveEncoding(), text, delim, CSV_ENCODING);
appendFields(saveConfig.saveSampleCount(), text, delim, CSV_SAMPLE_COUNT, CSV_ERROR_COUNT);
appendFields(saveConfig.saveHostname(), text, delim, CSV_HOSTNAME);
appendFields(saveConfig.saveIdleTime(), text, delim, CSV_IDLETIME);
appendFields(saveConfig.saveConnectTime(), text, delim, CSV_CONNECT_TIME);
for (int i = 0; i < SampleEvent.getVarCount(); i++) {
text.append(VARIABLE_NAME_QUOTE_CHAR);
text.append(SampleEvent.getVarName(i));
text.append(VARIABLE_NAME_QUOTE_CHAR);
text.append(delim);
}
String resultString;
int size = text.length();
int delSize = delim.length();
// Strip off the trailing delimiter
if (size >= delSize) {
resultString = text.substring(0, size - delSize);
} else {
resultString = text.toString();
}
return resultString;
}
private static void appendFields(final boolean condition, StringBuilder textBuffer, String delim, String... fieldNames) {
if (condition) {
for (String name: fieldNames) {
textBuffer.append(name);
textBuffer.append(delim);
}
}
}
// Map header names to set() methods
private static final LinkedMap headerLabelMethods = new LinkedMap();
// These entries must be in the same order as columns are saved/restored.
static {
headerLabelMethods.put(TIME_STAMP, new Functor("setTimestamp"));
headerLabelMethods.put(CSV_ELAPSED, new Functor("setTime"));
headerLabelMethods.put(LABEL, new Functor("setLabel"));
headerLabelMethods.put(RESPONSE_CODE, new Functor("setCode"));
headerLabelMethods.put(RESPONSE_MESSAGE, new Functor("setMessage"));
headerLabelMethods.put(THREAD_NAME, new Functor("setThreadName"));
headerLabelMethods.put(DATA_TYPE, new Functor("setDataType"));
headerLabelMethods.put(SUCCESSFUL, new Functor("setSuccess"));
headerLabelMethods.put(FAILURE_MESSAGE, new Functor(
"setAssertionResultsFailureMessage"));
headerLabelMethods.put(CSV_BYTES, new Functor("setBytes"));
headerLabelMethods.put(CSV_SENT_BYTES, new Functor("setSentBytes"));
// Both these are needed in the list even though they set the same
// variable
headerLabelMethods.put(CSV_THREAD_COUNT1,
new Functor("setThreadCounts"));
headerLabelMethods.put(CSV_THREAD_COUNT2,
new Functor("setThreadCounts"));
headerLabelMethods.put(CSV_URL, new Functor("setUrl"));
headerLabelMethods.put(CSV_FILENAME, new Functor("setFileName"));
headerLabelMethods.put(CSV_LATENCY, new Functor("setLatency"));
headerLabelMethods.put(CSV_ENCODING, new Functor("setEncoding"));
// Both these are needed in the list even though they set the same
// variable
headerLabelMethods.put(CSV_SAMPLE_COUNT, new Functor("setSampleCount"));
headerLabelMethods.put(CSV_ERROR_COUNT, new Functor("setSampleCount"));
headerLabelMethods.put(CSV_HOSTNAME, new Functor("setHostname"));
headerLabelMethods.put(CSV_IDLETIME, new Functor("setIdleTime"));
headerLabelMethods.put(CSV_CONNECT_TIME, new Functor("setConnectTime"));
}
/**
* Parse a CSV header line
*
* @param headerLine
* from CSV file
* @param filename
* name of file (for log message only)
* @return config corresponding to the header items found or null if not a
* header line
*/
public static SampleSaveConfiguration getSampleSaveConfiguration(
String headerLine, String filename) {
String[] parts = splitHeader(headerLine, _saveConfig.getDelimiter()); // Try
// default
// delimiter
String delim = null;
if (parts == null) {
Perl5Matcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(headerLine);
Pattern pattern = JMeterUtils.getPatternCache()
// This assumes the header names are all single words with no spaces
// word followed by 0 or more repeats of (non-word char + word)
// where the non-word char (\2) is the same
// e.g. abc|def|ghi but not abd|def~ghi
.getPattern("\\w+((\\W)\\w+)?(\\2\\w+)*(\\2\"\\w+\")*", // $NON-NLS-1$
// last entries may be quoted strings
Perl5Compiler.READ_ONLY_MASK);
if (matcher.matches(input, pattern)) {
delim = matcher.getMatch().group(2);
parts = splitHeader(headerLine, delim);// now validate the
// result
}
}
if (parts == null) {
return null; // failed to recognise the header
}
// We know the column names all exist, so create the config
SampleSaveConfiguration saveConfig = new SampleSaveConfiguration(false);
int varCount = 0;
for (String label : parts) {
if (isVariableName(label)) {
varCount++;
} else {
Functor set = (Functor) headerLabelMethods.get(label);
set.invoke(saveConfig, new Boolean[]{Boolean.TRUE});
}
}
if (delim != null) {
if (log.isWarnEnabled()) {
log.warn("Default delimiter '{}' did not work; using alternate '{}' for reading {}",
_saveConfig.getDelimiter(), delim, filename);
}
saveConfig.setDelimiter(delim);
}
saveConfig.setVarCount(varCount);
return saveConfig;
}
private static String[] splitHeader(String headerLine, String delim) {
String[] parts = headerLine.split("\\Q" + delim);// $NON-NLS-1$
int previous = -1;
// Check if the line is a header
for (int i = 0; i < parts.length; i++) {
final String label = parts[i];
// Check for Quoted variable names
if (isVariableName(label)) {
previous = Integer.MAX_VALUE; // they are always last
continue;
}
int current = headerLabelMethods.indexOf(label);
if (current == -1) {
log.warn("Unknown column name {}", label);
return null; // unknown column name
}
if (current <= previous) {
log.warn("Column header number {} name {} is out of order.", (i + 1), label);
return null; // out of order
}
previous = current;
}
return parts;
}
/**
* Check if the label is a variable name, i.e. is it enclosed in
* double-quotes?
*
* @param label
* column name from CSV file
* @return if the label is enclosed in double-quotes
*/
private static boolean isVariableName(final String label) {
return label.length() > 2 && label.startsWith(VARIABLE_NAME_QUOTE_CHAR)
&& label.endsWith(VARIABLE_NAME_QUOTE_CHAR);
}
/**
* Method will save aggregate statistics as CSV. For now I put it here. Not
* sure if it should go in the newer SaveService instead of here. if we ever
* decide to get rid of this class, we'll need to move this method to the
* new save service.
*
* @param data
* List of data rows
* @param writer
* output writer
* @throws IOException
* when writing to <code>writer</code> fails
*/
public static void saveCSVStats(List<?> data, Writer writer)
throws IOException {
saveCSVStats(data, writer, null);
}
/**
* Method will save aggregate statistics as CSV. For now I put it here. Not
* sure if it should go in the newer SaveService instead of here. if we ever
* decide to get rid of this class, we'll need to move this method to the
* new save service.
*
* @param data
* List of data rows
* @param writer
* output file
* @param headers
* header names (if non-null)
* @throws IOException
* when writing to <code>writer</code> fails
*/
public static void saveCSVStats(List<?> data, Writer writer,
String[] headers) throws IOException {
final char DELIM = ',';
final char[] SPECIALS = new char[] { DELIM, QUOTING_CHAR };
if (headers != null) {
for (int i = 0; i < headers.length; i++) {
if (i > 0) {
writer.write(DELIM);
}
writer.write(quoteDelimiters(headers[i], SPECIALS));
}
writer.write(LINE_SEP);
}
for (Object o : data) {
List<?> row = (List<?>) o;
for (int idy = 0; idy < row.size(); idy++) {
if (idy > 0) {
writer.write(DELIM);
}
Object item = row.get(idy);
writer.write(quoteDelimiters(String.valueOf(item), SPECIALS));
}
writer.write(LINE_SEP);
}
}
/**
* Method saves aggregate statistics (with header names) as CSV from a table
* model. Same as {@link #saveCSVStats(List, Writer, String[])} except
* that there is no need to create a List containing the data.
*
* @param model
* table model containing the data
* @param writer
* output file
* @throws IOException
* when writing to <code>writer</code> fails
*/
public static void saveCSVStats(DefaultTableModel model, FileWriter writer)
throws IOException {
saveCSVStats(model, writer, true);
}
/**
* Method saves aggregate statistics as CSV from a table model. Same as
* {@link #saveCSVStats(List, Writer, String[])} except that there is no
* need to create a List containing the data.
*
* @param model
* table model containing the data
* @param writer
* output file
* @param saveHeaders
* whether or not to save headers
* @throws IOException
* when writing to <code>writer</code> fails
*/
public static void saveCSVStats(DefaultTableModel model, FileWriter writer,
boolean saveHeaders) throws IOException {
final char DELIM = ',';
final char[] SPECIALS = new char[] { DELIM, QUOTING_CHAR };
final int columns = model.getColumnCount();
final int rows = model.getRowCount();
if (saveHeaders) {
for (int i = 0; i < columns; i++) {
if (i > 0) {
writer.write(DELIM);
}
writer.write(quoteDelimiters(model.getColumnName(i), SPECIALS));
}
writer.write(LINE_SEP);
}
for (int row = 0; row < rows; row++) {
for (int column = 0; column < columns; column++) {
if (column > 0) {
writer.write(DELIM);
}
Object item = model.getValueAt(row, column);
writer.write(quoteDelimiters(String.valueOf(item), SPECIALS));
}
writer.write(LINE_SEP);
}
}
/**
* Convert a result into a string, where the fields of the result are
* separated by the default delimiter.
*
* @param event
* the sample event to be converted
* @return the separated value representation of the result
*/
public static String resultToDelimitedString(SampleEvent event) {
return resultToDelimitedString(event, event.getResult().getSaveConfig()
.getDelimiter());
}
/*
* Class to handle generating the delimited string. - adds the delimiter
* if not the first call - quotes any strings that require it
*/
static final class StringQuoter {
private final StringBuilder sb;
private final char[] specials;
private boolean addDelim;
public StringQuoter(char delim) {
sb = new StringBuilder(150);
specials = new char[] { delim, QUOTING_CHAR, CharUtils.CR,
CharUtils.LF };
addDelim = false; // Don't add delimiter first time round
}
private void addDelim() {
if (addDelim) {
sb.append(specials[0]);
} else {
addDelim = true;
}
}
// These methods handle parameters that could contain delimiters or
// quotes:
public void append(String s) {
addDelim();
// if (s == null) return;
sb.append(quoteDelimiters(s, specials));
}
public void append(Object obj) {
append(String.valueOf(obj));
}
// These methods handle parameters that cannot contain delimiters or
// quotes
public void append(int i) {
addDelim();
sb.append(i);
}
public void append(long l) {
addDelim();
sb.append(l);
}
public void append(boolean b) {
addDelim();
sb.append(b);
}
@Override
public String toString() {
return sb.toString();
}
}
/**
* Convert a result into a string, where the fields of the result are
* separated by a specified String.
*
* @param event
* the sample event to be converted
* @param delimiter
* the separation string
* @return the separated value representation of the result
*/
public static String resultToDelimitedString(SampleEvent event,
final String delimiter) {
StringQuoter text = new StringQuoter(delimiter.charAt(0));
SampleResult sample = event.getResult();
SampleSaveConfiguration saveConfig = sample.getSaveConfig();
if (saveConfig.saveTimestamp()) {
if (saveConfig.printMilliseconds()) {
text.append(sample.getTimeStamp());
} else if (saveConfig.threadSafeLenientFormatter() != null) {
String stamp = saveConfig.threadSafeLenientFormatter().format(
new Date(sample.getTimeStamp()));
text.append(stamp);
}
}
if (saveConfig.saveTime()) {
text.append(sample.getTime());
}
if (saveConfig.saveLabel()) {
text.append(sample.getSampleLabel());
}
if (saveConfig.saveCode()) {
text.append(sample.getResponseCode());
}
if (saveConfig.saveMessage()) {
text.append(sample.getResponseMessage());
}
if (saveConfig.saveThreadName()) {
text.append(sample.getThreadName());
}
if (saveConfig.saveDataType()) {
text.append(sample.getDataType());
}
if (saveConfig.saveSuccess()) {
text.append(sample.isSuccessful());
}
if (saveConfig.saveAssertionResultsFailureMessage()) {
String message = null;
AssertionResult[] results = sample.getAssertionResults();
if (results != null) {
// Find the first non-null message
for (AssertionResult result : results) {
message = result.getFailureMessage();
if (message != null) {
break;
}
}
}
if (message != null) {
text.append(message);
} else {
text.append(""); // Need to append something so delimiter is
// added
}
}
if (saveConfig.saveBytes()) {
text.append(sample.getBytesAsLong());
}
if (saveConfig.saveSentBytes()) {
text.append(sample.getSentBytes());
}
if (saveConfig.saveThreadCounts()) {
text.append(sample.getGroupThreads());
text.append(sample.getAllThreads());
}
if (saveConfig.saveUrl()) {
text.append(sample.getURL());
}
if (saveConfig.saveFileName()) {
text.append(sample.getResultFileName());
}
if (saveConfig.saveLatency()) {
text.append(sample.getLatency());
}
if (saveConfig.saveEncoding()) {
text.append(sample.getDataEncodingWithDefault());
}
if (saveConfig.saveSampleCount()) {
// Need both sample and error count to be any use
text.append(sample.getSampleCount());
text.append(sample.getErrorCount());
}
if (saveConfig.saveHostname()) {
text.append(event.getHostname());
}
if (saveConfig.saveIdleTime()) {
text.append(event.getResult().getIdleTime());
}
if (saveConfig.saveConnectTime()) {
text.append(sample.getConnectTime());
}
for (int i = 0; i < SampleEvent.getVarCount(); i++) {
text.append(event.getVarValue(i));
}
return text.toString();
}
// =================================== CSV quote/unquote handling
// ==============================
/*
* Private versions of what might eventually be part of Commons-CSV or
* Commons-Lang/Io...
*/
/**
* <p> Returns a <code>String</code> value for a character-delimited column
* value enclosed in the quote character, if required. </p>
*
* <p> If the value contains a special character, then the String value is
* returned enclosed in the quote character. </p>
*
* <p> Any quote characters in the value are doubled up. </p>
*
* <p> If the value does not contain any special characters, then the String
* value is returned unchanged. </p>
*
* <p> N.B. The list of special characters includes the quote character.
* </p>
*
* @param input the input column String, may be null (without enclosing
* delimiters)
*
* @param specialChars special characters; second one must be the quote
* character
*
* @return the input String, enclosed in quote characters if the value
* contains a special character, <code>null</code> for null string input
*/
public static String quoteDelimiters(String input, char[] specialChars) {
if (StringUtils.containsNone(input, specialChars)) {
return input;
}
StringBuilder buffer = new StringBuilder(input.length() + 10);
final char quote = specialChars[1];
buffer.append(quote);
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == quote) {
buffer.append(quote); // double the quote char
}
buffer.append(c);
}
buffer.append(quote);
return buffer.toString();
}
// State of the parser
private enum ParserState {INITIAL, PLAIN, QUOTED, EMBEDDEDQUOTE}
public static final char QUOTING_CHAR = '"';
/**
* Reads from file and splits input into strings according to the delimiter,
* taking note of quoted strings.
* <p>
* Handles DOS (CRLF), Unix (LF), and Mac (CR) line-endings equally.
* <p>
* A blank line - or a quoted blank line - both return an array containing
* a single empty String.
* @param infile
* input file - must support mark(1)
* @param delim
* delimiter (e.g. comma)
* @return array of strings, will be empty if there is no data, i.e. if the input is at EOF.
* @throws IOException
* also for unexpected quote characters
*/
public static String[] csvReadFile(BufferedReader infile, char delim)
throws IOException {
int ch;
ParserState state = ParserState.INITIAL;
List<String> list = new ArrayList<>();
CharArrayWriter baos = new CharArrayWriter(200);
boolean push = false;
while (-1 != (ch = infile.read())) {
push = false;
switch (state) {
case INITIAL:
if (ch == QUOTING_CHAR) {
state = ParserState.QUOTED;
} else if (isDelimOrEOL(delim, ch)) {
push = true;