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