diff --git a/docs/user_guide/environment_variables.rst b/docs/user_guide/environment_variables.rst index 63d6291ed..fe147e016 100644 --- a/docs/user_guide/environment_variables.rst +++ b/docs/user_guide/environment_variables.rst @@ -14,3 +14,7 @@ The following environment variables can be used to configure the project's runti - ``PPC_IGNORE_TEST_TIME_LIMIT``: Specifies that test time limits are ignored. Used by ``scripts/run_tests.py`` to disable time limit enforcement. Default: ``0`` +- ``PPC_TASK_MAX_TIME``: Maximum allowed execution time in seconds for functional tests. + Default: ``1.0`` +- ``PPC_PERF_MAX_TIME``: Maximum allowed execution time in seconds for performance tests. + Default: ``10.0`` diff --git a/modules/performance/include/performance.hpp b/modules/performance/include/performance.hpp index eaafc5378..305d72659 100644 --- a/modules/performance/include/performance.hpp +++ b/modules/performance/include/performance.hpp @@ -10,6 +10,7 @@ #include #include "task/include/task.hpp" +#include "util/include/util.hpp" namespace ppc::performance { @@ -78,14 +79,15 @@ class Perf { } auto time_secs = perf_results_.time_sec; + const auto max_time = ppc::util::GetPerfMaxTime(); std::stringstream perf_res_str; - if (time_secs < PerfResults::kMaxTime) { + if (time_secs < max_time) { perf_res_str << std::fixed << std::setprecision(10) << time_secs; std::cout << test_id << ":" << type_test_name << ":" << perf_res_str.str() << '\n'; } else { std::stringstream err_msg; err_msg << '\n' << "Task execute time need to be: "; - err_msg << "time < " << PerfResults::kMaxTime << " secs." << '\n'; + err_msg << "time < " << max_time << " secs." << '\n'; err_msg << "Original time in secs: " << time_secs << '\n'; perf_res_str << std::fixed << std::setprecision(10) << -1.0; std::cout << test_id << ":" << type_test_name << ":" << perf_res_str.str() << '\n'; diff --git a/modules/performance/tests/perf_tests.cpp b/modules/performance/tests/perf_tests.cpp index 1888e39a3..573cb755f 100644 --- a/modules/performance/tests/perf_tests.cpp +++ b/modules/performance/tests/perf_tests.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,23 @@ TEST(perf_tests, check_perf_pipeline_uint8_t_slow_test) { ASSERT_ANY_THROW(perf_analyzer.PrintPerfStatistic("check_perf_pipeline_uint8_t_slow_test")); } +TEST(perf_tests, slow_perf_respects_env_override) { + env::detail::set_scoped_environment_variable scoped("PPC_PERF_MAX_TIME", "12"); + std::vector in(128, 1); + auto test_task = std::make_shared, uint8_t>>(in); + Perf, uint8_t> perf_analyzer(test_task); + PerfAttr perf_attr; + perf_attr.num_running = 1; + const auto t0 = std::chrono::high_resolution_clock::now(); + perf_attr.current_timer = [&] { + auto current_time_point = std::chrono::high_resolution_clock::now(); + auto duration = std::chrono::duration_cast(current_time_point - t0).count(); + return static_cast(duration) * 1e-9; + }; + perf_analyzer.PipelineRun(perf_attr); + EXPECT_NO_THROW(perf_analyzer.PrintPerfStatistic("slow_perf_respects_env_override")); +} + TEST(perf_tests, check_perf_task_exception) { std::vector in(2000, 1); diff --git a/modules/task/include/task.hpp b/modules/task/include/task.hpp index f54b73b4c..41d9c79d7 100644 --- a/modules/task/include/task.hpp +++ b/modules/task/include/task.hpp @@ -215,12 +215,13 @@ class Task { .count(); auto diff = static_cast(duration) * 1e-9; + const auto max_time = ppc::util::GetTaskMaxTime(); std::stringstream err_msg; - if (diff < kMaxTestTime) { + if (diff < max_time) { err_msg << "Test time:" << std::fixed << std::setprecision(10) << diff << '\n'; } else { err_msg << "\nTask execute time need to be: "; - err_msg << "time < " << kMaxTestTime << " secs.\n"; + err_msg << "time < " << max_time << " secs.\n"; err_msg << "Original time in secs: " << diff << '\n'; throw std::runtime_error(err_msg.str().c_str()); } @@ -249,7 +250,6 @@ class Task { StateOfTesting state_of_testing_ = kFunc; TypeOfTask type_of_task_ = kUnknown; StatusOfTask status_of_task_ = kEnabled; - static constexpr double kMaxTestTime = 1.0; std::chrono::high_resolution_clock::time_point tmp_time_point_; enum class PipelineStage : uint8_t { kNone, diff --git a/modules/task/tests/task_tests.cpp b/modules/task/tests/task_tests.cpp index daedf15a0..720ec5695 100644 --- a/modules/task/tests/task_tests.cpp +++ b/modules/task/tests/task_tests.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,16 @@ TEST(task_tests, check_int32_t_slow) { ASSERT_ANY_THROW(test_task.PostProcessing()); } +TEST(task_tests, slow_task_respects_env_override) { + env::detail::set_scoped_environment_variable scoped("PPC_TASK_MAX_TIME", "3"); + std::vector in(20, 1); + ppc::test::FakeSlowTask, int32_t> test_task(in); + ASSERT_EQ(test_task.Validation(), true); + test_task.PreProcessing(); + test_task.Run(); + EXPECT_NO_THROW(test_task.PostProcessing()); +} + TEST(task_tests, check_validate_func) { std::vector in; ppc::test::TestTask, int32_t> test_task(in); diff --git a/modules/util/include/util.hpp b/modules/util/include/util.hpp index de86faa90..d7ab449ba 100644 --- a/modules/util/include/util.hpp +++ b/modules/util/include/util.hpp @@ -52,6 +52,8 @@ enum GTestParamIndex : uint8_t { kTaskGetter, kNameTest, kTestParams }; std::string GetAbsoluteTaskPath(const std::string& id_path, const std::string& relative_path); int GetNumThreads(); +double GetTaskMaxTime(); +double GetPerfMaxTime(); template std::string GetNamespace() { diff --git a/modules/util/src/util.cpp b/modules/util/src/util.cpp index 8f776bbe0..7fe8d6506 100644 --- a/modules/util/src/util.cpp +++ b/modules/util/src/util.cpp @@ -28,6 +28,22 @@ int ppc::util::GetNumThreads() { return 1; } +double ppc::util::GetTaskMaxTime() { + const auto val = env::get("PPC_TASK_MAX_TIME"); + if (val.has_value()) { + return val.value(); + } + return 1.0; +} + +double ppc::util::GetPerfMaxTime() { + const auto val = env::get("PPC_PERF_MAX_TIME"); + if (val.has_value()) { + return val.value(); + } + return 10.0; +} + // List of environment variables that signal the application is running under // an MPI launcher. The array size must match the number of entries to avoid // looking up empty environment variable names. diff --git a/modules/util/tests/util.cpp b/modules/util/tests/util.cpp index 53450443f..748aa5988 100644 --- a/modules/util/tests/util.cpp +++ b/modules/util/tests/util.cpp @@ -2,6 +2,7 @@ #include +#include #include #include @@ -75,3 +76,35 @@ TEST(GetNamespaceTest, NoTerminatorCharactersInPrettyFunction) { std::string k_ns = ppc::util::GetNamespace(); EXPECT_EQ(k_ns, "crazy"); } + +TEST(GetTaskMaxTime, ReturnsDefaultWhenUnset) { + const auto old = env::get("PPC_TASK_MAX_TIME"); + if (old.has_value()) { + env::detail::delete_environment_variable("PPC_TASK_MAX_TIME"); + } + EXPECT_DOUBLE_EQ(ppc::util::GetTaskMaxTime(), 1.0); + if (old.has_value()) { + env::detail::set_environment_variable("PPC_TASK_MAX_TIME", std::to_string(*old)); + } +} + +TEST(GetTaskMaxTime, ReadsFromEnvironment) { + env::detail::set_scoped_environment_variable scoped("PPC_TASK_MAX_TIME", "2.5"); + EXPECT_DOUBLE_EQ(ppc::util::GetTaskMaxTime(), 2.5); +} + +TEST(GetPerfMaxTime, ReturnsDefaultWhenUnset) { + const auto old = env::get("PPC_PERF_MAX_TIME"); + if (old.has_value()) { + env::detail::delete_environment_variable("PPC_PERF_MAX_TIME"); + } + EXPECT_DOUBLE_EQ(ppc::util::GetPerfMaxTime(), 10.0); + if (old.has_value()) { + env::detail::set_environment_variable("PPC_PERF_MAX_TIME", std::to_string(*old)); + } +} + +TEST(GetPerfMaxTime, ReadsFromEnvironment) { + env::detail::set_scoped_environment_variable scoped("PPC_PERF_MAX_TIME", "12.5"); + EXPECT_DOUBLE_EQ(ppc::util::GetPerfMaxTime(), 12.5); +}