-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dvaas]: Adding Test_vector Stats files.
- Loading branch information
1 parent
b2f4ad2
commit c0542be
Showing
5 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |