Add TestGrpcScope

pull/24170/head
Esun Kim 5 years ago
parent a11e4df082
commit 91ca04cf6f
  1. 33
      test/core/util/test_config.cc
  2. 13
      test/core/util/test_config.h
  3. 13
      test/cpp/microbenchmarks/bm_opencensus_plugin.cc

@ -380,6 +380,19 @@ void grpc_test_init(int /*argc*/, char** /*argv*/) {
srand(seed()); srand(seed());
} }
bool grpc_wait_until_shutdown(int64_t time_s) {
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(time_s);
while (grpc_is_initialized()) {
grpc_maybe_wait_for_async_shutdown();
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
gpr_time_from_millis(1, GPR_TIMESPAN)));
if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
return false;
}
}
return true;
}
namespace grpc { namespace grpc {
namespace testing { namespace testing {
@ -390,15 +403,8 @@ TestEnvironment::TestEnvironment(int argc, char** argv) {
TestEnvironment::~TestEnvironment() { TestEnvironment::~TestEnvironment() {
// This will wait until gRPC shutdown has actually happened to make sure // This will wait until gRPC shutdown has actually happened to make sure
// no gRPC resources (such as thread) are active. (timeout = 10s) // no gRPC resources (such as thread) are active. (timeout = 10s)
gpr_timespec deadline = grpc_timeout_seconds_to_deadline(10); if (!grpc_wait_until_shutdown(10)) {
while (grpc_is_initialized()) { gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
grpc_maybe_wait_for_async_shutdown();
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),
gpr_time_from_millis(1, GPR_TIMESPAN)));
if (gpr_time_cmp(gpr_now(GPR_CLOCK_MONOTONIC), deadline) > 0) {
gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
break;
}
} }
if (BuiltUnderMsan()) { if (BuiltUnderMsan()) {
// This is a workaround for MSAN. MSAN doesn't like having shutdown thread // This is a workaround for MSAN. MSAN doesn't like having shutdown thread
@ -412,5 +418,14 @@ TestEnvironment::~TestEnvironment() {
gpr_log(GPR_INFO, "TestEnvironment ends"); gpr_log(GPR_INFO, "TestEnvironment ends");
} }
TestGrpcScope::TestGrpcScope() { grpc_init(); }
TestGrpcScope::~TestGrpcScope() {
grpc_shutdown();
if (!grpc_wait_until_shutdown(10)) {
gpr_log(GPR_ERROR, "Timeout in waiting for gRPC shutdown");
}
}
} // namespace testing } // namespace testing
} // namespace grpc } // namespace grpc

@ -40,6 +40,10 @@ gpr_timespec grpc_timeout_milliseconds_to_deadline(int64_t time_ms);
// Prefer TestEnvironment below. // Prefer TestEnvironment below.
void grpc_test_init(int argc, char** argv); void grpc_test_init(int argc, char** argv);
// Wait until gRPC is fully shut down.
// Returns if grpc is shutdown
bool grpc_wait_until_shutdown(int64_t time_s);
namespace grpc { namespace grpc {
namespace testing { namespace testing {
@ -51,6 +55,15 @@ class TestEnvironment {
~TestEnvironment(); ~TestEnvironment();
}; };
// A TestGrpcScope makes sure that
// - when it's created, gRPC will be initialized
// - when it's destroyed, gRPC will shutdown and it waits until shutdown
class TestGrpcScope {
public:
TestGrpcScope();
~TestGrpcScope();
};
} // namespace testing } // namespace testing
} // namespace grpc } // namespace grpc

@ -83,8 +83,7 @@ class EchoServerThread final {
}; };
static void BM_E2eLatencyCensusDisabled(benchmark::State& state) { static void BM_E2eLatencyCensusDisabled(benchmark::State& state) {
grpc::testing::TestEnvironment env(0, {}); grpc::testing::TestGrpcScope grpc_scope;
EchoServerThread server; EchoServerThread server;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub = std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
grpc::testing::EchoTestService::NewStub(grpc::CreateChannel( grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
@ -100,14 +99,13 @@ static void BM_E2eLatencyCensusDisabled(benchmark::State& state) {
BENCHMARK(BM_E2eLatencyCensusDisabled); BENCHMARK(BM_E2eLatencyCensusDisabled);
static void BM_E2eLatencyCensusEnabled(benchmark::State& state) { static void BM_E2eLatencyCensusEnabled(benchmark::State& state) {
grpc::testing::TestEnvironment env(0, {});
// Now start the test by registering the plugin (once in the execution) // Now start the test by registering the plugin (once in the execution)
RegisterOnce(); RegisterOnce();
// This we can safely repeat, and doing so clears accumulated data to avoid // This we can safely repeat, and doing so clears accumulated data to avoid
// initialization costs varying between runs. // initialization costs varying between runs.
grpc::RegisterOpenCensusViewsForExport(); grpc::RegisterOpenCensusViewsForExport();
grpc::testing::TestGrpcScope grpc_scope;
EchoServerThread server; EchoServerThread server;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub = std::unique_ptr<grpc::testing::EchoTestService::Stub> stub =
grpc::testing::EchoTestService::NewStub(grpc::CreateChannel( grpc::testing::EchoTestService::NewStub(grpc::CreateChannel(
@ -122,4 +120,9 @@ static void BM_E2eLatencyCensusEnabled(benchmark::State& state) {
} }
BENCHMARK(BM_E2eLatencyCensusEnabled); BENCHMARK(BM_E2eLatencyCensusEnabled);
BENCHMARK_MAIN(); int main(int argc, char** argv) {
grpc::testing::TestEnvironment env(argc, argv);
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1;
::benchmark::RunSpecifiedBenchmarks();
}

Loading…
Cancel
Save