Skip to content

Commit 5539f70

Browse files
committedDec 4, 2024
feat(#277): add one more performance test
1 parent 8cb22d5 commit 5539f70

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed
 

‎src/test/java/com/jcabi/xml/XMLDocumentTest.java

+59-9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.IOException;
3838
import java.nio.charset.StandardCharsets;
3939
import java.nio.file.Files;
40+
import java.nio.file.Path;
4041
import java.security.SecureRandom;
4142
import java.util.Arrays;
4243
import java.util.Collection;
@@ -49,6 +50,7 @@
4950
import java.util.concurrent.Executors;
5051
import java.util.concurrent.TimeUnit;
5152
import java.util.concurrent.atomic.AtomicInteger;
53+
import java.util.stream.IntStream;
5254
import javax.xml.parsers.DocumentBuilderFactory;
5355
import javax.xml.parsers.ParserConfigurationException;
5456
import org.apache.commons.lang3.RandomStringUtils;
@@ -62,6 +64,7 @@
6264
import org.hamcrest.core.IsEqual;
6365
import org.junit.jupiter.api.Disabled;
6466
import org.junit.jupiter.api.Test;
67+
import org.junit.jupiter.api.io.TempDir;
6568
import org.w3c.dom.Document;
6669
import org.w3c.dom.Element;
6770
import org.w3c.dom.Node;
@@ -680,16 +683,9 @@ void validatesMultipleXmlsInThreads() throws Exception {
680683
@Test
681684
void createsManyXmlDocuments() throws ParserConfigurationException, IOException, SAXException {
682685
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
683-
String xml = StringUtils.join(
684-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
685-
"<payment><id>333</id>",
686-
"<date>1-Jan-2013</date>",
687-
"<debit>test-1</debit>",
688-
"<credit>test-2</credit>",
689-
"</payment>"
690-
);
686+
String xml = this.large();
691687
final long startSimple = System.nanoTime();
692-
final String expected = "payment";
688+
final String expected = "root";
693689
for (int i = 0; i < 10_000; ++i) {
694690
final Document parse = factory.newDocumentBuilder()
695691
.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
@@ -720,4 +716,58 @@ void createsManyXmlDocuments() throws ParserConfigurationException, IOException,
720716
"jcabi-xml approach to create XML timing: " + (end - start) / 1_000_000 + " ms"
721717
);
722718
}
719+
720+
721+
@Test
722+
void createsXmlFromFile(
723+
@TempDir final Path temp
724+
) throws IOException, ParserConfigurationException, SAXException {
725+
final Path xml = temp.resolve("test.xml");
726+
String content = this.large();
727+
Files.write(xml, content.getBytes(StandardCharsets.UTF_8));
728+
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
729+
final long startSimple = System.nanoTime();
730+
final String expected = "root";
731+
for (int i = 0; i < 10_000; ++i) {
732+
final Document parse = factory.newDocumentBuilder().parse(xml.toFile());
733+
final String actual = parse.getFirstChild().getNodeName();
734+
MatcherAssert.assertThat(
735+
actual,
736+
Matchers.equalTo(expected)
737+
);
738+
}
739+
final long endSimple = System.nanoTime();
740+
System.out.println(
741+
"Default approach to create XML timing: " + (endSimple - startSimple) / 1_000_000 + " ms");
742+
743+
Logger.info(this, "Time: %[ms]s", (endSimple - startSimple) / 1000);
744+
final long start = System.nanoTime();
745+
for (int i = 0; i < 10_000; ++i) {
746+
final String actual = new XMLDocument(xml).node()
747+
.getFirstChild().getNodeName();
748+
MatcherAssert.assertThat(
749+
actual,
750+
Matchers.equalTo(expected)
751+
);
752+
}
753+
final long end = System.nanoTime();
754+
System.out.println(
755+
"jcabi-xml approach to create XML timing: " + (end - start) / 1_000_000 + " ms"
756+
);
757+
}
758+
759+
private String large() {
760+
final StringBuilder builder = new StringBuilder(
761+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>").append("<root>");
762+
final String payment = StringUtils.join(
763+
"<payment><id>333</id>",
764+
"<date>1-Jan-2013</date>",
765+
"<debit>test-1</debit>",
766+
"<credit>test-2</credit>",
767+
"</payment>"
768+
);
769+
IntStream.range(0, 100).mapToObj(i -> payment).forEach(builder::append);
770+
return builder.append("</root>").toString();
771+
}
772+
723773
}

0 commit comments

Comments
 (0)
Please sign in to comment.