| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Copyright (c) 2009-2021, Google LLC |
| 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. |
| 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 |
|
| 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 |
|
| 80 |
with open(base + "/100_msgs.proto", "w") as f: |
| 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): |
| 85 |
f.write('message Message{i} {{}}\n'.format(i=i)) |
| 86 |
|
| 87 |
with open(base + "/200_msgs.proto", "w") as f: |
| 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): |
| 92 |
f.write('message Message{i} {{}}\n'.format(i=i)) |
| 93 |
|
| 94 |
with open(base + "/100_fields.proto", "w") as f: |
| 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) |
| 101 |
for field in choices(100): |
| 102 |
field_type, label = field |
| 103 |
f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type)) |
| 104 |
i += 1 |
| 105 |
f.write('}\n') |
| 106 |
|
| 107 |
with open(base + "/200_fields.proto", "w") as f: |
| 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) |
| 114 |
for field in choices(200): |
| 115 |
field_type, label = field |
| 116 |
f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type)) |
| 117 |
i += 1 |
| 118 |
f.write('}\n') |