-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·396 lines (307 loc) · 9.23 KB
/
benchmark.py
File metadata and controls
executable file
·396 lines (307 loc) · 9.23 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python
# inspired by https://github.com/rushter/selectolax/blob/master/examples/benchmark.py
# this downloads 1000 html files that weight 361MB for me
# mkdir pages
# for i in $(./ahrefs 99); do ucurl "$i" > "$(sed 's/^https:\/\//pages\//' <<< "$i")"; done
# faulty pages can be removed by running: ./benchmark.py test | xargs rm
"""
For each page, in seperate steps we extract:
1. Title
2. Number of script tag
3. The ``href`` attribute from all links
4. The content of the Meta description tag
5. The amount of tags with [ flex, relative, items-center, w-full, icon, lazyload, block, title ] classes
"""
# TODO
# 6. Most frequent attribute name with frequency
# 7. html size of first span tag where its insides begin with upper case letter, that is a descendand of li tag, which is a child of ul tag - ul; li child@; span i@bB>[A-Z]
# 8. Average size of blocks of a tags that have immediate siblings of a tags
import functools
import time
import os
import sys
from typing import Optional
from pathlib import Path
from dataclasses import dataclass
import gc
import psutil
from bs4 import BeautifulSoup
from html5_parser import parse
from lxml.html import fromstring
from selectolax.parser import HTMLParser
from reliq import reliq
from selectolax.lexbor import LexborHTMLParser
popular_classes_list = [
"flex",
"relative",
"items-center",
"w-full",
"icon",
"lazyload",
"block",
"title",
]
@dataclass
class test_result:
title: Optional[str]
href_amount: list[str]
script_amount: int
description: Optional[str]
popular_classes: int
# freq_attribute: Tuple[str, int]
# span_size: int
# avg_a_blocks: float
def bs4_test(page, parseonly=False):
tree = BeautifulSoup(page, "html.parser")
# tree = BeautifulSoup(page, "lxml")
if parseonly:
return tree
# 1
title = tree.title.string
# 2
href_amount = 0
for i in tree.find_all("a"):
if len(i.attrs.get("href", "")) > 0:
href_amount += 1
# 3
script_amount = len(tree.find_all("script"))
# 4
description = None
meta_description = tree.find(
"meta", attrs={"name": lambda x: x and x.lower() == "description"}
)
if meta_description:
description = meta_description.get("content")
# 5
popular_classes = 0
for i in popular_classes_list:
# popular_classes += len(tree.find_all(class=i))
popular_classes += len(tree.select("." + i))
return tree, test_result(
title, href_amount, script_amount, description, popular_classes
)
def html5_test(page, parseonly=False):
tree = parse(page)
if parseonly:
return tree
# 1
title = tree.xpath("//title/text()")[0]
# 2
href_amount = 0
for i in tree.xpath("//a[@href]"):
if len(i.attrib.get("href", "")) > 0:
href_amount += 1
# 3
script_amount = len(tree.xpath("//script"))
# 4
description = None
meta_description = tree.xpath(
'//meta[translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="description"]'
)
if meta_description:
description = meta_description[0].attrib.get("content")
# 5
popular_classes = 0
for i in popular_classes_list:
popular_classes += len(
tree.xpath(
'//*[contains(concat(" ",normalize-space(@class)," ")," ' + i + ' ")]'
)
)
return tree, test_result(
title, href_amount, script_amount, description, popular_classes
)
def lxml_test(page, parseonly=False):
tree = fromstring(page)
if parseonly:
return tree
# 1
title = tree.xpath("//title/text()")[0]
# 2
href_amount = 0
for i in tree.xpath("//a[@href]"):
if len(i.attrib.get("href", "")) > 0:
href_amount += 1
# 3
script_amount = len(tree.xpath("//script"))
description = None
meta_description = tree.xpath(
'//meta[translate(@name,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz")="description"]'
)
if meta_description:
description = meta_description[0].attrib.get("content")
# 5
popular_classes = 0
for i in popular_classes_list:
popular_classes += len(
tree.xpath(
'//*[contains(concat(" ",normalize-space(@class)," ")," ' + i + ' ")]'
)
)
return tree, test_result(
title, href_amount, script_amount, description, popular_classes
)
def selectolax_test(page, parseonly=False, lexbor=False):
tree = LexborHTMLParser(page) if lexbor else HTMLParser(page)
if parseonly:
return tree
# 1
title = None
title_node = tree.css_first("title")
if title_node:
title = title_node.text()
# 2
href_amount = 0
for i in tree.css("a[href]"):
c = i.attrs.get("href", "")
if c is not None and len(c) > 0:
href_amount += 1
# 3
script_amount = len(tree.css("script"))
# 4
description = None
if lexbor:
meta_description = tree.css_first('meta[name="description" i]')
else:
meta_description = tree.css_first('meta[name="description"]')
if meta_description:
description = meta_description.attrs.sget("content")
# 5
popular_classes = 0
for i in popular_classes_list:
popular_classes += len(tree.css("." + i))
popular_classes = len(
tree.css(
".flex, .relative, .items-center, .w-full, .icon, .lazyload, .block, .title"
)
)
return tree, test_result(
title, href_amount, script_amount, description, popular_classes
)
def reliq_test(page, parseonly=False):
tree = reliq(page)
if parseonly:
return tree
# 1
title = tree.search(r'[0] title | "%Ui" decode "e"')
# 2
href_amount = len(tree.search(r'a href=>[1:] | "%(href)v\n"').split("\n")[:-1])
# 3
script_amount = len(tree.filter("script"))
# 4
description = None
meta_description = tree.filter("[0] meta name=i>description")
if len(meta_description) > 0:
description = reliq.decode(
meta_description[0].attrib.get("content"), no_nbsp=False
)
# 5
popular_classes = len(
tree.filter(
"* .flex, * .relative, * .items-center, * .w-full, * .icon, * .lazyload, * .block, * .title"
)
)
return tree, test_result(
title, href_amount, script_amount, description, popular_classes
)
def tests_check_results(r):
model = r["reliq"]
tm = model
# lxml cannot fathom that classes can be split in multiple attributes
tm.popular_classes = r["lxml"].popular_classes
assert r["bs4"] == tm
assert r["lxml"] == tm
for i in ["html5_parser", "modest", "lexbor"]:
assert r[i].title == model.title
assert r[i].description == model.description
def compare_test(page, testers):
r = {}
for name, tester in testers:
r[name] = tester(page)
tests_check_results(r)
def compare_tests(pages, testers):
for path, page in pages:
try:
compare_test(page, testers)
except:
print(path)
def run_tests_time(pages, tester, name, parseonly):
start = time.time()
for path, page in pages:
tester(page, parseonly)
diff = time.time() - start
print("{}: {:0.3f}s".format(name, diff))
def memusage():
gc.collect()
return psutil.Process(os.getpid()).memory_info().rss // 1024**2
def run_tests_memory(pages, tester, name, parseonly):
start = memusage()
acc = []
for path, page in pages:
acc.append(tester(page))
diff = memusage() - start
print("{}: {}MB".format(name, diff))
def run_test_memory(pages, tester, name, parseonly):
pid = os.fork()
if pid > 0:
os.wait()
elif pid == 0:
run_tests_memory(pages, tester, name, parseonly)
exit()
def run_tests_r(pages, testers, parseonly=False, memory=False):
func = run_test_memory if memory else run_tests_time
for name, tester in testers:
func(pages, tester, name, parseonly)
def run_tests(pages, testers):
print("########### parse")
run_tests_r(pages, testers, True, False)
print()
print("########### memory usage - parse")
run_tests_r(pages, testers, True, True)
print()
print("########### parse and process")
run_tests_r(pages, testers, False, False)
print()
# print("########### mem - parse and process")
# run_tests_r(pages, testers, False, True)
# print()
def load_files(path):
ret = []
for i in os.scandir(path):
if not i.is_file():
continue
with open(i.path, "r") as f:
try:
ret.append((i.path, f.read().encode("utf-8")))
except:
pass
return ret
pages_path = Path("pages")
pages = load_files(pages_path)
testers = [
(
"bs4",
bs4_test,
),
(
"html5_parser",
html5_test,
),
(
"lxml",
lxml_test,
),
(
"modest",
selectolax_test,
),
("lexbor", functools.partial(selectolax_test, lexbor=True)),
(
"reliq",
reliq_test,
),
]
if len(sys.argv) == 2 and sys.argv[1] == "test":
compare_tests(pages, testers)
else:
run_tests(pages, testers)