File: saxtimer.py

package info (click to toggle)
qm 1.1.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,628 kB
  • ctags: 10,249
  • sloc: python: 41,482; ansic: 20,611; xml: 12,837; sh: 485; makefile: 226
file content (51 lines) | stat: -rw-r--r-- 1,078 bytes parent folder | download | duplicates (3)
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

# A simple SAX application that measures the time spent parsing a 
# document with an empty document handler.

from xml.sax import saxexts
from xml.sax import saxlib
import sys,time

if len(sys.argv)<3:
    print "Usage: python <parser> <document>"
    print
    print " <document>: file name of the document to parse"
    print " <parser>:   driver package name"
    sys.exit(1)

# Load parser and driver

print "\nLoading parser..."

try:
    p=saxexts.make_parser("xml.sax.drivers.drv_" + sys.argv[1])
except saxlib.SAXException,e:
    print "ERROR: Parser not available"
    sys.exit(1)

# Ready, set, go!

sum=0
print "Starting parse..."
for ix in range(3):
    start=time.clock()

    OK=0
    pt=0
    try:
        p.parse(sys.argv[2])
        pt=time.clock()-start
        OK=1
    except IOError,e:
        print "\nERROR: "+sys.argv[2]+": "+str(e)
    except saxlib.SAXException,e:
        print "\nERROR: "+str(e)

    if OK:
        print "Parse time: "+`pt`
    else:
        print "Error occurred, parse aborted."

    sum=sum+pt

print "Average: %f" % (sum/3.0)