|
| 1 | +""" |
| 2 | +Script for testing the performance of YAML parsing, using yaml. |
| 3 | +
|
| 4 | +This will dump/load several real world-representative objects a few thousand |
| 5 | +times. The methodology below was chosen for was chosen to be similar to |
| 6 | +real-world scenarios which operate on single objects at a time. |
| 7 | +
|
| 8 | +This explicitly tests the pure Python implementation in pyyaml, not its C |
| 9 | +extension. |
| 10 | +
|
| 11 | +The object structure is copied from the `json_load` benchmark. |
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +import random |
| 16 | +import sys |
| 17 | + |
| 18 | + |
| 19 | +import pyperf |
| 20 | +import yaml |
| 21 | + |
| 22 | + |
| 23 | +DICT = { |
| 24 | + 'ads_flags': 0, |
| 25 | + 'age': 18, |
| 26 | + 'bulletin_count': 0, |
| 27 | + 'comment_count': 0, |
| 28 | + 'country': 'BR', |
| 29 | + 'encrypted_id': 'G9urXXAJwjE', |
| 30 | + 'favorite_count': 9, |
| 31 | + 'first_name': '', |
| 32 | + 'flags': 412317970704, |
| 33 | + 'friend_count': 0, |
| 34 | + 'gender': 'm', |
| 35 | + 'gender_for_display': 'Male', |
| 36 | + 'id': 302935349, |
| 37 | + 'is_custom_profile_icon': 0, |
| 38 | + 'last_name': '', |
| 39 | + 'locale_preference': 'pt_BR', |
| 40 | + 'member': 0, |
| 41 | + 'tags': ['a', 'b', 'c', 'd', 'e', 'f', 'g'], |
| 42 | + 'profile_foo_id': 827119638, |
| 43 | + 'secure_encrypted_id': 'Z_xxx2dYx3t4YAdnmfgyKw', |
| 44 | + 'session_number': 2, |
| 45 | + 'signup_id': '201-19225-223', |
| 46 | + 'status': 'A', |
| 47 | + 'theme': 1, |
| 48 | + 'time_created': 1225237014, |
| 49 | + 'time_updated': 1233134493, |
| 50 | + 'unread_message_count': 0, |
| 51 | + 'user_group': '0', |
| 52 | + 'username': 'collinwinter', |
| 53 | + 'play_count': 9, |
| 54 | + 'view_count': 7, |
| 55 | + 'zip': ''} |
| 56 | + |
| 57 | +TUPLE = ( |
| 58 | + [265867233, 265868503, 265252341, 265243910, 265879514, |
| 59 | + 266219766, 266021701, 265843726, 265592821, 265246784, |
| 60 | + 265853180, 45526486, 265463699, 265848143, 265863062, |
| 61 | + 265392591, 265877490, 265823665, 265828884, 265753032], 60) |
| 62 | + |
| 63 | + |
| 64 | +def mutate_dict(orig_dict, random_source): |
| 65 | + new_dict = dict(orig_dict) |
| 66 | + for key, value in new_dict.items(): |
| 67 | + rand_val = random_source.random() * sys.maxsize |
| 68 | + if isinstance(key, (int, bytes, str)): |
| 69 | + new_dict[key] = type(key)(rand_val) |
| 70 | + return new_dict |
| 71 | + |
| 72 | + |
| 73 | +random_source = random.Random(5) # Fixed seed. |
| 74 | +DICT_GROUP = [mutate_dict(DICT, random_source) for _ in range(3)] |
| 75 | + |
| 76 | + |
| 77 | +def bench_yaml(objs): |
| 78 | + for obj in objs: |
| 79 | + yaml.load(obj, Loader=yaml.Loader) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + runner = pyperf.Runner() |
| 84 | + runner.metadata['description'] = "Benchmark json.loads()" |
| 85 | + |
| 86 | + yaml_dict = yaml.dump(DICT) |
| 87 | + yaml_tuple = yaml.dump(TUPLE) |
| 88 | + yaml_dict_group = yaml.dump(DICT_GROUP) |
| 89 | + objs = (yaml_dict, yaml_tuple, yaml_dict_group) |
| 90 | + |
| 91 | + runner.bench_func('yaml', bench_yaml, objs, inner_loops=20) |
0 commit comments