Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BufferedWriter to output Job for 200 fold performance improvement #3583

Merged
merged 1 commit into from Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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, files.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