1 module xmlsax;
2 
3 import tango.io.device.File;
4 import tango.io.Stdout;
5 import tango.time.StopWatch;
6 import tango.text.xml.SaxParser;
7 
8 void main() 
9 {       
10         for (int i = 10; --i;)
11             {
12             auto parser = new SaxParser!(char);
13             auto handler = new LengthHandler!(char);
14             parser.setSaxHandler(handler);
15             benchmark (2000, parser);
16             }
17 }
18 
19 
20 void benchmark (int iterations, SaxParser!(char) parser) 
21 {       
22         StopWatch elapsed;
23 
24         auto content = cast(char[]) File.get ("hamlet.xml");
25         parser.setContent(content);
26 
27         elapsed.start();
28         for (auto i=0; ++i < iterations;)
29             {
30             parser.parse();
31             parser.reset();
32             }
33         Stdout.formatln ("{} MB/s", (content.length * iterations) / (elapsed.stop() * (1024 * 1024)));
34 }
35 
36 
37 
38 
39 private class EventsHandler(Ch = char) : SaxHandler!(Ch) {
40 
41         public uint events;
42 
43 
44         public void setDocumentLocator(Locator!(Ch) locator) {
45                 events++;
46         }
47 
48         public void startDocument() {
49                 events++;
50         }
51 
52         public void endDocument() {
53                 events++;
54         }
55 
56         public override void startPrefixMapping(const(Ch)[] prefix, const(Ch)[] uri) {
57                 events++;
58         }
59 
60         public override void endPrefixMapping(const(Ch)[] prefix) {
61                 events++;
62         }                                               
63 
64         public override void startElement(const(Ch)[] uri, const(Ch)[] localName, const(Ch)[] qName, Attribute!(Ch)[] atts) {
65                 events++;
66                 foreach (ref attr; atts) {
67                         events++;
68                 }
69         }
70 
71         public override void endElement(const(Ch)[] uri, const(Ch)[] localName, const(Ch)[] qName) {
72                 events++;
73         }
74 
75         public override void characters(const(Ch)[] ch) {
76                 events++;
77         }
78 
79         public override void ignorableWhitespace(Ch[] ch) {
80                 events++;
81         }
82 
83         public override void processingInstruction(const(Ch)[] target, const(Ch)[] data) {
84                 events++;
85         }
86 
87         public override void skippedEntity(const(Ch)[] name) {
88                 events++;
89         }       
90 }
91 
92 private class LengthHandler(Ch = char) : SaxHandler!(Ch) {
93 
94         public uint elm;
95         public uint att;
96         public uint txt;
97         public uint elmlen;
98         public uint attlen;
99         public uint txtlen;
100 
101         public override void setDocumentLocator(Locator!(Ch) locator) {
102 
103         }
104 
105         public override void startDocument() {
106                 
107         }
108 
109         public override void endDocument() {
110                 
111         }
112 
113         public override void startPrefixMapping(const(Ch)[] prefix, const(Ch)[] uri) {
114 
115         }
116 
117         public override void endPrefixMapping(const(Ch)[] prefix) {
118 
119         }                                               
120 
121         public override void startElement(const(Ch)[] uri, const(Ch)[] localName, const(Ch)[] qName, Attribute!(Ch)[] atts) {
122                 elm++;
123                 elmlen += localName.length;
124                 foreach (ref attr; atts) {
125                         att++;
126                         attlen += attr.localName.length;
127                 }
128         }
129 
130         public override void endElement(const(Ch)[] uri, const(Ch)[] localName, const(Ch)[] qName) {
131                 
132         }
133 
134         public override void characters(const(Ch)[] ch) {
135                 txt++;
136                 txtlen += ch.length;
137         }
138 
139         public override void ignorableWhitespace(Ch[] ch) {
140 
141         }
142 
143         public override void processingInstruction(const(Ch)[] target, const(Ch)[] data) {
144 
145         }
146 
147         public override void skippedEntity(const(Ch)[] name) {
148 
149         }       
150 }
151