Repository: envoyproxy/envoy
File: test/common/upstream/metadata_comparison_benchmark.cc (C++)

Code before:
  std::vector<envoy::config::core::v3::Metadata> incoming;
  existing.reserve(num_hosts);
  incoming.reserve(num_hosts);
  for (int i = 0; i < num_hosts; i++) {
    existing.push_back(buildMetadata(1, num_fields));
    incoming.push_back(buildMetadata(1, num_fields));
  }

  for (auto _ : state) { // NOLINT
    int changed = 0;
    for (int i = 0; i < num_hosts; i++) {
      if (!Protobuf::util::MessageDifferencer::Equivalent(existing[i], incoming[i])) {
        changed++;
      }
    }
    ::benchmark::DoNotOptimize(changed);
  }
}
BENCHMARK(bmUpdateHostListEquivalent)
    ->Args({100, 5})
    ->Args({1000, 5})
    ->Args({5000, 5})
    ->Args({5000, 20})
    ->Unit(::benchmark::kMillisecond);

void bmUpdateHostListHash(::benchmark::State& state) {
  const int num_hosts = state.range(0);
  const int num_fields = state.range(1);
  std::vector<envoy::config::core::v3::Metadata> existing;
  std::vector<envoy::config::core::v3::Metadata> incoming;
  existing.reserve(num_hosts);
  incoming.reserve(num_hosts);
  for (int i = 0; i < num_hosts; i++) {
    existing.push_back(buildMetadata(1, num_fields));
    incoming.push_back(buildMetadata(1, num_fields));
  }

  for (auto _ : state) { // NOLINT
    int changed = 0;
    for (int i = 0; i < num_hosts; i++) {
      if (MessageUtil::hash(existing[i]) != MessageUtil::hash(incoming[i])) {
        changed++;
      }
    }
    ::benchmark::DoNotOptimize(changed);
  }
}
BENCHMARK(bmUpdateHostListHash)
    ->Args({100, 5})
    ->Args({1000, 5})
    ->Args({5000, 5})

Review comment (performance):
do you mind adding a `skipExpensiveBenchmarks` to expensive bench mark tests, a similar pattern: https://github.com/envoyproxy/envoy/blob/7a45b03fe22023fa995d9fb4e20a70444e2e1904/test/extensions/load_balancing_policies/least_request/least_request_lb_benchmark.cc#L28

Code after:
    state.SkipWithError("Skipping expensive benchmark");
    return;
  }
  std::vector<envoy::config::core::v3::Metadata> existing;
  std::vector<envoy::config::core::v3::Metadata> incoming;
  existing.reserve(num_hosts);
  incoming.reserve(num_hosts);
  for (int i = 0; i < num_hosts; i++) {
    existing.push_back(buildMetadata(1, num_fields));
    incoming.push_back(buildMetadata(1, num_fields));
  }

  for (auto _ : state) { // NOLINT
    int changed = 0;
    for (int i = 0; i < num_hosts; i++) {
      if (!Protobuf::util::MessageDifferencer::Equivalent(existing[i], incoming[i])) {
        changed++;
      }
    }
    ::benchmark::DoNotOptimize(changed);
  }
}
BENCHMARK(bmUpdateHostListEquivalent)
    ->Args({100, 5})
    ->Args({1000, 5})
    ->Args({5000, 5})
    ->Args({5000, 20})
    ->Unit(::benchmark::kMillisecond);

void bmUpdateHostListHash(::benchmark::State& state) {
  const int num_hosts = state.range(0);
  const int num_fields = state.range(1);
  if (benchmark::skipExpensiveBenchmarks() && num_hosts > 1000) {
    state.SkipWithError("Skipping expensive benchmark");
    return;
  }
  std::vector<envoy::config::core::v3::Metadata> existing;
  std::vector<envoy::config::core::v3::Metadata> incoming;
  existing.reserve(num_hosts);
  incoming.reserve(num_hosts);
  for (int i = 0; i < num_hosts; i++) {
    existing.push_back(buildMetadata(1, num_fields));
    incoming.push_back(buildMetadata(1, num_fields));
  }

  for (auto _ : state) { // NOLINT
    int changed = 0;
    for (int i = 0; i < num_hosts; i++) {
      if (MessageUtil::hash(existing[i]) != MessageUtil::hash(incoming[i])) {
        changed++;
      }