Joshua Haberman | eabb774 | 2021-08-16 16:44:27 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 2 | # |
Adam Cozzette | 5aca728 | 2023-08-07 10:01:08 -0700 | [diff] [blame] | 3 | # Protocol Buffers - Google's data interchange format |
| 4 | # Copyright 2023 Google LLC. All rights reserved. |
| 5 | # https://developers.google.com/protocol-buffers/ |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 6 | # |
| 7 | # Redistribution and use in source and binary forms, with or without |
Adam Cozzette | 5aca728 | 2023-08-07 10:01:08 -0700 | [diff] [blame] | 8 | # modification, are permitted provided that the following conditions are |
| 9 | # met: |
Joshua Haberman | e59d2c8 | 2021-04-05 10:47:53 -0700 | [diff] [blame] | 10 | # |
Adam Cozzette | 5aca728 | 2023-08-07 10:01:08 -0700 | [diff] [blame] | 11 | # * Redistributions of source code must retain the above copyright |
| 12 | # notice, this list of conditions and the following disclaimer. |
| 13 | # * Redistributions in binary form must reproduce the above |
| 14 | # copyright notice, this list of conditions and the following disclaimer |
| 15 | # in the documentation and/or other materials provided with the |
| 16 | # distribution. |
| 17 | # * Neither the name of Google LLC nor the names of its |
| 18 | # contributors may be used to endorse or promote products derived from |
| 19 | # this software without specific prior written permission. |
| 20 | # |
| 21 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 32 | |
| 33 | import sys |
| 34 | import random |
| 35 | |
| 36 | base = sys.argv[1] |
| 37 | |
| 38 | field_freqs = [ |
| 39 | (('bool', 'optional'), 8.321), |
| 40 | (('bool', 'repeated'), 0.033), |
| 41 | (('bytes', 'optional'), 0.809), |
| 42 | (('bytes', 'repeated'), 0.065), |
| 43 | (('double', 'optional'), 2.845), |
| 44 | (('double', 'repeated'), 0.143), |
| 45 | (('fixed32', 'optional'), 0.084), |
| 46 | (('fixed32', 'repeated'), 0.012), |
| 47 | (('fixed64', 'optional'), 0.204), |
| 48 | (('fixed64', 'repeated'), 0.027), |
| 49 | (('float', 'optional'), 2.355), |
| 50 | (('float', 'repeated'), 0.132), |
| 51 | (('int32', 'optional'), 6.717), |
| 52 | (('int32', 'repeated'), 0.366), |
| 53 | (('int64', 'optional'), 9.678), |
| 54 | (('int64', 'repeated'), 0.425), |
| 55 | (('sfixed32', 'optional'), 0.018), |
| 56 | (('sfixed32', 'repeated'), 0.005), |
| 57 | (('sfixed64', 'optional'), 0.022), |
| 58 | (('sfixed64', 'repeated'), 0.005), |
| 59 | (('sint32', 'optional'), 0.026), |
| 60 | (('sint32', 'repeated'), 0.009), |
| 61 | (('sint64', 'optional'), 0.018), |
| 62 | (('sint64', 'repeated'), 0.006), |
| 63 | (('string', 'optional'), 25.461), |
| 64 | (('string', 'repeated'), 2.606), |
| 65 | (('Enum', 'optional'), 6.16), |
| 66 | (('Enum', 'repeated'), 0.576), |
| 67 | (('Message', 'optional'), 22.472), |
| 68 | (('Message', 'repeated'), 7.766), |
| 69 | (('uint32', 'optional'), 1.289), |
| 70 | (('uint32', 'repeated'), 0.051), |
| 71 | (('uint64', 'optional'), 1.044), |
| 72 | (('uint64', 'repeated'), 0.079), |
| 73 | ] |
| 74 | |
| 75 | population = [item[0] for item in field_freqs] |
| 76 | weights = [item[1] for item in field_freqs] |
| 77 | |
Joshua Haberman | 165e01e | 2020-11-11 17:08:25 -0800 | [diff] [blame] | 78 | def choices(k): |
| 79 | if sys.version_info >= (3, 6): |
| 80 | return random.choices(population=population, weights=weights, k=k) |
| 81 | else: |
| 82 | print("WARNING: old Python version, field types are not properly weighted!") |
| 83 | return [random.choice(population) for _ in range(k)] |
| 84 | |
Joshua Haberman | da48e01 | 2020-11-11 19:00:52 -0800 | [diff] [blame] | 85 | with open(base + "/100_msgs.proto", "w") as f: |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 86 | f.write('syntax = "proto3";\n') |
| 87 | f.write('package upb_benchmark;\n') |
| 88 | f.write('message Message {}\n') |
| 89 | for i in range(2, 101): |
Joshua Haberman | 881ddac | 2020-11-10 20:53:17 -0800 | [diff] [blame] | 90 | f.write('message Message{i} {{}}\n'.format(i=i)) |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 91 | |
Joshua Haberman | da48e01 | 2020-11-11 19:00:52 -0800 | [diff] [blame] | 92 | with open(base + "/200_msgs.proto", "w") as f: |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 93 | f.write('syntax = "proto3";\n') |
| 94 | f.write('package upb_benchmark;\n') |
| 95 | f.write('message Message {}\n') |
| 96 | for i in range(2, 501): |
Joshua Haberman | 881ddac | 2020-11-10 20:53:17 -0800 | [diff] [blame] | 97 | f.write('message Message{i} {{}}\n'.format(i=i)) |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 98 | |
Joshua Haberman | da48e01 | 2020-11-11 19:00:52 -0800 | [diff] [blame] | 99 | with open(base + "/100_fields.proto", "w") as f: |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 100 | f.write('syntax = "proto2";\n') |
| 101 | f.write('package upb_benchmark;\n') |
| 102 | f.write('enum Enum { ZERO = 0; }\n') |
| 103 | f.write('message Message {\n') |
| 104 | i = 1 |
| 105 | random.seed(a=0, version=2) |
Joshua Haberman | 165e01e | 2020-11-11 17:08:25 -0800 | [diff] [blame] | 106 | for field in choices(100): |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 107 | field_type, label = field |
Joshua Haberman | 881ddac | 2020-11-10 20:53:17 -0800 | [diff] [blame] | 108 | f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type)) |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 109 | i += 1 |
| 110 | f.write('}\n') |
| 111 | |
Joshua Haberman | da48e01 | 2020-11-11 19:00:52 -0800 | [diff] [blame] | 112 | with open(base + "/200_fields.proto", "w") as f: |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 113 | f.write('syntax = "proto2";\n') |
| 114 | f.write('package upb_benchmark;\n') |
| 115 | f.write('enum Enum { ZERO = 0; }\n') |
| 116 | f.write('message Message {\n') |
| 117 | i = 1 |
| 118 | random.seed(a=0, version=2) |
Joshua Haberman | 165e01e | 2020-11-11 17:08:25 -0800 | [diff] [blame] | 119 | for field in choices(200): |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 120 | field_type, label = field |
Joshua Haberman | 881ddac | 2020-11-10 20:53:17 -0800 | [diff] [blame] | 121 | f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type)) |
Joshua Haberman | 555fbbc | 2020-11-10 20:20:06 -0800 | [diff] [blame] | 122 | i += 1 |
| 123 | f.write('}\n') |