This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-csv.git
The following commit(s) were added to refs/heads/master by this push: new 66da39d Better resource management. 66da39d is described below commit 66da39dd6f6fbd7001cb9cfc4be3156e56857b6f Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Jul 9 11:26:50 2021 -0400 Better resource management. --- .../apache/commons/csv/perf/PerformanceTest.java | 26 +++++++++++++--------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java index 33ffa95..90c549b 100644 --- a/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java +++ b/src/test/java/org/apache/commons/csv/perf/PerformanceTest.java @@ -30,6 +30,7 @@ import java.io.Reader; 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 class PerformanceTest { 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 @@ public class PerformanceTest { 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 class PerformanceTest { 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; } }