|
30 | 30 | package com.jcabi.xml;
|
31 | 31 |
|
32 | 32 | import com.google.common.collect.Iterables;
|
| 33 | +import com.jcabi.log.Logger; |
33 | 34 | import com.jcabi.matchers.XhtmlMatchers;
|
34 | 35 | import java.io.ByteArrayInputStream;
|
35 | 36 | import java.io.File;
|
36 | 37 | import java.io.IOException;
|
| 38 | +import java.nio.charset.StandardCharsets; |
37 | 39 | import java.nio.file.Files;
|
38 | 40 | import java.security.SecureRandom;
|
39 | 41 | import java.util.Arrays;
|
|
47 | 49 | import java.util.concurrent.Executors;
|
48 | 50 | import java.util.concurrent.TimeUnit;
|
49 | 51 | import java.util.concurrent.atomic.AtomicInteger;
|
| 52 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 53 | +import javax.xml.parsers.ParserConfigurationException; |
50 | 54 | import org.apache.commons.lang3.RandomStringUtils;
|
51 | 55 | import org.apache.commons.lang3.StringUtils;
|
52 | 56 | import org.cactoos.io.ResourceOf;
|
|
61 | 65 | import org.w3c.dom.Document;
|
62 | 66 | import org.w3c.dom.Element;
|
63 | 67 | import org.w3c.dom.Node;
|
| 68 | +import org.xml.sax.InputSource; |
| 69 | +import org.xml.sax.SAXException; |
64 | 70 | import org.xml.sax.SAXParseException;
|
65 | 71 |
|
66 | 72 | /**
|
@@ -670,4 +676,48 @@ void validatesMultipleXmlsInThreads() throws Exception {
|
670 | 676 | service.shutdownNow();
|
671 | 677 | }
|
672 | 678 |
|
| 679 | + |
| 680 | + @Test |
| 681 | + void createsManyXmlDocuments() throws ParserConfigurationException, IOException, SAXException { |
| 682 | + 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 | + ); |
| 691 | + final long startSimple = System.nanoTime(); |
| 692 | + final String expected = "payment"; |
| 693 | + for (int i = 0; i < 10_000; ++i) { |
| 694 | + final Document parse = factory.newDocumentBuilder() |
| 695 | + .parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); |
| 696 | + final String actual = parse.getFirstChild().getNodeName(); |
| 697 | + MatcherAssert.assertThat( |
| 698 | + actual, |
| 699 | + Matchers.equalTo(expected) |
| 700 | + ); |
| 701 | + } |
| 702 | + final long endSimple = System.nanoTime(); |
| 703 | + System.out.println( |
| 704 | + "Default approach to create XML timing: " + (endSimple - startSimple) / 1_000_000 + " ms"); |
| 705 | + Logger.info(this, "Time: %[ms]s", (endSimple - startSimple) / 1000); |
| 706 | + final long start = System.nanoTime(); |
| 707 | + for (int i = 0; i < 10_000; ++i) { |
| 708 | + ; |
| 709 | + final String actual = new XMLDocument( |
| 710 | + new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))) |
| 711 | + .node() |
| 712 | + .getFirstChild().getNodeName(); |
| 713 | + MatcherAssert.assertThat( |
| 714 | + actual, |
| 715 | + Matchers.equalTo(expected) |
| 716 | + ); |
| 717 | + } |
| 718 | + final long end = System.nanoTime(); |
| 719 | + System.out.println( |
| 720 | + "jcabi-xml approach to create XML timing: " + (end - start) / 1_000_000 + " ms" |
| 721 | + ); |
| 722 | + } |
673 | 723 | }
|
0 commit comments