blob: 99f83c15e161cc2cd7544a0e10305f6c7df6c4fe [file] [log] [blame]
Joshua Habermaneabb7742021-08-16 16:44:27 -07001#!/usr/bin/python3
Joshua Habermane59d2c82021-04-05 10:47:53 -07002#
Joshua Haberman823eb092021-04-05 12:26:41 -07003# Copyright (c) 2009-2021, Google LLC
Joshua Habermane59d2c82021-04-05 10:47:53 -07004# 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 Haberman555fbbc2020-11-10 20:20:06 -080027
28import sys
29import random
30
31base = sys.argv[1]
32
33field_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
70population = [item[0] for item in field_freqs]
71weights = [item[1] for item in field_freqs]
72
Joshua Haberman165e01e2020-11-11 17:08:25 -080073def 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 Habermanda48e012020-11-11 19:00:52 -080080with open(base + "/100_msgs.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -080081 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 Haberman881ddac2020-11-10 20:53:17 -080085 f.write('message Message{i} {{}}\n'.format(i=i))
Joshua Haberman555fbbc2020-11-10 20:20:06 -080086
Joshua Habermanda48e012020-11-11 19:00:52 -080087with open(base + "/200_msgs.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -080088 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 Haberman881ddac2020-11-10 20:53:17 -080092 f.write('message Message{i} {{}}\n'.format(i=i))
Joshua Haberman555fbbc2020-11-10 20:20:06 -080093
Joshua Habermanda48e012020-11-11 19:00:52 -080094with open(base + "/100_fields.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -080095 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 Haberman165e01e2020-11-11 17:08:25 -0800101 for field in choices(100):
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800102 field_type, label = field
Joshua Haberman881ddac2020-11-10 20:53:17 -0800103 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 -0800104 i += 1
105 f.write('}\n')
106
Joshua Habermanda48e012020-11-11 19:00:52 -0800107with open(base + "/200_fields.proto", "w") as f:
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800108 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 Haberman165e01e2020-11-11 17:08:25 -0800114 for field in choices(200):
Joshua Haberman555fbbc2020-11-10 20:20:06 -0800115 field_type, label = field
Joshua Haberman881ddac2020-11-10 20:53:17 -0800116 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 -0800117 i += 1
118 f.write('}\n')