cpp.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/bin/bash
  2. # SwiftProtobuf/Performance/generators/cpp.sh - C++ test harness generator
  3. #
  4. # This source file is part of the Swift.org open source project
  5. #
  6. # Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  7. # Licensed under Apache License v2.0 with Runtime Library Exception
  8. #
  9. # See http://swift.org/LICENSE.txt for license information
  10. # See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  11. #
  12. # -----------------------------------------------------------------------------
  13. #
  14. # Functions for generating the C++ harness.
  15. #
  16. # -----------------------------------------------------------------------------
  17. function print_cpp_set_field() {
  18. num=$1
  19. type=$2
  20. case "$type" in
  21. repeated\ string)
  22. echo " for (auto i = 0; i < repeated_count; i++) {"
  23. echo " message.add_field$num(\"$((200+num))\");"
  24. echo " }"
  25. ;;
  26. repeated\ bytes)
  27. echo " for (auto i = 0; i < repeated_count; i++) {"
  28. echo " message.add_field$num(std::string(20, (char)$((num))));"
  29. echo " }"
  30. ;;
  31. repeated\ *)
  32. echo " for (auto i = 0; i < repeated_count; i++) {"
  33. echo " message.add_field$num($((200+num)));"
  34. echo " }"
  35. ;;
  36. string)
  37. echo " message.set_field$num(\"$((200+num))\");"
  38. ;;
  39. bytes)
  40. echo " message.set_field$num(std::string(20, (char)$((num))));"
  41. ;;
  42. *)
  43. echo " message.set_field$num($((200+num)));"
  44. ;;
  45. esac
  46. }
  47. function generate_cpp_harness() {
  48. cat >"$gen_harness_path" <<EOF
  49. #include "Harness.h"
  50. #include "message.pb.h"
  51. #include <iostream>
  52. #include <google/protobuf/text_format.h>
  53. #include <google/protobuf/util/json_util.h>
  54. #include <google/protobuf/util/message_differencer.h>
  55. #include <google/protobuf/util/type_resolver_util.h>
  56. using google::protobuf::Descriptor;
  57. using google::protobuf::DescriptorPool;
  58. using google::protobuf::TextFormat;
  59. using google::protobuf::util::BinaryToJsonString;
  60. using google::protobuf::util::JsonToBinaryString;
  61. using google::protobuf::util::MessageDifferencer;
  62. using google::protobuf::util::NewTypeResolverForDescriptorPool;
  63. using google::protobuf::util::Status;
  64. using google::protobuf::util::TypeResolver;
  65. using std::cerr;
  66. using std::endl;
  67. using std::string;
  68. static const char kTypeUrlPrefix[] = "type.googleapis.com";
  69. static string GetTypeUrl(const Descriptor* message) {
  70. return string(kTypeUrlPrefix) + "/" + message->full_name();
  71. }
  72. TypeResolver* type_resolver;
  73. string* type_url;
  74. static void populate_fields(PerfMessage& message, int repeated_count);
  75. void Harness::run() {
  76. GOOGLE_PROTOBUF_VERIFY_VERSION;
  77. type_resolver = NewTypeResolverForDescriptorPool(
  78. kTypeUrlPrefix, DescriptorPool::generated_pool());
  79. type_url = new string(GetTypeUrl(PerfMessage::descriptor()));
  80. measure([&]() {
  81. auto message = PerfMessage();
  82. measure_subtask("Populate fields", [&]() {
  83. populate_fields(message, repeated_count);
  84. // Dummy return value since void won't propagate.
  85. return false;
  86. });
  87. // Exercise binary serialization.
  88. auto data = measure_subtask("Encode binary", [&]() {
  89. return message.SerializeAsString();
  90. });
  91. auto decoded_message = measure_subtask("Decode binary", [&]() {
  92. auto result = PerfMessage();
  93. result.ParseFromString(data);
  94. return result;
  95. });
  96. // Exercise JSON serialization.
  97. auto json = measure_subtask("Encode JSON", [&]() {
  98. string out_json;
  99. BinaryToJsonString(type_resolver, *type_url, data, &out_json);
  100. return out_json;
  101. });
  102. auto decoded_binary = measure_subtask("Decode JSON", [&]() {
  103. string out_binary;
  104. JsonToBinaryString(type_resolver, *type_url, json, &out_binary);
  105. return out_binary;
  106. });
  107. // Exercise text serialization.
  108. auto text = measure_subtask("Encode text", [&]() {
  109. string out_text;
  110. TextFormat::PrintToString(message, &out_text);
  111. return out_text;
  112. });
  113. measure_subtask("Decode text", [&]() {
  114. auto result = PerfMessage();
  115. TextFormat::ParseFromString(text, &result);
  116. return result;
  117. });
  118. // Exercise equality.
  119. measure_subtask("Equality", [&]() {
  120. return MessageDifferencer::Equals(message, decoded_message);
  121. });
  122. });
  123. google::protobuf::ShutdownProtobufLibrary();
  124. }
  125. void populate_fields(PerfMessage& message, int repeated_count) {
  126. (void)repeated_count; /* Possibly unused: Quiet the compiler */
  127. EOF
  128. for field_number in $(seq 1 "$field_count"); do
  129. print_cpp_set_field "$field_number" "$field_type" >>"$gen_harness_path"
  130. done
  131. cat >> "$gen_harness_path" <<EOF
  132. }
  133. EOF
  134. }