This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench.py
174 lines (135 loc) · 5.09 KB
/
bench.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import os
import sys
import time
import html5lib
import lxml.html
def typical_bench_html5lib_etree(files):
print('html5lib etree:')
for i in range(1, 201):
sys.stdout.write('.')
sys.stdout.flush()
etree_doc = html5lib.parse(files['wiki{}.html'.format(i)])
assert etree_doc.tag == '{http://www.w3.org/1999/xhtml}html'
print(' done.')
def typical_bench_html5lib_dom(files):
print('html5lib dom:')
for i in range(1, 201):
sys.stdout.write('.')
sys.stdout.flush()
dom_doc = html5lib.parse(files['wiki{}.html'.format(i)], treebuilder="dom")
assert len(dom_doc.toxml()) > 1024
print(' done.')
def typical_bench_html5lib_lxml(files):
print('html5lib lxml:')
for i in range(1, 201):
sys.stdout.write('.')
sys.stdout.flush()
lxml_doc = html5lib.parse(files['wiki{}.html'.format(i)], treebuilder="lxml")
assert lxml_doc.getroot().tag == '{http://www.w3.org/1999/xhtml}html'
print(' done.')
def typical_bench_lxml(files):
print('lxml.html:')
for i in range(1, 201):
sys.stdout.write('.')
sys.stdout.flush()
lxml_doc = lxml.html.document_fromstring(files['wiki{}.html'.format(i)])
assert len(lxml.html.tostring(lxml_doc)) > 1024
print(' done.')
def huge_bench_html5lib_etree(files):
etree_doc = html5lib.parse(files['template.html'])
assert etree_doc.tag == '{http://www.w3.org/1999/xhtml}html'
print(' template done;')
etree_doc2 = html5lib.parse(files['spec.html'])
assert etree_doc2.tag == '{http://www.w3.org/1999/xhtml}html'
print(' spec done;')
etree_doc3 = html5lib.parse(files['py33_py34.html'])
assert etree_doc3.tag == '{http://www.w3.org/1999/xhtml}html'
print(' py33_py34 done.')
def huge_bench_html5lib_dom(files):
print('html5lib dom:')
dom_doc = html5lib.parse(files['template.html'], treebuilder="dom")
assert len(dom_doc.toxml()) > 1024
print(' template done;')
dom_doc2 = html5lib.parse(files['spec.html'], treebuilder="dom")
assert len(dom_doc2.toxml()) > 1024
print(' spec done;')
dom_doc3 = html5lib.parse(files['py33_py34.html'], treebuilder="dom")
assert len(dom_doc3.toxml()) > 1024
print(' py33_py34 done.')
def huge_bench_html5lib_lxml(files):
print('html5lib lxml:')
lxml_doc = html5lib.parse(files['template.html'], treebuilder="lxml")
assert lxml_doc.getroot().tag == '{http://www.w3.org/1999/xhtml}html'
print(' template done;')
lxml_doc2 = html5lib.parse(files['spec.html'], treebuilder="lxml")
assert lxml_doc2.getroot().tag == '{http://www.w3.org/1999/xhtml}html'
print(' spec done;')
lxml_doc3 = html5lib.parse(files['py33_py34.html'], treebuilder="lxml")
assert lxml_doc3.getroot().tag == '{http://www.w3.org/1999/xhtml}html'
print(' py33_py34 done.')
def huge_bench_lxml(files):
print('lxml.html:')
lxml_doc = lxml.html.document_fromstring(files['template.html'])
assert len(lxml.html.tostring(lxml_doc)) > 1024
print(' template done;')
lxml_doc2 = lxml.html.document_fromstring(files['spec.html'])
assert len(lxml.html.tostring(lxml_doc2)) > 1024
print(' spec done;')
lxml_doc3 = lxml.html.document_fromstring(files['py33_py34.html'])
assert len(lxml.html.tostring(lxml_doc3)) > 1024
print(' py33_py34 done.')
def main():
files = {}
cwd = os.path.dirname(__file__)
print('prereading files:')
for file_name in ('template.html', 'spec.html', 'py33_py34.html'):
with open(os.path.join(cwd, 'data', file_name), 'rb') as f:
files[file_name] = f.read()
print(' ', file_name)
for i in range(1, 201):
file_name = 'wiki{}.html'.format(i)
with open(os.path.join(cwd, 'data', file_name), 'rb') as f:
files[file_name] = f.read()
print(' 200 Wiki pages as wiki*.html')
print("*" * 10, "Typical pages", "*" * 10)
start = time.time()
for i in range(1):
typical_bench_html5lib_etree(files)
print(time.time() - start)
start = time.time()
for i in range(1):
typical_bench_html5lib_dom(files)
print(time.time() - start)
start = time.time()
for i in range(1):
typical_bench_html5lib_lxml(files)
print(time.time() - start)
start = time.time()
for i in range(1):
typical_bench_lxml(files)
print(time.time() - start)
print("*" * 10, "Huge pages", "*" * 10)
start = time.time()
for i in range(1):
huge_bench_html5lib_etree(files)
print(time.time() - start)
start = time.time()
for i in range(1):
huge_bench_html5lib_dom(files)
print(time.time() - start)
start = time.time()
for i in range(1):
huge_bench_html5lib_lxml(files)
print(time.time() - start)
start = time.time()
for i in range(1):
huge_bench_lxml(files)
print(time.time() - start)
if __name__ == '__main__':
main()