Skip to content

Commit

Permalink
[Dvaas]: Adding Test_vector Stats files.
Browse files Browse the repository at this point in the history
  • Loading branch information
VSuryaprasad-HCL committed Nov 12, 2024
1 parent b2f4ad2 commit c0542be
Show file tree
Hide file tree
Showing 5 changed files with 355 additions and 0 deletions.
37 changes: 37 additions & 0 deletions dvaas/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,43 @@ cc_library(
],
)

cc_library(
name = "test_vector_stats",
srcs = ["test_vector_stats.cc"],
hdrs = ["test_vector_stats.h"],
deps = [
":test_vector_cc_proto",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
],
)

# go/golden-test-with-coverage
cc_test(
name = "test_vector_stats_test",
srcs = ["test_vector_stats_test.cc"],
linkstatic = True,
deps = [
":test_vector_cc_proto",
":test_vector_stats",
"//gutil:testing",
"@com_google_googletest//:gtest_main",
],
)

cmd_diff_test(
name = "test_vector_stats_diff_test",
actual_cmd = " | ".join([
"$(execpath :test_vector_stats_test)",
# Strip unnecessary lines for golden testing.
"sed '1,/^\\[ RUN/d'", # Strip everything up to a line beginning with '[ RUN'.
"sed '/^\\[/d'", # Strip every line beginning with '['.
]),
expected = "test_vector_stats_test.expected",
tools = [":test_vector_stats_test"],
)

cc_library(
name = "mirror_testbed_config",
testonly = True,
Expand Down
108 changes: 108 additions & 0 deletions dvaas/test_vector_stats.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2024 Google LLC
//
// 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
//
// http://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 "dvaas/test_vector_stats.h"

#include <string>

#include "absl/algorithm/container.h"
#include "absl/strings/str_format.h"
#include "dvaas/test_vector.pb.h"

namespace dvaas {

namespace {

void AddTestVectorStats(const PacketTestOutcome& outcome,
TestVectorStats& stats) {
stats.num_vectors += 1;

if (!outcome.test_result().has_failure()) {
stats.num_vectors_passed += 1;
}

const SwitchOutput& actual_output = outcome.test_run().actual_output();
const int num_forwarded = actual_output.packets_size();
const int num_punted = actual_output.packet_ins_size();

bool has_correct_num_outputs = absl::c_any_of(
outcome.test_run().test_vector().acceptable_outputs(),
[&](const SwitchOutput& acceptable_output) {
return num_forwarded == acceptable_output.packets_size() &&
num_punted == acceptable_output.packet_ins_size();
});
if (has_correct_num_outputs) {
stats.num_vectors_with_correct_number_of_outputs += 1;
}

stats.num_vectors_forwarding += num_forwarded > 0 ? 1 : 0;
stats.num_vectors_punting += num_punted > 0 ? 1 : 0;
stats.num_vectors_dropping += num_forwarded == 0 && num_punted == 0;
stats.num_packets_forwarded += num_forwarded;
stats.num_packets_punted += num_punted;
}

} // namespace

TestVectorStats ComputeTestVectorStats(
const PacketTestOutcomes& test_outcomes) {
TestVectorStats stats;
for (const auto& outcome : test_outcomes.outcomes()) {
AddTestVectorStats(outcome, stats);
}
return stats;
}

namespace {

std::string ExplainPercent(double fraction) {
return absl::StrFormat("%.2f%%", fraction * 100);
}
std::string ExplainPercent(int numerator, int denominator) {
if (denominator == 0) return "100%";
return ExplainPercent(static_cast<double>(numerator) / denominator);
}

std::string ExplainFraction(int numerator, int denominator) {
return absl::StrFormat("%s of %d", ExplainPercent(numerator, denominator),
denominator);
}

} // namespace

std::string ExplainTestVectorStats(const TestVectorStats& stats) {
std::string result;

absl::StrAppendFormat(
&result, "%s test vectors passed\n",
ExplainFraction(stats.num_vectors_passed, stats.num_vectors));
absl::StrAppendFormat(
&result,
"%s test vectors produced the correct number and type of output "
"packets\n",
ExplainFraction(stats.num_vectors_with_correct_number_of_outputs,
stats.num_vectors));
absl::StrAppendFormat(
&result,
"%d test vectors forwarded, producing %d forwarded output packets\n",
stats.num_vectors_forwarding, stats.num_packets_forwarded);
absl::StrAppendFormat(
&result, "%d test vectors punted, producing %d punted output packets\n",
stats.num_vectors_punting, stats.num_packets_punted);
absl::StrAppendFormat(&result, "%d test vectors produced no output packets\n",
stats.num_vectors_dropping);
return result;
}

} // namespace dvaas
44 changes: 44 additions & 0 deletions dvaas/test_vector_stats.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 Google LLC
//
// 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
//
// http://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.

#ifndef PINS_DVAAS_TEST_VECTOR_STATS_H_
#define PINS_DVAAS_TEST_VECTOR_STATS_H_

#include <string>

#include "dvaas/test_vector.pb.h"

namespace dvaas {

struct TestVectorStats {
int num_vectors = 0;
int num_vectors_passed = 0;
// Can be higher than `num_vectors_passed`, e.g. because the outputs
// could have incorrect header field values.
int num_vectors_with_correct_number_of_outputs = 0;
int num_vectors_forwarding = 0;
int num_vectors_punting = 0;
int num_vectors_dropping = 0;

int num_packets_forwarded = 0;
int num_packets_punted = 0;
};

TestVectorStats ComputeTestVectorStats(const PacketTestOutcomes& test_outcomes);

std::string ExplainTestVectorStats(const TestVectorStats& stats);

} // namespace dvaas

#endif // PINS_DVAAS_TEST_VECTOR_STATS_H_
161 changes: 161 additions & 0 deletions dvaas/test_vector_stats_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2024 Google LLC
//
// 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
//
// http://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 "dvaas/test_vector_stats.h"

#include <iostream>

#include "dvaas/test_vector.pb.h"
#include "gtest/gtest.h"
#include "gutil/testing.h"

namespace dvaas {
namespace {

PacketTestOutcomes GetPacketTestOutcomees() {
return gutil::ParseProtoOrDie<PacketTestOutcomes>(R"pb(
# Correct drop.
outcomes {
test_run {
test_vector {
input {}
# Deterministic drop.
acceptable_outputs {
packets: []
packet_ins: []
}
}
actual_output {
packets: []
packet_ins: []
}
}
# Passed.
test_result {}
}
# Incorrect forward.
outcomes {
test_run {
test_vector {
input {}
# Deterministic forward.
acceptable_outputs {
packets {}
packet_ins: []
}
}
actual_output {
packets {}
packet_ins: []
}
}
# Failed.
test_result { failure {} }
}
# Correct forward.
outcomes {
test_run {
test_vector {
input {}
# Deterministic forward.
acceptable_outputs {
packets {}
packet_ins: []
}
}
actual_output {
packets {}
packet_ins: []
}
}
# Passed.
test_result {}
}
# Correct multi forward.
outcomes {
test_run {
test_vector {
input {}
# Deterministic multi forward.
acceptable_outputs {
packets {}
packets {}
packets {}
packet_ins: []
}
}
actual_output {
packets {}
packets {}
packets {}
packet_ins: []
}
}
# Passed.
test_result {}
}
# Incorrect punt.
outcomes {
test_run {
test_vector {
input {}
# Deterministic trap.
acceptable_outputs {
packets: []
packet_ins {}
}
}
actual_output {
packets: []
packet_ins: {}
}
}
# Failed.
test_result { failure {} }
}
# Correct forward & copy.
outcomes {
test_run {
test_vector {
input {}
# Deterministic forward & copy.
acceptable_outputs {
packets {}
packet_ins {}
}
}
actual_output {
packets {}
packet_ins: {}
}
}
# Passed.
test_result {}
}
)pb");
}

TEST(TestVectorStatsGoldenTest,
ComputeTestVectorStatsAndExplainTestVectorStats) {
PacketTestOutcomes outcomes = GetPacketTestOutcomees();
TestVectorStats stats = ComputeTestVectorStats(outcomes);
std::cout << ExplainTestVectorStats(stats);
}
} // namespace
} // namespace dvaas
5 changes: 5 additions & 0 deletions dvaas/test_vector_stats_test.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
66.67% of 6 test vectors passed
100.00% of 6 test vectors produced the correct number and type of output packets
4 test vectors forwarded, producing 6 forwarded output packets
2 test vectors punted, producing 2 punted output packets
1 test vectors produced no output packets

0 comments on commit c0542be

Please sign in to comment.