Skip to content

Commit 65d44b2

Browse files
author
Olivier Chédru
authored
Merge pull request #108 from rzymek/junit5
Junit5
2 parents e6b2e9a + b978577 commit 65d44b2

File tree

23 files changed

+443
-650
lines changed

23 files changed

+443
-650
lines changed

e2e/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Benchmarks
2+
3+
All benchmarks classes must end with `Benchmark` and extend `BenchmarkLauncher`.
4+
5+
mvn clean test -Pbench
6+
7+
CSV results will be written to `target` directory with the name of the class (e.g. `ReaderBenchmark.csv`)
8+
9+
## Tests
10+
11+
Run tests with
12+
13+
mvn clean test

e2e/pom.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.dhatim</groupId>
6+
<artifactId>fastexcel-parent</artifactId>
7+
<version>0-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>fastexcel-e2e</artifactId>
10+
<name>Fastexcel End-to-End tests</name>
11+
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.dhatim</groupId>
15+
<artifactId>fastexcel</artifactId>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.dhatim</groupId>
19+
<artifactId>fastexcel-reader</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>commons-io</groupId>
23+
<artifactId>commons-io</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.apache.poi</groupId>
28+
<artifactId>poi-ooxml</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.assertj</groupId>
32+
<artifactId>assertj-core</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.openjdk.jmh</groupId>
42+
<artifactId>jmh-core</artifactId>
43+
<version>1.21</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.openjdk.jmh</groupId>
48+
<artifactId>jmh-generator-annprocess</artifactId>
49+
<version>1.21</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.monitorjbl</groupId>
54+
<artifactId>xlsx-streamer</artifactId>
55+
<version>2.1.0</version>
56+
<scope>test</scope>
57+
<exclusions>
58+
<exclusion>
59+
<groupId>org.apache.poi</groupId>
60+
<artifactId>poi</artifactId>
61+
</exclusion>
62+
<exclusion>
63+
<groupId>org.apache.poi</groupId>
64+
<artifactId>poi-ooxml-schemas</artifactId>
65+
</exclusion>
66+
</exclusions>
67+
</dependency>
68+
</dependencies>
69+
<build>
70+
<plugins>
71+
<plugin>
72+
<artifactId>maven-surefire-plugin</artifactId>
73+
<configuration>
74+
<argLine>-Xmx50M</argLine>
75+
</configuration>
76+
</plugin>
77+
</plugins>
78+
</build>
79+
<profiles>
80+
<profile>
81+
<id>bench</id>
82+
<build>
83+
<plugins>
84+
<plugin>
85+
<artifactId>maven-surefire-plugin</artifactId>
86+
<configuration>
87+
<includes>
88+
<include>**/*Benchmark.java</include>
89+
</includes>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
</profile>
95+
</profiles>
96+
</project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.dhatim.fastexcel.benchmarks;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openjdk.jmh.annotations.*;
5+
import org.openjdk.jmh.results.format.ResultFormatType;
6+
import org.openjdk.jmh.runner.Runner;
7+
import org.openjdk.jmh.runner.RunnerException;
8+
import org.openjdk.jmh.runner.options.Options;
9+
import org.openjdk.jmh.runner.options.OptionsBuilder;
10+
11+
import java.util.regex.Pattern;
12+
13+
@BenchmarkMode(Mode.SingleShotTime)
14+
@Warmup(iterations = 3)
15+
@Measurement(iterations = 5)
16+
@Fork(1)
17+
@Threads(1)
18+
public abstract class BenchmarkLauncher {
19+
20+
@Test
21+
public void launchBenchmarks() throws RunnerException {
22+
Options options = new OptionsBuilder()
23+
.include(Pattern.quote(getClass().getName()))
24+
.shouldFailOnError(true)
25+
.result("target/" + getClass().getSimpleName() + ".csv")
26+
.resultFormat(ResultFormatType.CSV)
27+
.build();
28+
new Runner(options).run();
29+
}
30+
31+
}

fastexcel-reader/src/test/java/org/dhatim/fastexcel/reader/BenchmarksTest.java renamed to e2e/src/test/java/org.dhatim.fastexcel.benchmarks/ReaderBenchmark.java

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.dhatim.fastexcel.reader;
16+
package org.dhatim.fastexcel.benchmarks;
1717

1818
import com.monitorjbl.xlsx.StreamingReader;
1919
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
@@ -27,14 +27,10 @@
2727
import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler;
2828
import org.apache.poi.xssf.model.StylesTable;
2929
import org.apache.poi.xssf.usermodel.XSSFComment;
30-
import org.junit.jupiter.api.Test;
30+
import org.dhatim.fastexcel.reader.ReadableWorkbook;
31+
import org.dhatim.fastexcel.reader.Row;
32+
import org.dhatim.fastexcel.reader.Sheet;
3133
import org.openjdk.jmh.annotations.Benchmark;
32-
import org.openjdk.jmh.annotations.Mode;
33-
import org.openjdk.jmh.results.format.ResultFormatType;
34-
import org.openjdk.jmh.runner.Runner;
35-
import org.openjdk.jmh.runner.RunnerException;
36-
import org.openjdk.jmh.runner.options.Options;
37-
import org.openjdk.jmh.runner.options.OptionsBuilder;
3834
import org.xml.sax.ContentHandler;
3935
import org.xml.sax.InputSource;
4036
import org.xml.sax.SAXException;
@@ -47,13 +43,12 @@
4743
import java.io.InputStream;
4844
import java.util.Spliterator;
4945
import java.util.Spliterators;
50-
import java.util.concurrent.TimeUnit;
5146
import java.util.stream.Stream;
5247
import java.util.stream.StreamSupport;
5348

5449
import static org.junit.jupiter.api.Assertions.assertEquals;
5550

56-
public class BenchmarksTest {
51+
public class ReaderBenchmark extends BenchmarkLauncher {
5752

5853
private static final long RESULT = 2147385345;
5954

@@ -171,26 +166,8 @@ public long monitorjbl() throws IOException {
171166
return sum;
172167
}
173168

174-
@Test
175-
public void benchmarks() throws RunnerException {
176-
String foo = BenchmarksTest.class.getName() + "\\..*";
177-
Options options = new OptionsBuilder().include(foo)
178-
.mode(Mode.SingleShotTime)
179-
.warmupIterations(3)
180-
.warmupBatchSize(1)
181-
.measurementIterations(5)
182-
.threads(1)
183-
.forks(1)
184-
.timeUnit(TimeUnit.MILLISECONDS)
185-
.shouldFailOnError(true)
186-
.resultFormat(ResultFormatType.CSV)
187-
.result("jmh.csv")
188-
.build();
189-
new Runner(options).run();
190-
}
191-
192169
private static InputStream openResource(String name) {
193-
InputStream result = BenchmarksTest.class.getResourceAsStream(name);
170+
InputStream result = ReaderBenchmark.class.getResourceAsStream(name);
194171
if (result == null) {
195172
throw new IllegalStateException("Cannot read resource " + name);
196173
}

fastexcel-writer/src/test/java/org/dhatim/fastexcel/Benchmarks.java renamed to e2e/src/test/java/org.dhatim.fastexcel.benchmarks/WriterBenchmark.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.dhatim.fastexcel;
16+
package org.dhatim.fastexcel.benchmarks;
1717

1818
import java.io.IOException;
1919
import java.util.Date;
@@ -26,13 +26,15 @@
2626
import org.apache.poi.ss.usermodel.Sheet;
2727
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
2828
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
29+
import org.dhatim.fastexcel.Workbook;
30+
import org.dhatim.fastexcel.Worksheet;
2931
import org.openjdk.jmh.annotations.Benchmark;
3032

3133
/**
3234
* Compare write performance between this library and
3335
* <a href="https://poi.apache.org/">Apache POI</a>.
3436
*/
35-
public class Benchmarks {
37+
public class WriterBenchmark extends BenchmarkLauncher {
3638

3739
private static final int NB_ROWS = 100_000;
3840

@@ -79,4 +81,5 @@ private int poiPopulate(org.apache.poi.ss.usermodel.Workbook wb) throws Exceptio
7981
wb.write(count);
8082
return count.getCount();
8183
}
84+
8285
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.dhatim.fastexcel;
2+
3+
import org.dhatim.fastexcel.reader.ReadableWorkbook;
4+
import org.dhatim.fastexcel.reader.Row;
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.IOException;
12+
import java.io.OutputStream;
13+
import java.text.MessageFormat;
14+
import java.util.stream.Stream;
15+
16+
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
19+
public class MemoryUsageTest {
20+
private static final File testFile = new File("target/memtest.xlsx");
21+
public static final int ROWS = 25_000;
22+
public static final int SHEETS = 3;
23+
public static final int COLS = 200;
24+
public static final int FLUSH_EVERY_NR_OR_ROWS = 100;
25+
26+
@BeforeAll
27+
static void checkMemoryLimit() {
28+
assertThat(Runtime.getRuntime().totalMemory() / (1024 * 1024)).isLessThan(51);
29+
}
30+
31+
@Test
32+
void writeAndReadFile() throws IOException {
33+
try (OutputStream out = new FileOutputStream(testFile)) {
34+
write(out);
35+
}
36+
37+
try (ReadableWorkbook wb = new ReadableWorkbook(testFile)) {
38+
read(wb);
39+
}
40+
}
41+
42+
private void write(OutputStream out) throws IOException {
43+
Workbook wb = new Workbook(out, "test", "1.0");
44+
for (int s = 0; s < SHEETS; s++) {
45+
Worksheet sheet = wb.newWorksheet("sheet " + s);
46+
for (int r = 0; r < ROWS; r++) {
47+
printProgress("writing", s, r);
48+
for (int c = 0; c < COLS; c++) {
49+
sheet.value(r, c, valueFor(r, c));
50+
}
51+
if (r % FLUSH_EVERY_NR_OR_ROWS == 0) {
52+
sheet.flush();
53+
}
54+
}
55+
sheet.finish();
56+
}
57+
wb.finish();
58+
}
59+
60+
private void read(ReadableWorkbook wb) {
61+
wb.getSheets().forEach(sheet -> {
62+
try (Stream<Row> rows = sheet.openStream()) {
63+
rows.forEach(r -> {
64+
printProgress("reading", sheet.getIndex(), r.getRowNum() - 1);
65+
for (int c = 0; c < r.getCellCount(); c++) {
66+
assertEquals(
67+
valueFor(r.getRowNum() - 1, c),
68+
r.getCell(c).asNumber().doubleValue(),
69+
1e-5);
70+
71+
}
72+
});
73+
} catch (IOException e) {
74+
throw new RuntimeException(e);
75+
}
76+
});
77+
}
78+
79+
@AfterAll
80+
static void cleanup() {
81+
testFile.delete();
82+
}
83+
84+
private static double valueFor(int r, int c) {
85+
return (double) r * COLS + c;
86+
}
87+
88+
private static void printProgress(String prefix, int sheetIndex, int r) {
89+
int total = ROWS;
90+
if (r % (total / 100) == 0) {
91+
String msg = MessageFormat.format("{0} sheet {2}/{3}: {1}%",
92+
prefix, 100 * r / total, sheetIndex + 1, SHEETS);
93+
System.out.println(msg);
94+
}
95+
}
96+
97+
}
4.11 MB
Binary file not shown.

0 commit comments

Comments
 (0)