Skip to content

Commit

Permalink
Use BufferedWriter to output Job for 200 fold performance improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Jarno Elovirta <jarno@elovirta.com>
  • Loading branch information
jelovirt committed Sep 10, 2020
1 parent 13a04a1 commit 91ff96d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/main/java/org/dita/dost/util/Job.java
Expand Up @@ -25,6 +25,8 @@
import java.io.*;
import java.lang.reflect.Field;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand Down Expand Up @@ -315,31 +317,24 @@ public void write() throws IOException {
if (!tempDir.exists() && !tempDir.mkdirs()) {
throw new IOException("Failed to create " + tempDir + " directory");
}
OutputStream outStream = null;
XMLStreamWriter out = null;
try {
outStream = new FileOutputStream(jobFile);
out = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream, "UTF-8");
serialize(out, prop, files.values());
} catch (final IOException e) {
throw new IOException("Failed to write file: " + e.getMessage());
} catch (final XMLStreamException e) {
throw new IOException("Failed to serialize job file: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (final XMLStreamException e) {
throw new IOException("Failed to close file: " + e.getMessage());
}
}
if (outStream != null) {
try {
outStream.close();
} catch (final IOException e) {
throw new IOException("Failed to close file: " + e.getMessage());
try (Writer outStream = Files.newBufferedWriter(jobFile.toPath())) {
XMLStreamWriter out = null;
try {
out = XMLOutputFactory.newInstance().createXMLStreamWriter(outStream);
serialize(out, prop, byTemp.values());
} catch (final XMLStreamException e) {
throw new IOException("Failed to serialize job file: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (final XMLStreamException e) {
throw new IOException("Failed to close file: " + e.getMessage());
}
}
}
} catch (final IOException e) {
throw new IOException("Failed to write file: " + e.getMessage());
}
lastModified = jobFile.lastModified();
}
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/dita/dost/util/JobTest.java
Expand Up @@ -21,6 +21,7 @@
import org.dita.dost.store.StreamStore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import org.dita.dost.TestUtils;
Expand Down Expand Up @@ -72,6 +73,25 @@ public void testGetValue() throws URISyntaxException {
assertEquals(new URI("file:/foo/bar"), job.getInputDir());
}

@Test
@Ignore
public void write_performance_large() throws IOException {
for (int i = 0; i < 60_000; i++) {
job.add(Job.FileInfo.builder()
.src(new File(tempDir, "topic_" + i + ".dita").toURI())
.uri(new File("topic_" + i + ".dita").toURI())
.result(new File(tempDir, "topic_" + i + ".html").toURI())
.format("dita")
.hasKeyref(true)
.hasLink(true)
.build());
}
final long start = System.currentTimeMillis();
job.write();
final long end = System.currentTimeMillis();
System.out.println(((end - start)) + " ms");
}

@AfterClass
public static void tearDown() throws IOException {
TestUtils.forceDelete(tempDir);
Expand Down

0 comments on commit 91ff96d

Please sign in to comment.