use buffered writer

despite my previous test, writing the csv is a little bit faster with
a buffered writer (~ 10-15%)
This commit is contained in:
2017-09-23 20:22:15 +02:00
parent 9d66c9e0da
commit 955127dc4a

View File

@@ -1,5 +1,6 @@
package org.lucares.recommind.logs;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -129,10 +130,12 @@ public class Plotter {
if (minDate.until(maxDate, ChronoUnit.WEEKS) > 1) {
formatX = "%Y-%m-%d";
rotateX = 0;
} else {
} else if (minDate.until(maxDate, ChronoUnit.SECONDS) > 10) {
formatX = "%Y-%m-%d %H:%M:%S";
rotateX = gnuplotSettings.getRotateXAxisLabel();
} else {
formatX = "%Y-%m-%d %H:%M:%.3S";
rotateX = gnuplotSettings.getRotateXAxisLabel();
}
formattedMinDate = minDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
formattedMaxDate = maxDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
@@ -214,9 +217,11 @@ public class Plotter {
final long fromEpochMilli = dateFrom.toInstant().toEpochMilli();
final long toEpochMilli = dateTo.toInstant().toEpochMilli();
long maxValue = 0;
long ignoredValues = 0;
final int separator = ',';
final int newline = '\n';
try (final Writer output = new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII);) {
final DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
try (final Writer output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.US_ASCII));) {
final Iterator<Entry> it = entries.iterator();
while (it.hasNext()) {
@@ -225,7 +230,7 @@ public class Plotter {
if (fromEpochMilli <= entry.getEpochMilli() && entry.getEpochMilli() <= toEpochMilli) {
final OffsetDateTime date = entry.getDate();
final String value = String.valueOf(entry.getValue());
final String formattedDate = date.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
final String formattedDate = date.format(formatter);
output.write(formattedDate);
output.write(separator);
output.write(value);
@@ -233,11 +238,13 @@ public class Plotter {
count++;
maxValue = Math.max(maxValue, entry.getValue());
}else {
ignoredValues++;
}
}
}
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms", count, (System.nanoTime() - start) / 1_000_000.0);
METRICS_LOGGER.debug("wrote {} values to csv in: {}ms (ignored {} values)", count, (System.nanoTime() - start) / 1_000_000.0, ignoredValues);
return new CsvSummary(count, maxValue);
}
}