File: PerfDOM4J.java

package info (click to toggle)
dom4j 2.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: java: 26,365; xml: 24,952; makefile: 8
file content (260 lines) | stat: -rw-r--r-- 9,074 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.XPath;
import org.dom4j.io.DOMWriter;
import org.dom4j.io.DocumentResult;
import org.dom4j.io.DocumentSource;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class PerfDOM4J {

    public static void main(String args[]) {
        Document doc;

        try {
            int numrec = 1;

            numrec = 10000;
            System.out.println("\n10000 Elements ------------------");
            doc = PerfDOM4J.createDocument(numrec, 20, 1);
            PerfDOM4J.createW3CDOM(doc);
            PerfDOM4J.write(doc, "dom4j_" + numrec + ".xml");
            // PerfDOM4J.parse(numrec,1);
            // PerfDOM4J.transform(doc,"item.xslt",1);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x1", 1);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x5000", 1);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x9999", 1);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x1", 1);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x5000", 1);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x9999", 1);
            PerfDOM4J.xpathNodes(doc, "/*/Item", 3);

            numrec = 1000;
            System.out.println("\n1000 Elements -------------------");
            doc = PerfDOM4J.createDocument(numrec, 20, 1);
            PerfDOM4J.createW3CDOM(doc);
            PerfDOM4J.write(doc, "dom4j_" + numrec + ".xml");
            PerfDOM4J.parse(numrec, 3);
            PerfDOM4J.transform(doc, "item.xslt", 3);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x1", 3);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x500", 3);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x999", 3);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x1", 3);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x500", 3);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x999", 3);
            PerfDOM4J.xpathNodes(doc, "/*/Item", 10);

            numrec = 100;
            System.out.println("\n100 Elements --------------------");
            doc = PerfDOM4J.createDocument(numrec, 20, 10);
            PerfDOM4J.createW3CDOM(doc);
            PerfDOM4J.write(doc, "dom4j_" + numrec + ".xml");
            PerfDOM4J.parse(numrec, 10);
            PerfDOM4J.transform(doc, "item.xslt", 10);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x1", 10);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x50", 10);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x99", 10);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x1", 10);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x50", 10);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x99", 10);
            PerfDOM4J.xpathNodes(doc, "/*/Item", 100);

            numrec = 10;
            System.out.println("\n10 Elements ---------------------");
            doc = PerfDOM4J.createDocument(numrec, 20, 100);
            PerfDOM4J.createW3CDOM(doc);
            PerfDOM4J.write(doc, "dom4j_" + numrec + ".xml");
            PerfDOM4J.parse(numrec, 100);
            PerfDOM4J.transform(doc, "item.xslt", 10);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x5", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x1", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x5", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x9", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/Item", 1000);

            numrec = 1;
            System.out.println("\n1 Element -----------------------");
            doc = PerfDOM4J.createDocument(numrec, 20, 100);
            PerfDOM4J.createW3CDOM(doc);
            PerfDOM4J.write(doc, "dom4j_" + numrec + ".xml");
            PerfDOM4J.parse(numrec, 100);
            PerfDOM4J.transform(doc, "item.xslt", 10);
            PerfDOM4J.xpath(doc, "/*/*/Attr1x1", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/*/Attr1x1", 1000);
            PerfDOM4J.xpathNodes(doc, "/*/Item", 1000);

        } catch (IOException ie) {
            ie.printStackTrace();
        }
    }

    public static Document createDocument(int iNumRecs, int iNumFlds, int pp) {

        double start = System.currentTimeMillis();
        Document document = null;
        for (int kk = 0; kk < pp; kk++) {
            document = DocumentHelper.createDocument();

            Element root = document.addElement("ItemResultSet");
            for (int ii = 0; ii < iNumRecs; ii++) {

                Element Record = root.addElement("Item");
                for (int jj = 0; jj < iNumFlds; jj++) {
                    Record.addElement("Attr" + jj + "x" + ii).addText(
                            "123456789");
                }

            }
        }
        double end = System.currentTimeMillis();

        System.err.println("Creation time  			:  " + (end - start) / pp);

        return document;
    }

    public static Document parse(int iNumRecs, int kk) {

        File file = new File("dom4j_" + iNumRecs + ".xml");
        double start = System.currentTimeMillis();
        Document document = null;
        for (int pp = 0; pp < kk; pp++) {
            try {
                SAXReader SAXrd = new SAXReader();
                SAXrd.read(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        double end = System.currentTimeMillis();

        System.err.println("Parsing time for 		:" + 1.000 * (end - start) / kk);

        return document;
    }

    public static void createW3CDOM(Document doc) {

        long start = System.currentTimeMillis();
        try {
            DOMWriter dw = new DOMWriter();
            dw.write(doc);

        } catch (Exception de) {
        }

        long end = System.currentTimeMillis();

        System.err.println("W3C Creation time for 		:" + (end - start));
    }

    public static void write(Document document, String name) throws IOException {

        long start = System.currentTimeMillis();
        // lets write to a file

        try {
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter writer = new XMLWriter(new FileWriter(name), format);
            writer.write(document);
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();

        System.err.println("DOM4J File write time 		:" + (end - start) + "  "
                + name);
    }

    public static void transform(Document xmlDoc, String xslFile, int kk) {

        System.err.println("DOM4J start transform ");
        int ii = 1;
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer(new StreamSource(
                    xslFile));

            long start = System.currentTimeMillis();
            for (ii = 0; ii < kk; ii++) {
                Source source = new DocumentSource(xmlDoc);
                DocumentResult result = new DocumentResult();
                transformer.transform(source, result);

                // output the transformed document
            }
            long end = System.currentTimeMillis();

            System.err
                    .println("DOM4J transform  time 		:" + (end - start) / ii);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void xpath(Document document, String xpathExp, int kk) {

        long start = System.currentTimeMillis();
        XPath xpath = document.createXPath(xpathExp);
        for (int ii = 0; ii < kk; ii++) {
            Node node = xpath.selectSingleNode(document);

            if ((node != null) & (ii == 0)) {
                String val = node.getStringValue();
                // System.out.println(val);
            }
        }

        long end = System.currentTimeMillis();
        System.err.println("DOM4J xpath  time 		:" + (end - start) / kk);
    }

    public static void xpathNodes(Document document, String xpathExp, int kk) {

        long start = System.currentTimeMillis();
        XPath xpath = document.createXPath(xpathExp);

        for (int ii = 0; ii < kk; ii++) {

            try {

                List nodeList = xpath.selectNodes(document);
                if ((nodeList != null) && (nodeList.size() > 0)) {
                    Node node = (Node) nodeList.get(0);
                    if ((node != null) & (ii == 0)) {
                        String val = node.getStringValue();
                        // System.out.println(val);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

            }

        }

        long end = System.currentTimeMillis();

        System.err.println("DOM4J xpath Nodes time 		:" + 1.000 * (end - start)
                / kk);
    }

}