-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
test_url_benchmarks.py
732 lines (524 loc) · 19.3 KB
/
test_url_benchmarks.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
"""codspeed benchmarks for yarl.URL."""
from pytest_codspeed import BenchmarkFixture
from yarl import URL
MANY_HOSTS = [f"www.domain{i}.tld" for i in range(256)]
MANY_URLS = [f"https://www.domain{i}.tld" for i in range(256)]
MANY_IPV4_URLS = [f"http://127.0.0.{i}" for i in range(256)]
MANY_IPV6_URLS = [f"http://[::1{i}]" for i in range(256)]
BASE_URL_STR = "http://www.domain.tld"
BASE_URL = URL(BASE_URL_STR)
URL_WITH_USER_PASS_STR = "http://user:[email protected]"
URL_WITH_USER_PASS = URL(URL_WITH_USER_PASS_STR)
IPV6_QUERY_URL = URL("http://[::1]/req?query=1&query=2&query=3&query=4&query=5")
URL_WITH_NOT_DEFAULT_PORT = URL("http://www.domain.tld:1234")
QUERY_URL_STR = "http://www.domain.tld?query=1&query=2&query=3&query=4&query=5"
QUERY_URL = URL(QUERY_URL_STR)
URL_WITH_PATH_STR = "http://www.domain.tld/req"
URL_WITH_PATH = URL(URL_WITH_PATH_STR)
REL_URL = URL("/req")
QUERY_SEQ = {str(i): tuple(str(j) for j in range(10)) for i in range(10)}
SIMPLE_QUERY = {str(i): str(i) for i in range(10)}
SIMPLE_INT_QUERY = {str(i): i for i in range(10)}
QUERY_STRING = "x=y&z=1"
class _SubClassedStr(str):
"""A subclass of str that does nothing."""
def test_url_build_with_host_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(host="www.domain.tld", path="/req", port=1234)
def test_url_build_with_simple_query(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(host="www.domain.tld", query=SIMPLE_QUERY)
def test_url_build_no_netloc(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(path="/req/req/req")
def test_url_build_no_netloc_relative(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(path="req/req/req")
def test_url_build_encoded_with_host_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(host="www.domain.tld", path="/req", port=1234, encoded=True)
def test_url_build_with_host(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(host="domain")
def test_url_build_access_username_password(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL.build(host="www.domain.tld", user="user", password="password")
url.raw_user
url.raw_password
def test_url_build_access_raw_host(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL.build(host="www.domain.tld")
url.raw_host
def test_url_build_access_fragment(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL.build(host="www.domain.tld")
url.fragment
def test_url_build_access_raw_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL.build(host="www.domain.tld", path="/req")
url.raw_path
def test_url_build_with_different_hosts(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for host in MANY_HOSTS:
URL.build(host=host)
def test_url_build_with_host_path_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL.build(host="www.domain.tld", port=1234)
def test_url_make_no_netloc(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("/req/req/req")
def test_url_make_no_netloc_relative(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("req/req/req")
def test_url_make_with_host_path_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://www.domain.tld:1234/req")
def test_url_make_encoded_with_host_path_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://www.domain.tld:1234/req", encoded=True)
def test_url_make_with_host_and_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://www.domain.tld")
def test_url_make_with_many_hosts(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for url in MANY_URLS:
URL(url)
def test_url_make_with_many_ipv4_hosts(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for url in MANY_IPV4_URLS:
URL(url)
def test_url_make_with_many_ipv6_hosts(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for url in MANY_IPV6_URLS:
URL(url)
def test_url_make_access_raw_host(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL("http://www.domain.tld")
url.raw_host
def test_raw_host_empty_cache(benchmark: BenchmarkFixture) -> None:
url = URL("http://www.domain.tld")
@benchmark
def _run() -> None:
for _ in range(100):
url._cache.pop("raw_host", None)
url.raw_host
def test_url_make_access_fragment(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL("http://www.domain.tld")
url.fragment
def test_url_make_access_raw_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL("http://www.domain.tld/req")
url.raw_path
def test_url_make_access_username_password(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url = URL("http://user:[email protected]")
url.raw_user
url.raw_password
def test_url_make_empty_username(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://:[email protected]")
def test_url_make_empty_password(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://user:@www.domain.tld")
def test_url_make_with_ipv4_address_path_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://127.0.0.1:1234/req")
def test_url_make_with_ipv4_address_and_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://127.0.0.1/req")
def test_url_make_with_ipv6_address_path_and_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://[::1]:1234/req")
def test_url_make_with_ipv6_address_and_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL("http://[::1]/req")
def test_extend_query_subclassed_str(benchmark: BenchmarkFixture) -> None:
"""Test extending a query with a subclassed str."""
subclassed_query = {str(i): _SubClassedStr(i) for i in range(10)}
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query(subclassed_query)
def test_with_query_mapping(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query(SIMPLE_QUERY)
def test_with_query_mapping_int_values(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query(SIMPLE_INT_QUERY)
def test_with_query_sequence_mapping(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query(QUERY_SEQ)
def test_with_query_empty(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query({})
def test_with_query_none(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.with_query(None)
def test_update_query_mapping(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.update_query(SIMPLE_QUERY)
def test_update_query_mapping_with_existing_query(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
QUERY_URL.update_query(SIMPLE_QUERY)
def test_update_query_sequence_mapping(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.update_query(QUERY_SEQ)
def test_update_query_empty(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.update_query({})
def test_update_query_none(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.update_query(None)
def test_update_query_string(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.update_query(QUERY_STRING)
def test_url_extend_query_simple_query_dict(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
BASE_URL.extend_query(SIMPLE_QUERY)
def test_url_extend_query_existing_query_simple_query_dict(
benchmark: BenchmarkFixture,
) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
QUERY_URL.extend_query(SIMPLE_QUERY)
def test_url_extend_query_existing_query_string(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(25):
QUERY_URL.extend_query(QUERY_STRING)
def test_url_to_string(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
str(BASE_URL)
def test_url_with_path_to_string(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
str(URL_WITH_PATH)
def test_url_with_query_to_string(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
str(QUERY_URL)
def test_url_with_fragment(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_fragment("fragment")
def test_url_with_user(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_user("user")
def test_url_with_password(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_password("password")
def test_url_with_host(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_host("www.domain.tld")
def test_url_with_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_port(1234)
def test_url_with_scheme(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_scheme("https")
def test_url_with_name(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_name("other.tld")
def test_url_with_path(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.with_path("/req")
def test_url_origin(benchmark: BenchmarkFixture) -> None:
urls = [URL(BASE_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.origin()
def test_url_origin_with_user_pass(benchmark: BenchmarkFixture) -> None:
urls = [URL(URL_WITH_USER_PASS_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.origin()
def test_url_with_path_origin(benchmark: BenchmarkFixture) -> None:
urls = [URL(URL_WITH_PATH_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.origin()
def test_url_with_path_relative(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
URL_WITH_PATH.relative()
def test_url_with_path_parent(benchmark: BenchmarkFixture) -> None:
cache = URL_WITH_PATH._cache
@benchmark
def _run() -> None:
for _ in range(100):
cache.pop("parent", None)
URL_WITH_PATH.parent
def test_url_join(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.join(REL_URL)
def test_url_joinpath_encoded(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.joinpath("req", encoded=True)
def test_url_joinpath_encoded_long(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.joinpath(
"req/req/req/req/req/req/req/req/req/req/req/req/req/req", encoded=True
)
def test_url_joinpath(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.joinpath("req", encoded=False)
def test_url_joinpath_with_truediv(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL / "req/req/req"
def test_url_equality(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL == BASE_URL
BASE_URL == URL_WITH_PATH
URL_WITH_PATH == URL_WITH_PATH
def test_url_hash(benchmark: BenchmarkFixture) -> None:
cache = BASE_URL._cache
@benchmark
def _run() -> None:
for _ in range(100):
cache.pop("hash", None)
hash(BASE_URL)
def test_is_default_port(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.is_default_port()
URL_WITH_NOT_DEFAULT_PORT.is_default_port()
def test_human_repr(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.human_repr()
URL_WITH_PATH.human_repr()
QUERY_URL.human_repr()
URL_WITH_NOT_DEFAULT_PORT.human_repr()
IPV6_QUERY_URL.human_repr()
REL_URL.human_repr()
def test_query_string(benchmark: BenchmarkFixture) -> None:
urls = [URL(QUERY_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.query_string
def test_empty_query_string(benchmark: BenchmarkFixture) -> None:
urls = [URL(BASE_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.query_string
def test_empty_query_string_uncached(benchmark: BenchmarkFixture) -> None:
urls = [URL(BASE_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
URL.query_string.wrapped(url)
def test_query(benchmark: BenchmarkFixture) -> None:
urls = [URL(QUERY_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.query
def test_empty_query(benchmark: BenchmarkFixture) -> None:
urls = [URL(BASE_URL_STR) for _ in range(100)]
@benchmark
def _run() -> None:
for url in urls:
url.query
def test_url_host_port_subcomponent(benchmark: BenchmarkFixture) -> None:
cache_non_default = URL_WITH_NOT_DEFAULT_PORT._cache
cache = BASE_URL._cache
@benchmark
def _run() -> None:
for _ in range(100):
cache.pop("host_port_subcomponent", None)
cache_non_default.pop("host_port_subcomponent", None)
URL_WITH_NOT_DEFAULT_PORT.host_port_subcomponent
BASE_URL.host_port_subcomponent
def test_empty_path(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty path."""
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.path
def test_empty_path_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty path without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.path.wrapped(BASE_URL)
def test_empty_path_safe(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty path safe."""
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.path_safe
def test_empty_path_safe_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty path safe without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.path_safe.wrapped(BASE_URL)
def test_path_safe(benchmark: BenchmarkFixture) -> None:
"""Test accessing path safe."""
@benchmark
def _run() -> None:
for _ in range(100):
URL_WITH_PATH.path_safe
def test_path_safe_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing path safe without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.path_safe.wrapped(URL_WITH_PATH)
def test_empty_raw_path_qs(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty raw path with query."""
@benchmark
def _run() -> None:
for _ in range(100):
BASE_URL.raw_path_qs
def test_empty_raw_path_qs_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing empty raw path with query without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.raw_path_qs.wrapped(BASE_URL)
def test_raw_path_qs(benchmark: BenchmarkFixture) -> None:
"""Test accessing raw path qs without query."""
@benchmark
def _run() -> None:
for _ in range(100):
URL_WITH_PATH.raw_path_qs
def test_raw_path_qs_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing raw path qs without query and without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.raw_path_qs.wrapped(URL_WITH_PATH)
def test_raw_path_qs_with_query(benchmark: BenchmarkFixture) -> None:
"""Test accessing raw path qs with query."""
@benchmark
def _run() -> None:
for _ in range(100):
IPV6_QUERY_URL.raw_path_qs
def test_raw_path_qs_with_query_uncached(benchmark: BenchmarkFixture) -> None:
"""Test accessing raw path qs with query and without cache."""
@benchmark
def _run() -> None:
for _ in range(100):
URL.raw_path_qs.wrapped(IPV6_QUERY_URL)