blob: 3b4e37e6951917a79042e251f93ee72ecc2282b2 [file] [log] [blame]
Joshua Habermaneabb7742021-08-16 16:44:27 -07001#!/usr/bin/python3
Joshua Habermane59d2c82021-04-05 10:47:53 -07002#
Adam Cozzette5aca7282023-08-07 10:01:08 -07003# Protocol Buffers - Google's data interchange format
4# Copyright 2023 Google LLC. All rights reserved.
5# https://developers.google.com/protocol-buffers/
Joshua Habermane59d2c82021-04-05 10:47:53 -07006#
7# Redistribution and use in source and binary forms, with or without
Adam Cozzette5aca7282023-08-07 10:01:08 -07008# modification, are permitted provided that the following conditions are
9# met:
Joshua Habermane59d2c82021-04-05 10:47:53 -070010#
Adam Cozzette5aca7282023-08-07 10:01:08 -070011# * 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 Haberman555fbbc2020-11-10 20:20:06 -080032
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
Joshua Haberman165e01e2020-11-11 17:08:25 -080078def 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 Habermanda48e012020-11-11 19:00:52 -080085with open(base + "/100_msgs.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -080086 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 Haberman881ddac2020-11-10 20:53:17 -080090 f.write('message Message{i} {{}}\n'.format(i=i))
Joshua Haberman555fbbc2020-11-10 20:20:06 -080091
Joshua Habermanda48e012020-11-11 19:00:52 -080092with open(base + "/200_msgs.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -080093 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 Haberman881ddac2020-11-10 20:53:17 -080097 f.write('message Message{i} {{}}\n'.format(i=i))
Joshua Haberman555fbbc2020-11-10 20:20:06 -080098
Joshua Habermanda48e012020-11-11 19:00:52 -080099with open(base + "/100_fields.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800100 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 Haberman165e01e2020-11-11 17:08:25 -0800106 for field in choices(100):
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800107 field_type, label = field
Joshua Haberman881ddac2020-11-10 20:53:17 -0800108 f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type))
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800109 i += 1
110 f.write('}\n')
111
Joshua Habermanda48e012020-11-11 19:00:52 -0800112with open(base + "/200_fields.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800113 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 Haberman165e01e2020-11-11 17:08:25 -0800119 for field in choices(200):
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800120 field_type, label = field
Joshua Haberman881ddac2020-11-10 20:53:17 -0800121 f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type))
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800122 i += 1
123 f.write('}\n')