|
37 | 37 | import java.io.IOException;
|
38 | 38 | import java.nio.charset.StandardCharsets;
|
39 | 39 | import java.nio.file.Files;
|
| 40 | +import java.nio.file.Path; |
40 | 41 | import java.security.SecureRandom;
|
41 | 42 | import java.util.Arrays;
|
42 | 43 | import java.util.Collection;
|
|
49 | 50 | import java.util.concurrent.Executors;
|
50 | 51 | import java.util.concurrent.TimeUnit;
|
51 | 52 | import java.util.concurrent.atomic.AtomicInteger;
|
| 53 | +import java.util.stream.IntStream; |
52 | 54 | import javax.xml.parsers.DocumentBuilderFactory;
|
53 | 55 | import javax.xml.parsers.ParserConfigurationException;
|
54 | 56 | import org.apache.commons.lang3.RandomStringUtils;
|
|
62 | 64 | import org.hamcrest.core.IsEqual;
|
63 | 65 | import org.junit.jupiter.api.Disabled;
|
64 | 66 | import org.junit.jupiter.api.Test;
|
| 67 | +import org.junit.jupiter.api.io.TempDir; |
65 | 68 | import org.w3c.dom.Document;
|
66 | 69 | import org.w3c.dom.Element;
|
67 | 70 | import org.w3c.dom.Node;
|
@@ -680,16 +683,9 @@ void validatesMultipleXmlsInThreads() throws Exception {
|
680 | 683 | @Test
|
681 | 684 | void createsManyXmlDocuments() throws ParserConfigurationException, IOException, SAXException {
|
682 | 685 | 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(); |
691 | 687 | final long startSimple = System.nanoTime();
|
692 |
| - final String expected = "payment"; |
| 688 | + final String expected = "root"; |
693 | 689 | for (int i = 0; i < 10_000; ++i) {
|
694 | 690 | final Document parse = factory.newDocumentBuilder()
|
695 | 691 | .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
|
@@ -720,4 +716,58 @@ void createsManyXmlDocuments() throws ParserConfigurationException, IOException,
|
720 | 716 | "jcabi-xml approach to create XML timing: " + (end - start) / 1_000_000 + " ms"
|
721 | 717 | );
|
722 | 718 | }
|
| 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 | + |
723 | 773 | }
|
0 commit comments