pw_protobuf: Added basic encoder performance testing

Change-Id: I175488df7f972789fb6ca4762efda1125e846d67
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/123511
Reviewed-by: Wyatt Hepler <hepler@google.com>
Commit-Queue: Wyatt Hepler <hepler@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
diff --git a/BUILD.gn b/BUILD.gn
index a7014b9..ec1ba58 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -439,6 +439,7 @@
     deps = [
       "$dir_pw_checksum:perf_tests",
       "$dir_pw_perf_test:perf_test_tests_test",
+      "$dir_pw_protobuf:perf_tests",
     ]
   }
 
diff --git a/pw_protobuf/BUILD.bazel b/pw_protobuf/BUILD.bazel
index 6d26a0d..abf9a26 100644
--- a/pw_protobuf/BUILD.bazel
+++ b/pw_protobuf/BUILD.bazel
@@ -16,6 +16,7 @@
 load(
     "//pw_build:pigweed.bzl",
     "pw_cc_library",
+    "pw_cc_perf_test",
     "pw_cc_test",
 )
 load("//pw_fuzzer:fuzzer.bzl", "pw_cc_fuzz_test")
@@ -176,6 +177,15 @@
     ],
 )
 
+pw_cc_perf_test(
+    name = "encoder_perf_test",
+    srcs = ["encoder_perf_test.cc"],
+    deps = [
+        ":pw_protobuf",
+        "//pw_unit_test",
+    ],
+)
+
 proto_library(
     name = "codegen_protos",
     srcs = [
diff --git a/pw_protobuf/BUILD.gn b/pw_protobuf/BUILD.gn
index 33c0152..dc4b32e 100644
--- a/pw_protobuf/BUILD.gn
+++ b/pw_protobuf/BUILD.gn
@@ -19,6 +19,7 @@
 import("$dir_pw_build/target_types.gni")
 import("$dir_pw_docgen/docs.gni")
 import("$dir_pw_fuzzer/fuzzer.gni")
+import("$dir_pw_perf_test/perf_test.gni")
 import("$dir_pw_protobuf_compiler/proto.gni")
 import("$dir_pw_unit_test/facade_test.gni")
 import("$dir_pw_unit_test/test.gni")
@@ -182,6 +183,16 @@
   sources = [ "message_test.cc" ]
 }
 
+group("perf_tests") {
+  deps = [ ":encoder_perf_test" ]
+}
+
+pw_perf_test("encoder_perf_test") {
+  enable_if = pw_perf_test_TIMER_INTERFACE_BACKEND != ""
+  deps = [ ":pw_protobuf" ]
+  sources = [ "encoder_perf_test.cc" ]
+}
+
 config("one_byte_varint") {
   defines = [ "PW_PROTOBUF_CFG_MAX_VARINT_SIZE=1" ]
   visibility = [ ":*" ]
diff --git a/pw_protobuf/encoder_perf_test.cc b/pw_protobuf/encoder_perf_test.cc
new file mode 100644
index 0000000..9b4671a
--- /dev/null
+++ b/pw_protobuf/encoder_perf_test.cc
@@ -0,0 +1,38 @@
+// Copyright 2022 The Pigweed Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License"); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+#include "pw_bytes/span.h"
+#include "pw_perf_test/perf_test.h"
+#include "pw_protobuf/encoder.h"
+#include "pw_span/span.h"
+#include "pw_status/status.h"
+#include "pw_stream/memory_stream.h"
+
+namespace pw::protobuf {
+namespace {
+
+void BasicIntegerPerformance(pw::perf_test::State& state, int64_t value) {
+  std::byte encode_buffer[30];
+
+  while (state.KeepRunning()) {
+    MemoryEncoder encoder(encode_buffer);
+    encoder.WriteUint32(1, value).IgnoreError();
+  }
+}
+
+PW_PERF_TEST(SmallIntegerEncoding, BasicIntegerPerformance, 1);
+PW_PERF_TEST(LargerIntegerEncoding, BasicIntegerPerformance, 4000000000);
+
+}  // namespace
+}  // namespace pw::protobuf