Skip to content

Commit f962d1d

Browse files
committed
More Sonar fixes
1 parent da8b126 commit f962d1d

File tree

3 files changed

+154
-107
lines changed

3 files changed

+154
-107
lines changed

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,7 @@ public static void main(String [] args, PerfTestOptions perfTestOptions) {
148148

149149
PrintWriter output;
150150
if (outputFile != null) {
151-
File file = new File(outputFile);
152-
if (file.exists()) {
153-
boolean deleted = file.delete();
154-
if (!deleted) {
155-
LOGGER.warn("Could not delete existing CSV file, will try to append at the end of the file");
156-
}
157-
}
158-
output = new PrintWriter(new BufferedWriter(new FileWriter(file, true)), true); //NOSONAR
159-
Runtime.getRuntime().addShutdownHook(new Thread(() -> output.close()));
151+
output = openCsvFileForWriting(outputFile);
160152
} else {
161153
output = null;
162154
}
@@ -286,6 +278,20 @@ public static void main(String [] args, PerfTestOptions perfTestOptions) {
286278
}
287279
}
288280

281+
private static PrintWriter openCsvFileForWriting(String outputFile) throws IOException {
282+
PrintWriter output;
283+
File file = new File(outputFile);
284+
if (file.exists()) {
285+
boolean deleted = file.delete();
286+
if (!deleted) {
287+
LOGGER.warn("Could not delete existing CSV file, will try to append at the end of the file");
288+
}
289+
}
290+
output = new PrintWriter(new BufferedWriter(new FileWriter(file, true)), true); //NOSONAR
291+
Runtime.getRuntime().addShutdownHook(new Thread(() -> output.close()));
292+
return output;
293+
}
294+
289295
private static ConnectionFactory configureNioIfRequested(CommandLineProxy cmd, ConnectionFactory factory) {
290296
int nbThreads = intArg(cmd, "niot", -1);
291297
int executorSize = intArg(cmd, "niotp", -1);
@@ -507,7 +513,7 @@ static boolean boolArg(CommandLineProxy cmd, String opt, boolean def) {
507513
return Boolean.parseBoolean(cmd.getOptionValue(opt, Boolean.toString(def)));
508514
}
509515

510-
static List<?> lstArg(CommandLineProxy cmd, char opt) {
516+
static List<Object> lstArg(CommandLineProxy cmd, char opt) {
511517
String[] vals = cmd.getOptionValues(opt);
512518
if (vals == null) {
513519
vals = new String[] {};

src/main/java/com/rabbitmq/perf/PrintlnStats.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Class to output stats on the console and in a CSV file.
2828
*/
2929
class PrintlnStats extends Stats {
30+
public static final String MESSAGE_RATE_LABEL = "msg/s";
3031
private final boolean sendStatsEnabled;
3132
private final boolean recvStatsEnabled;
3233
private final boolean returnStatsEnabled;
@@ -139,10 +140,7 @@ protected void report(long now) {
139140
long[] consumerLatencyStats = null;
140141
long[] confirmLatencyStats = null;
141142
if (legacyMetrics && latencyCountInterval > 0) {
142-
output += ", min/avg/max latency: " +
143-
minLatency / 1000L + "/" +
144-
cumulativeLatencyInterval / (1000L * latencyCountInterval) + "/" +
145-
maxLatency / 1000L + " µs ";
143+
output += legacyMetrics();
146144
} else {
147145
if (shouldDisplayConsumerLatency() || shouldDisplayConfirmLatency()) {
148146
output += ", min/median/75th/95th/99th ";
@@ -172,6 +170,17 @@ protected void report(long now) {
172170
}
173171

174172
this.out.println(output);
173+
writeToCsvIfNecessary(now, ratePublished, rateReturned, rateConfirmed, rateNacked, rateConsumed, consumerLatencyStats, confirmLatencyStats);
174+
}
175+
176+
private String legacyMetrics() {
177+
return ", min/avg/max latency: " +
178+
minLatency / 1000L + "/" +
179+
cumulativeLatencyInterval / (1000L * latencyCountInterval) + "/" +
180+
maxLatency / 1000L + " µs ";
181+
}
182+
183+
private void writeToCsvIfNecessary(long now, double ratePublished, double rateReturned, double rateConfirmed, double rateNacked, double rateConsumed, long[] consumerLatencyStats, long[] confirmLatencyStats) {
175184
if (this.csvOut != null) {
176185
if (consumerLatencyStats == null) {
177186
consumerLatencyStats = getStats(latency);
@@ -202,7 +211,6 @@ protected void report(long now) {
202211

203212
);
204213
}
205-
206214
}
207215

208216
boolean shouldDisplayConsumerLatency() {
@@ -233,7 +241,7 @@ private long div(double p) {
233241

234242
private String getRate(String descr, double rate, boolean display) {
235243
if (display) {
236-
return ", " + descr + ": " + formatRate(rate) + " msg/s";
244+
return ", " + descr + ": " + formatRate(rate) + " " + MESSAGE_RATE_LABEL;
237245
} else {
238246
return "";
239247
}
@@ -244,13 +252,13 @@ public void printFinal() {
244252

245253
System.out.println("id: " + testID + ", sending rate avg: " +
246254
formatRate(sendCountTotal * 1000.0 / (now - startTime)) +
247-
" msg/s");
255+
" " + MESSAGE_RATE_LABEL);
248256

249257
long elapsed = now - startTime;
250258
if (elapsed > 0) {
251259
System.out.println("id: " + testID + ", receiving rate avg: " +
252260
formatRate(recvCountTotal * 1000.0 / elapsed) +
253-
" msg/s");
261+
" " + MESSAGE_RATE_LABEL);
254262
}
255263
}
256264
}

0 commit comments

Comments
 (0)