gen_synthetic_protos.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import sys
  28. import random
  29. base = sys.argv[1]
  30. field_freqs = [
  31. (('bool', 'optional'), 8.321),
  32. (('bool', 'repeated'), 0.033),
  33. (('bytes', 'optional'), 0.809),
  34. (('bytes', 'repeated'), 0.065),
  35. (('double', 'optional'), 2.845),
  36. (('double', 'repeated'), 0.143),
  37. (('fixed32', 'optional'), 0.084),
  38. (('fixed32', 'repeated'), 0.012),
  39. (('fixed64', 'optional'), 0.204),
  40. (('fixed64', 'repeated'), 0.027),
  41. (('float', 'optional'), 2.355),
  42. (('float', 'repeated'), 0.132),
  43. (('int32', 'optional'), 6.717),
  44. (('int32', 'repeated'), 0.366),
  45. (('int64', 'optional'), 9.678),
  46. (('int64', 'repeated'), 0.425),
  47. (('sfixed32', 'optional'), 0.018),
  48. (('sfixed32', 'repeated'), 0.005),
  49. (('sfixed64', 'optional'), 0.022),
  50. (('sfixed64', 'repeated'), 0.005),
  51. (('sint32', 'optional'), 0.026),
  52. (('sint32', 'repeated'), 0.009),
  53. (('sint64', 'optional'), 0.018),
  54. (('sint64', 'repeated'), 0.006),
  55. (('string', 'optional'), 25.461),
  56. (('string', 'repeated'), 2.606),
  57. (('Enum', 'optional'), 6.16),
  58. (('Enum', 'repeated'), 0.576),
  59. (('Message', 'optional'), 22.472),
  60. (('Message', 'repeated'), 7.766),
  61. (('uint32', 'optional'), 1.289),
  62. (('uint32', 'repeated'), 0.051),
  63. (('uint64', 'optional'), 1.044),
  64. (('uint64', 'repeated'), 0.079),
  65. ]
  66. population = [item[0] for item in field_freqs]
  67. weights = [item[1] for item in field_freqs]
  68. def choices(k):
  69. if sys.version_info >= (3, 6):
  70. return random.choices(population=population, weights=weights, k=k)
  71. else:
  72. print("WARNING: old Python version, field types are not properly weighted!")
  73. return [random.choice(population) for _ in range(k)]
  74. with open(base + "/100_msgs.proto", "w") as f:
  75. f.write('syntax = "proto3";\n')
  76. f.write('package upb_benchmark;\n')
  77. f.write('message Message {}\n')
  78. for i in range(2, 101):
  79. f.write('message Message{i} {{}}\n'.format(i=i))
  80. with open(base + "/200_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, 501):
  85. f.write('message Message{i} {{}}\n'.format(i=i))
  86. with open(base + "/100_fields.proto", "w") as f:
  87. f.write('syntax = "proto2";\n')
  88. f.write('package upb_benchmark;\n')
  89. f.write('enum Enum { ZERO = 0; }\n')
  90. f.write('message Message {\n')
  91. i = 1
  92. random.seed(a=0, version=2)
  93. for field in choices(100):
  94. field_type, label = field
  95. f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label, field_type=field_type))
  96. i += 1
  97. f.write('}\n')
  98. with open(base + "/200_fields.proto", "w") as f:
  99. f.write('syntax = "proto2";\n')
  100. f.write('package upb_benchmark;\n')
  101. f.write('enum Enum { ZERO = 0; }\n')
  102. f.write('message Message {\n')
  103. i = 1
  104. random.seed(a=0, version=2)
  105. for field in choices(200):
  106. field_type, label = field
  107. f.write(' {label} {field_type} field{i} = {i};\n'.format(i=i, label=label,field_type=field_type))
  108. i += 1
  109. f.write('}\n')