File: PerfW3C.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 (250 lines) | stat: -rw-r--r-- 8,425 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

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xpath.XPathAPI;

import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class PerfW3C {

    public static void main(String args[]) {
        Document doc;
        System.err.println("W3C createDocument:");

        int numrec;
        long start = 0;
        long end = 0;

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

        numrec = 100;
        System.out.println("\n100 Elements ----------------------------------");
        doc = PerfW3C.createDocument(numrec, 20, 1);
        PerfW3C.write(doc, "w3c_" + numrec + ".xml");
        PerfW3C.transform(doc, "item.xslt", 10);
        PerfW3C.parse(numrec, 10);
        PerfW3C.xpath(doc, "/*/*/Attr0x1", 10);
        PerfW3C.xpath(doc, "/*/*/Attr0x50", 10);
        PerfW3C.xpath(doc, "/*/*/Attr0x99", 10);
        PerfW3C.xpathNodes(doc, "/*/*/Attr0x0", 10);
        PerfW3C.xpathNodes(doc, "/*/*/Attr1x50", 10);
        PerfW3C.xpathNodes(doc, "/*/*/Attr1x99", 10);
        PerfW3C.xpathNodes(doc, "/*/Item", 10);

        numrec = 10;
        System.out.println("\n10 Elements -----------------------------------");
        doc = PerfW3C.createDocument(numrec, 20, 10);
        PerfW3C.write(doc, "w3c_" + numrec + ".xml");
        PerfW3C.parse(numrec, 50);
        PerfW3C.transform(doc, "item.xslt", 10);
        PerfW3C.xpath(doc, "/*/*/Attr5", 100);
        PerfW3C.xpathNodes(doc, "/*/*/Attr1x5", 100);
        PerfW3C.xpathNodes(doc, "/*/Item", 100);

        numrec = 1;
        System.out.println("\n1 Elements ------------------------------------");
        doc = PerfW3C.createDocument(numrec, 20, 10);
        PerfW3C.write(doc, "w3c_" + numrec + ".xml");
        PerfW3C.parse(numrec, 100);
        PerfW3C.transform(doc, "item.xslt", 10);
        PerfW3C.xpath(doc, "/*/*/Attr1x0", 100);
        PerfW3C.xpathNodes(doc, "/*/*/Attr1x0", 100);
        PerfW3C.xpathNodes(doc, "/*/Item", 100);

    }

    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 = new DocumentImpl();

            Element root = document.createElement("ItemResultSet"); // Create
                                                                    // Root
                                                                    // Element
            document.appendChild(root);

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

                Element Record = document.createElement("Item");
                root.appendChild(Record);
                for (int jj = 0; jj < iNumFlds; jj++) {
                    /*
                     * AttrImpl a =
                     * (AttrImpl)document.createAttribute("Attr"+jj);
                     * a.setNodeValue("123456789"); Record.setAttributeNode(a);
                     */
                    Element field = document.createElement("Attr" + jj + "x"
                            + ii);
                    field.appendChild(document.createTextNode("123456789"));
                    Record.appendChild(field);

                }
            }
        }

        double end = System.currentTimeMillis();

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

        return document;
    }

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

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

        OutputFormat format = new OutputFormat(document); // Serialize DOM
        format.setIndent(2);
        format.setLineSeparator(System.getProperty("line.separator"));
        format.setLineWidth(80);
        try {

            FileWriter writer = new FileWriter(name);
            BufferedWriter buf = new BufferedWriter(writer);
            XMLSerializer FileSerial = new XMLSerializer(writer, format);
            FileSerial.asDOMSerializer(); // As a DOM Serializer
            FileSerial.serialize(document);

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

        }

        long end = System.currentTimeMillis();

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

    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("DOM4J createDocument:" + "Num Rec. = " + iNumRecs
        // + " Num. Fld.=" + iNumFlds);
        System.err.println("Parsing time for 			:" + iNumRecs + "  "
                + (end - start) / kk);

        return document;
    }

    public static void transform(Document xmlDoc, String xslFile, int kk) {
        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++) {
                DOMSource source = new DOMSource(xmlDoc);
                DOMResult result = new DOMResult();
                transformer.transform(source, result);
            }
            long end = System.currentTimeMillis();

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

    }

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

        long start = System.currentTimeMillis();

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

            try {

                Node node = XPathAPI.selectSingleNode(document, xpathExp);
                if ((node != null) & (ii == 0)) {
                    String val = node.getNodeName();
                    // System.out.println(val);
                }
            } catch (Exception e) {
                e.printStackTrace();

            }

        }

        long end = System.currentTimeMillis();

        System.err.println("W3C xpath  time :" + 1.000 * (end - start) / pp);
    }

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

        long start = System.currentTimeMillis();

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

            try {

                NodeList nodeList = XPathAPI.selectNodeList(document, xpathExp);
                if ((nodeList != null) && (nodeList.getLength() > 0)) {
                    Node node = nodeList.item(0);
                    if ((node != null) & (ii == 0)) {
                        String val = node.getNodeName();
                        // System.out.println(val);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();

            }

        }

        long end = System.currentTimeMillis();

        System.err.println("W3C xpathNodes  time :" + 1.000 * (end - start)
                / pp);
    }

}