Skip to content
Permalink
Browse files
Better resource management.
  • Loading branch information
garydgregory committed Jul 9, 2021
1 parent d1602ac commit 66da39dd6f6fbd7001cb9cfc4be3156e56857b6f
Showing 1 changed file with 16 additions and 10 deletions.
@@ -30,6 +30,7 @@
import java.util.zip.GZIPInputStream;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeAll;
@@ -54,9 +55,10 @@ public static void setUpClass() throws FileNotFoundException, IOException {
return;
}
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
try (final InputStream input = new GZIPInputStream(
try (
final InputStream input = new GZIPInputStream(
new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
final OutputStream output = new FileOutputStream(BIG_FILE)) {
final OutputStream output = new FileOutputStream(BIG_FILE)) {
IOUtils.copy(input, output);
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
}
@@ -66,14 +68,17 @@ private BufferedReader createBufferedReader() throws IOException {
return new BufferedReader(new FileReader(BIG_FILE));
}

private long parse(final Reader in, final boolean traverseColumns) throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.withIgnoreSurroundingSpaces(false);
private long parse(final Reader reader, final boolean traverseColumns) throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setIgnoreSurroundingSpaces(false).build();
long recordCount = 0;
for (final CSVRecord record : format.parse(in)) {
recordCount++;
if (traverseColumns) {
for (@SuppressWarnings("unused") final String value : record) {
// do nothing for now
try (final CSVParser parser = format.parse(reader)) {
for (final CSVRecord record : parser) {
recordCount++;
if (traverseColumns) {
for (@SuppressWarnings("unused")
final String value : record) {
// do nothing for now
}
}
}
}
@@ -97,7 +102,8 @@ public long testParseBigFile(final boolean traverseColumns) throws Exception {
try (final BufferedReader reader = this.createBufferedReader()) {
final long count = this.parse(reader, traverseColumns);
final long totalMillis = System.currentTimeMillis() - startMillis;
this.println(String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
this.println(
String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
return totalMillis;
}
}

0 comments on commit 66da39d

Please sign in to comment.