forked from sonic-net/sonic-pins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Thinkit] Replaced GetGenericTestbed with GetTestbedWithRequirements …
…to take in TestbedRequiredments proto. (sonic-net#359) [ThinKit] Fix PacketGenerationFinalizer and RequestType enum. [thinkit] Benchmark performance of Bazel Test Environment. [thinkit] Add a way to set test_case_id with Bazel Test Environment. [thinkit] Add a mutex to BazelTestEnvironment making it thread safe. [thinkit] Language change to remove disrespectful term. [thinkit] Add a test for BazelTestEnvironment using protos. Co-authored-by: rhalstea <[email protected]> Co-authored-by: bhagatgit <[email protected]>
- Loading branch information
1 parent
ca7d506
commit be869c3
Showing
9 changed files
with
170 additions
and
29 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
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
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 |
---|---|---|
@@ -1,32 +1,130 @@ | ||
#include "thinkit/bazel_test_environment.h" | ||
|
||
#include <cstdlib> | ||
#include <memory> | ||
#include <string> | ||
|
||
// Switching benchmark dependency to third_party seems to not output any | ||
// benchmarking information when run. | ||
#include "absl/memory/memory.h" | ||
#include "absl/strings/string_view.h" | ||
#include "benchmark/benchmark.h" | ||
#include "gmock/gmock.h" | ||
#include "google/protobuf/wrappers.pb.h" | ||
#include "gtest/gtest.h" | ||
#include "gutil/status_matchers.h" | ||
#include "gutil/testing.h" | ||
#include "thinkit/test_environment.h" | ||
|
||
namespace thinkit { | ||
namespace { | ||
|
||
using ::gutil::IsOk; | ||
using ::testing::StrEq; | ||
|
||
// -- Tests -------------------------------------------------------------------- | ||
|
||
constexpr absl::string_view kTestArtifact = "my_test_artifact.txt"; | ||
|
||
class BazelTestEnvironmentTest : public testing::Test { | ||
protected: | ||
std::unique_ptr<TestEnvironment> environment_ = | ||
TEST(BazelTestEnvironmentTest, StoreTestArtifact) { | ||
std::unique_ptr<TestEnvironment> environment = | ||
absl::make_unique<BazelTestEnvironment>(/*mask_known_failures=*/true); | ||
}; | ||
|
||
TEST_F(BazelTestEnvironmentTest, StoreTestArtifact) { | ||
EXPECT_OK(environment_->StoreTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
EXPECT_OK(environment_->StoreTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
EXPECT_OK(environment->StoreTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
EXPECT_OK(environment->StoreTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
} | ||
|
||
TEST_F(BazelTestEnvironmentTest, AppendToTestArtifact) { | ||
EXPECT_OK( | ||
environment_->AppendToTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
TEST(BazelTestEnvironmentTest, AppendToTestArtifact) { | ||
std::unique_ptr<TestEnvironment> environment = | ||
absl::make_unique<BazelTestEnvironment>(/*mask_known_failures=*/true); | ||
EXPECT_OK( | ||
environment_->AppendToTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
environment->AppendToTestArtifact(kTestArtifact, "Hello, World!\n")); | ||
EXPECT_OK(environment->AppendToTestArtifact(kTestArtifact, "Hello, Test!\n")); | ||
} | ||
|
||
TEST(BazelTestEnvironmentTest, | ||
BazelTestEnvironmentStoreAndAppendTestArtifactWithProto) { | ||
// Explicitly uses the BazelTestEnvironment to ensure that we inherit | ||
// definitions from TestEnvironment appropriately. | ||
std::unique_ptr<BazelTestEnvironment> environment = | ||
absl::make_unique<BazelTestEnvironment>(/*mask_known_failures=*/true); | ||
auto foo_proto = gutil::ParseProtoOrDie<google::protobuf::Int32Value>(R"pb( | ||
value: 42)pb"); | ||
EXPECT_OK(environment->StoreTestArtifact(kTestArtifact, foo_proto)); | ||
EXPECT_OK(environment->AppendToTestArtifact(kTestArtifact, foo_proto)); | ||
} | ||
|
||
// Test that SetTestCaseID correctly calls its input function. | ||
TEST(BazelTestEnvironmentTest, SetTestCaseIdCallsMemberFunction) { | ||
std::string stored_test_case_id; | ||
std::string test_id = "1"; | ||
|
||
std::unique_ptr<TestEnvironment> environment = | ||
absl::make_unique<BazelTestEnvironment>( | ||
/*mask_known_failures=*/true, | ||
/*set_test_case_id=*/[&](absl::string_view test_case_id) { | ||
stored_test_case_id = test_case_id; | ||
}); | ||
|
||
environment->SetTestCaseID(test_id); | ||
EXPECT_THAT(test_id, StrEq(stored_test_case_id)); | ||
} | ||
|
||
// SetTestCaseID should not crash even if the environment is constructed without | ||
// a function. | ||
TEST(BazelTestEnvironmentTest, SetTestCaseIdWorksForUnaryConstructor) { | ||
std::unique_ptr<TestEnvironment> environment = | ||
absl::make_unique<BazelTestEnvironment>(/*mask_known_failures=*/true); | ||
environment->SetTestCaseID("1"); | ||
} | ||
|
||
// -- Benchmarks --------------------------------------------------------------- | ||
// | ||
// Best run with 'blaze test --benchmarks=all'. | ||
// | ||
// Ideally, we would like to use 'benchy' for benchmarking purposes, but, | ||
// because we are benchmarking a testing environment, it relies on being set | ||
// up as a test environment. | ||
// | ||
// For now, one can manually set the environment variable TEST_TMPDIR, then run | ||
// it with benchy, but do not expect that to remain feasible since we may rely | ||
// on additional parts of the test environment in the future. | ||
|
||
// The state specifies the size of the written string in Bytes. | ||
void RunBenchmark(benchmark::State& state, bool truncate) { | ||
const int size = state.range(0); | ||
const std::string filename = "benchmark_file"; | ||
BazelTestEnvironment env(false); | ||
|
||
std::string str(size, 'a'); | ||
if (truncate) { | ||
for (auto s : state) { | ||
ASSERT_THAT(env.StoreTestArtifact(filename, str), IsOk()); | ||
} | ||
} else { | ||
for (auto s : state) { | ||
ASSERT_THAT(env.AppendToTestArtifact(filename, str), IsOk()); | ||
} | ||
} | ||
// Outputs number of iterations (items) per second. | ||
state.SetItemsProcessed(static_cast<int64_t>(state.iterations())); | ||
} | ||
|
||
void BM_Bazel_AppendToTestArtifact(benchmark::State& state) { | ||
RunBenchmark(state, /*truncate=*/false); | ||
} | ||
|
||
BENCHMARK(BM_Bazel_AppendToTestArtifact) | ||
->Args({/*write_size in bytes=*/1}) | ||
->Args({1024}) | ||
->Args({1024 * 1024}); | ||
|
||
void BM_Bazel_StoreTestArtifact(benchmark::State& state) { | ||
RunBenchmark(state, /*truncate=*/true); | ||
} | ||
|
||
BENCHMARK(BM_Bazel_StoreTestArtifact) | ||
->Args({/*write_size in bytes=*/1}) | ||
->Args({1024}) | ||
->Args({1024 * 1024}); | ||
} // namespace | ||
} // namespace thinkit |
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
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