• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python3
2#
3# Protocol Buffers - Google's data interchange format
4# Copyright 2023 Google LLC.  All rights reserved.
5# https://developers.google.com/protocol-buffers/
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions are
9# met:
10#
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.
32
33import sys
34import random
35
36base = sys.argv[1]
37
38field_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
75population = [item[0] for item in field_freqs]
76weights = [item[1] for item in field_freqs]
77
78def 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
85with open(base + "/100_msgs.proto", "w") as f:
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):
90    f.write('message Message{i} {{}}\n'.format(i=i))
91
92with open(base + "/200_msgs.proto", "w") as f:
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):
97    f.write('message Message{i} {{}}\n'.format(i=i))
98
99with open(base + "/100_fields.proto", "w") as f:
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)
106  for field in choices(100):
107    field_type, label = field
108    f.write('  {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type))
109    i += 1
110  f.write('}\n')
111
112with open(base + "/200_fields.proto", "w") as f:
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)
119  for field in choices(200):
120    field_type, label = field
121    f.write('  {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type))
122    i += 1
123  f.write('}\n')
124