// Copyright 2020 Google LLC. All Rights Reserved. #include #include #include "api/license_whitebox.h" #include "api/license_whitebox_benchmark.h" #include "api/result.h" #include "api/test_data.h" #include "api/test_license_builder.h" #include "base/logging.h" #include "benchmarking/measurements.h" #include "testing/gtest/include/gtest/gtest.h" namespace widevine { namespace { // The white-box implementation is slow, so the build bots keep timing out. To // work around this, use a fixed number of iterations. constexpr size_t kIterations = 100; } // namespace class LicenseWhiteboxProcessLicenseResponseBenchmark : public LicenseWhiteboxBenchmark { protected: void SetUp() override { LicenseWhiteboxBenchmark::SetUp(); license_ = CreateLicense(); } void TearDown() override { WB_License_Delete(whitebox_); } WB_License_Whitebox* whitebox_; License license_; }; TEST_F(LicenseWhiteboxProcessLicenseResponseBenchmark, CreateAndProcessLicenseResponseAndDelete) { Timer timer; Sampler sampler; for (size_t i = 0; i < kIterations; i++) { const auto init_data = GetLicenseInitData(); // We can only call ProcessLicenseResponse() once per whitebox instance. So // we need to create a new instance each time. Collect samples for create + // process license + delete so that we know the cost of getting a new // license. timer.Reset(); ASSERT_EQ(WB_License_Create(init_data.data(), init_data.size(), &whitebox_), WB_RESULT_OK); ASSERT_EQ(WB_License_ProcessLicenseResponse( whitebox_, license_.message.data(), license_.message.size(), license_.signature.data(), license_.signature.size(), license_.session_key.data(), license_.session_key.size(), license_.request.data(), license_.request.size()), WB_RESULT_OK); WB_License_Delete(whitebox_); sampler.Push(timer.Get()); whitebox_ = nullptr; } PrettyPrint("License White-box Create + Process License + Delete Duration", sampler, license_.message.size()); } TEST_F(LicenseWhiteboxProcessLicenseResponseBenchmark, CreateAndProcessLicenseResponse) { Timer timer; Sampler sampler; for (size_t i = 0; i < kIterations; i++) { const auto init_data = GetLicenseInitData(); // We can only call ProcessLicenseResponse() once per whitebox instance. So // we need to create a new instance each time. Collect samples for create + // process license so that we know the true start-up cost. timer.Reset(); ASSERT_EQ(WB_License_Create(init_data.data(), init_data.size(), &whitebox_), WB_RESULT_OK); ASSERT_EQ(WB_License_ProcessLicenseResponse( whitebox_, license_.message.data(), license_.message.size(), license_.signature.data(), license_.signature.size(), license_.session_key.data(), license_.session_key.size(), license_.request.data(), license_.request.size()), WB_RESULT_OK); sampler.Push(timer.Get()); WB_License_Delete(whitebox_); whitebox_ = nullptr; } PrettyPrint("License White-box Create + Process License Duration", sampler, license_.message.size()); } TEST_F(LicenseWhiteboxProcessLicenseResponseBenchmark, ProcessLicenseResponse) { Timer timer; Sampler sampler; for (size_t i = 0; i < kIterations; i++) { // We can only call ProcessLicenseResponse() once per whitebox instance. So // we need to create a new instance each time. Do this before we reset the // timer so that we are not counting it in the execution time. const auto init_data = GetLicenseInitData(); ASSERT_EQ(WB_License_Create(init_data.data(), init_data.size(), &whitebox_), WB_RESULT_OK); timer.Reset(); ASSERT_EQ(WB_License_ProcessLicenseResponse( whitebox_, license_.message.data(), license_.message.size(), license_.signature.data(), license_.signature.size(), license_.session_key.data(), license_.session_key.size(), license_.request.data(), license_.request.size()), WB_RESULT_OK); sampler.Push(timer.Get()); WB_License_Delete(whitebox_); whitebox_ = nullptr; } PrettyPrint("License White-box Process License Duration", sampler, license_.message.size()); } } // namespace widevine