From: Marcin Niesluchowski Date: Mon, 20 Oct 2014 12:49:13 +0000 (+0200) Subject: Add PerformanceResult class X-Git-Tag: security-manager_5.5_testing~122^2~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=61dcae22f2c6c15bface68bce9f50134876d2ccd;p=platform%2Fcore%2Ftest%2Fsecurity-tests.git Add PerformanceResult class Test time measurement framework mechanism refactoring for performance tests. Change-Id: I08f3206f0dcd0504cb5bae6fa22f7480d5533a36 --- diff --git a/src/framework/include/dpl/test/performance_result.h b/src/framework/include/dpl/test/performance_result.h new file mode 100644 index 00000000..f15c0da6 --- /dev/null +++ b/src/framework/include/dpl/test/performance_result.h @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ +/* + * @file performance_result.h + * @author Marcin Niesluchowski (m.niesluchow@samsung.com) + * @version 1.0 + * @brief Header file with declaration of PerformanceResult class + */ + +#ifndef DPL_TEST_PERFORMANCE_RESULT_H +#define DPL_TEST_PERFORMANCE_RESULT_H + +#include + +#include +#include + +namespace DPL { +namespace Test { + +class PerformanceResult; + +typedef std::shared_ptr PerformanceResultPtr; +typedef std::shared_ptr ConstPerformanceResultPtr; + +class PerformanceResult +{ +public: + PerformanceResult(const std::chrono::system_clock::duration& maxDuration) + : m_startTime(std::chrono::system_clock::now()) + , m_duration(std::chrono::microseconds::zero()) + , m_maxDuration(maxDuration < std::chrono::microseconds::zero() + ? std::chrono::microseconds::zero() + : maxDuration) {} + + PerformanceResult(BinaryQueue &queue) + : m_startTime(std::chrono::system_clock::now()) + , m_duration(std::chrono::microseconds::zero()) + , m_maxDuration(std::chrono::microseconds::zero()) { + queue.FlattenConsume(const_cast(&m_startTime), + sizeof(std::chrono::system_clock::time_point)); + queue.FlattenConsume(const_cast(&m_duration), + sizeof(std::chrono::system_clock::duration)); + queue.FlattenConsume(const_cast(&m_maxDuration), + sizeof(std::chrono::system_clock::duration)); + } + + bool IsMaxDuration() const { + return m_maxDuration > std::chrono::microseconds::zero(); + } + + bool IsDurationOk() const { + return m_duration <= m_maxDuration; + } + + const std::chrono::system_clock::duration &GetDuration() const { + return m_duration; + } + + const std::chrono::system_clock::duration &GetMaxDuration() const { + return m_maxDuration; + } + + void Finish() { + if (m_duration == std::chrono::microseconds::zero()) + m_duration = std::chrono::system_clock::now() - m_startTime; + } + + const std::string ToBinaryString() const { + std::string strStartTime(reinterpret_cast(&m_startTime), sizeof(m_startTime)); + std::string strDuration(reinterpret_cast(&m_duration), sizeof(m_duration)); + std::string strMaxDuration(reinterpret_cast(&m_maxDuration), + sizeof(m_maxDuration)); + return strStartTime + strDuration + strMaxDuration; + } + +private: + const std::chrono::system_clock::time_point m_startTime; + std::chrono::system_clock::duration m_duration; + const std::chrono::system_clock::duration m_maxDuration; +}; + +} // namespace Test +} // namespace DPL + +#endif // DPL_TEST_PERFORMANCE_RESULT_H diff --git a/src/framework/include/dpl/test/test_results_collector.h b/src/framework/include/dpl/test/test_results_collector.h index cfd92277..6b1f6feb 100644 --- a/src/framework/include/dpl/test/test_results_collector.h +++ b/src/framework/include/dpl/test/test_results_collector.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +24,11 @@ #define DPL_TEST_RESULTS_COLLECTOR_H #include +#include + #include #include #include -#include #include #include @@ -65,9 +66,7 @@ class TestResultsCollectorBase : virtual void CollectResult(const std::string& id, const FailStatus status = FailStatus::NONE, const std::string& reason = "", - const bool& isPerformanceTest = false, - const std::chrono::system_clock::duration& performanceTime = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime = std::chrono::microseconds::zero()) = 0; + const ConstPerformanceResultPtr &performanceResult = nullptr) = 0; virtual std::string CollectorSpecificHelp() const { return ""; diff --git a/src/framework/include/dpl/test/test_results_collector_console.h b/src/framework/include/dpl/test/test_results_collector_console.h index 4fbe00f1..d56ccfad 100644 --- a/src/framework/include/dpl/test/test_results_collector_console.h +++ b/src/framework/include/dpl/test/test_results_collector_console.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,11 +46,7 @@ private: virtual void CollectResult(const std::string& id, const FailStatus status = FailStatus::NONE, const std::string& reason = "", - const bool& isPerformanceTest = true, - const std::chrono::system_clock::duration& performanceTime - = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime - = std::chrono::microseconds::zero()); + const ConstPerformanceResultPtr &performanceResult = nullptr); virtual void Finish(); void PrintfErrorMessage(const char* type, const std::string& message); diff --git a/src/framework/include/dpl/test/test_results_collector_html.h b/src/framework/include/dpl/test/test_results_collector_html.h index 6d8eafc5..8efe6d2a 100644 --- a/src/framework/include/dpl/test/test_results_collector_html.h +++ b/src/framework/include/dpl/test/test_results_collector_html.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,11 +49,7 @@ private: virtual void CollectResult(const std::string& id, const FailStatus status = FailStatus::NONE, const std::string& reason = "", - const bool& isPerformanceTest = false, - const std::chrono::system_clock::duration& performanceTime - = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime - = std::chrono::microseconds::zero()); + const ConstPerformanceResultPtr &performanceResult = nullptr); virtual void Finish(); void PrintfErrorMessage(const char* type, const std::string& message); diff --git a/src/framework/include/dpl/test/test_results_collector_summary.h b/src/framework/include/dpl/test/test_results_collector_summary.h index f0edfccb..ffc87690 100644 --- a/src/framework/include/dpl/test/test_results_collector_summary.h +++ b/src/framework/include/dpl/test/test_results_collector_summary.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,11 +48,7 @@ private: virtual void CollectResult(const std::string& id, const FailStatus status = FailStatus::NONE, const std::string& reason = "", - const bool& isPerformanceTest = false, - const std::chrono::system_clock::duration& performanceTime - = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime - = std::chrono::microseconds::zero()); + const ConstPerformanceResultPtr &performanceResult = nullptr); virtual void Finish(); void writeStats(bool segfault); diff --git a/src/framework/include/dpl/test/test_results_collector_xml.h b/src/framework/include/dpl/test/test_results_collector_xml.h index 16bfa5a9..49ea7954 100644 --- a/src/framework/include/dpl/test/test_results_collector_xml.h +++ b/src/framework/include/dpl/test/test_results_collector_xml.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,11 +47,7 @@ private: virtual void CollectResult(const std::string& id, const FailStatus status = FailStatus::NONE, const std::string& reason = "", - const bool& isPerformanceTest = false, - const std::chrono::system_clock::duration& performanceTime - = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime - = std::chrono::microseconds::zero()); + const ConstPerformanceResultPtr &performanceResult = nullptr); virtual void Finish(); void GroupStart(const std::size_t pos, const std::string& name); diff --git a/src/framework/include/dpl/test/test_runner.h b/src/framework/include/dpl/test/test_runner.h index 373b3194..063fafb3 100644 --- a/src/framework/include/dpl/test/test_runner.h +++ b/src/framework/include/dpl/test/test_runner.h @@ -41,6 +41,7 @@ #include #include #include +#include #include namespace DPL { @@ -63,14 +64,10 @@ class TestRunner , m_allowChildLogs(false) {} - void beginPerformanceTestTime(std::chrono::system_clock::duration maxTimeInMicroseconds); - void endPerformanceTestTime(); - void getCurrentTestCasePerformanceResult(bool& isPerformanceTest, - std::chrono::system_clock::duration& result, - std::chrono::system_clock::duration& resultMax); - void setCurrentTestCasePerformanceResult(bool isPerformanceTest, - std::chrono::system_clock::duration result, - std::chrono::system_clock::duration resultMax); + void beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds); + void endPerformance(); + void setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance); + ConstPerformanceResultPtr getCurrentTestCasePerformanceResult(); void addFailReason(const std::string &reason); @@ -81,11 +78,7 @@ class TestRunner { std::string name; TestCase proc; - - bool m_isPerformanceTest; - std::chrono::system_clock::time_point m_performanceTestStartTime; - std::chrono::system_clock::duration m_performanceTestDurationTime; - std::chrono::system_clock::duration m_performanceMaxTime; + PerformanceResultPtr performance; bool operator <(const TestCaseStruct &other) const { @@ -99,8 +92,7 @@ class TestRunner TestCaseStruct(const std::string &n, TestCase p) : name(n), - proc(p), - m_isPerformanceTest(false) + proc(p) {} }; @@ -147,10 +139,7 @@ class TestRunner const TestResultsCollectorBase::FailStatus status = TestResultsCollectorBase::FailStatus::NONE, const std::string& reason = std::string(), - const bool& isPerformanceTest = false, - const std::chrono::system_clock::duration& performanceTime = std::chrono::microseconds::zero(), - const std::chrono::system_clock::duration& performanceMaxTime = std::chrono::microseconds::zero()); - + const ConstPerformanceResultPtr &performance = nullptr); public: class TestFailed { @@ -331,13 +320,13 @@ typedef DPL::Singleton TestRunnerSingleton; #define RUNNER_PERF_TEST_BEGIN(maxTime) \ do { \ - DPL::Test::TestRunnerSingleton::Instance().beginPerformanceTestTime( \ + DPL::Test::TestRunnerSingleton::Instance().beginPerformance( \ std::chrono::microseconds{static_cast(maxTime*1000000.0)}); \ } while (0) -#define RUNNER_PERF_TEST_END() \ - do { \ - DPL::Test::TestRunnerSingleton::Instance().endPerformanceTestTime(); \ +#define RUNNER_PERF_TEST_END() \ + do { \ + DPL::Test::TestRunnerSingleton::Instance().endPerformance(); \ } while (0) /** diff --git a/src/framework/include/dpl/test/test_runner_child.h b/src/framework/include/dpl/test/test_runner_child.h index d1e4b1c7..8c52600c 100644 --- a/src/framework/include/dpl/test/test_runner_child.h +++ b/src/framework/include/dpl/test/test_runner_child.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,15 +50,11 @@ class PipeWrapper : DPL::Noncopyable virtual ~PipeWrapper(); Status send(int code, std::string &message); - Status sendTime(int code, - std::chrono::system_clock::duration time, - std::chrono::system_clock::duration timeMax); + Status sendPerformance(const ConstPerformanceResultPtr &performance); Status receive(int &code, - int &msgType, std::string &data, - std::chrono::system_clock::duration &time, - std::chrono::system_clock::duration &timeMax, + PerformanceResultPtr &performance, time_t deadline); void closeAll(); @@ -66,7 +62,6 @@ class PipeWrapper : DPL::Noncopyable protected: std::string toBinaryString(int data); - std::string toBinaryString(std::chrono::system_clock::duration data); void closeHelp(int desc); diff --git a/src/framework/src/test_results_collector_console.cpp b/src/framework/src/test_results_collector_console.cpp index 9ad0c044..2c3a7374 100644 --- a/src/framework/src/test_results_collector_console.cpp +++ b/src/framework/src/test_results_collector_console.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,9 +73,7 @@ void ConsoleCollector::Finish() void ConsoleCollector::CollectResult(const std::string& id, const FailStatus status, const std::string& reason, - const bool& isPerformanceTest, - const std::chrono::system_clock::duration& performanceTime, - const std::chrono::system_clock::duration& performanceMaxTime) + const ConstPerformanceResultPtr &performanceResult) { using namespace DPL::Colors::Text; std::string tmp = "'" + id + "' ..."; @@ -83,25 +81,24 @@ void ConsoleCollector::CollectResult(const std::string& id, printf("Running test case %-60s", tmp.c_str()); switch (status) { case FailStatus::NONE: - if (isPerformanceTest) { - if (performanceMaxTime <= std::chrono::microseconds::zero()) { - printf(GREEN_RESULT_OK_TIME, - get_milliseconds(performanceTime)); - break; - } - else { - if (performanceTime > performanceMaxTime) - printf(GREEN_RESULT_OK_TIME_TOO_LONG( - get_milliseconds(performanceTime), - get_milliseconds(performanceMaxTime))); - else - printf(GREEN_RESULT_OK_TIME_MAX( - get_milliseconds(performanceTime), - get_milliseconds(performanceMaxTime))); - break; - } + if (!performanceResult) { + printf(GREEN_RESULT_OK); + break; } - printf(GREEN_RESULT_OK); + if (!performanceResult->IsMaxDuration()) { + printf(GREEN_RESULT_OK_TIME, + get_milliseconds(performanceResult->GetDuration())); + break; + } + if (!performanceResult->IsDurationOk()) { + printf(GREEN_RESULT_OK_TIME_TOO_LONG( + get_milliseconds(performanceResult->GetDuration()), + get_milliseconds(performanceResult->GetMaxDuration()))); + break; + } + printf(GREEN_RESULT_OK_TIME_MAX( + get_milliseconds(performanceResult->GetDuration()), + get_milliseconds(performanceResult->GetMaxDuration()))); break; case FailStatus::FAILED: PrintfErrorMessage(" FAILED ", reason); diff --git a/src/framework/src/test_results_collector_html.cpp b/src/framework/src/test_results_collector_html.cpp index ee76c493..eb390109 100644 --- a/src/framework/src/test_results_collector_html.cpp +++ b/src/framework/src/test_results_collector_html.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -108,9 +108,7 @@ bool HtmlCollector::ParseCollectorSpecificArg(const std::string& arg) void HtmlCollector::CollectResult(const std::string& id, const FailStatus status, const std::string& reason, - const bool& isPerformanceTest, - const std::chrono::system_clock::duration& performanceTime, - const std::chrono::system_clock::duration& performanceMaxTime) + const ConstPerformanceResultPtr &performanceResult) { using namespace DPL::Colors::Html; std::string tmp = "'" + id + "' ..."; @@ -118,24 +116,24 @@ void HtmlCollector::CollectResult(const std::string& id, fprintf(m_fp.Get(), "Running test case %-100s", tmp.c_str()); switch (status) { case FailStatus::NONE: - if (isPerformanceTest) { - if (performanceMaxTime <= std::chrono::microseconds::zero()) { - fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME, - get_milliseconds(performanceTime)); - break; - } else { - if (performanceTime > performanceMaxTime) - fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG( - get_milliseconds(performanceTime), - get_milliseconds(performanceMaxTime))); - else - fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX( - get_milliseconds(performanceTime), - get_milliseconds(performanceMaxTime))); - break; - } + if (!performanceResult) { + fprintf(m_fp.Get(), GREEN_RESULT_OK); + break; } - fprintf(m_fp.Get(), GREEN_RESULT_OK); + if (!performanceResult->IsMaxDuration()) { + fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME, + get_milliseconds(performanceResult->GetDuration())); + break; + } + if (!performanceResult->IsDurationOk()) { + fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_TOO_LONG( + get_milliseconds(performanceResult->GetDuration()), + get_milliseconds(performanceResult->GetMaxDuration()))); + break; + } + fprintf(m_fp.Get(), GREEN_RESULT_OK_TIME_MAX( + get_milliseconds(performanceResult->GetDuration()), + get_milliseconds(performanceResult->GetMaxDuration()))); break; case FailStatus::FAILED: PrintfErrorMessage(" FAILED ", reason); diff --git a/src/framework/src/test_results_collector_summary.cpp b/src/framework/src/test_results_collector_summary.cpp index 1fe83fec..39789a85 100644 --- a/src/framework/src/test_results_collector_summary.cpp +++ b/src/framework/src/test_results_collector_summary.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -64,15 +64,11 @@ void SummaryCollector::Start() void SummaryCollector::CollectResult(const std::string& id, const FailStatus status, const std::string& reason, - const bool& isPerformanceTest, - const std::chrono::system_clock::duration& performanceTime, - const std::chrono::system_clock::duration& performanceMaxTime) + const ConstPerformanceResultPtr &performanceResult) { DPL_UNUSED_PARAM(id); DPL_UNUSED_PARAM(reason); - DPL_UNUSED_PARAM(isPerformanceTest); - DPL_UNUSED_PARAM(performanceTime); - DPL_UNUSED_PARAM(performanceMaxTime); + DPL_UNUSED_PARAM(performanceResult); m_stats.AddTest(status); writeStats(true); diff --git a/src/framework/src/test_results_collector_xml.cpp b/src/framework/src/test_results_collector_xml.cpp index 65f751e5..fe661aa2 100644 --- a/src/framework/src/test_results_collector_xml.cpp +++ b/src/framework/src/test_results_collector_xml.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -127,38 +127,35 @@ bool XmlCollector::ParseCollectorSpecificArg(const std::string& arg) void XmlCollector::CollectResult(const std::string& id, const FailStatus status, const std::string& reason, - const bool& isPerformanceTest, - const std::chrono::system_clock::duration& performanceTime, - const std::chrono::system_clock::duration& performanceMaxTime) + const ConstPerformanceResultPtr &performanceResult) { m_resultBuffer.erase(); m_resultBuffer.append("\t\t\n"); - break; - } else { - m_resultBuffer.append(" status=\"OK\" time=\""); - std::ostringstream ostr; - ostr << performanceTime.count(); - m_resultBuffer.append(ostr.str()); - m_resultBuffer.append("\" time_expected=\""); - ostr.str(""); - ostr << performanceMaxTime.count(); - m_resultBuffer.append(ostr.str()); - m_resultBuffer.append("\"/>\n"); - break; - } + if (!performanceResult) { + m_resultBuffer.append(" status=\"OK\"/>\n"); + break; + } + if (!performanceResult->IsMaxDuration()) { + m_resultBuffer.append(" status=\"OK\" time=\""); + ostr << performanceResult->GetDuration().count(); + m_resultBuffer.append(ostr.str()); + m_resultBuffer.append("\"/>\n"); + break; } - m_resultBuffer.append(" status=\"OK\"/>\n"); + m_resultBuffer.append(" status=\"OK\" time=\""); + ostr << performanceResult->GetDuration().count(); + m_resultBuffer.append(ostr.str()); + m_resultBuffer.append("\" time_expected=\""); + ostr.str(""); + ostr << performanceResult->GetMaxDuration().count(); + m_resultBuffer.append(ostr.str()); + m_resultBuffer.append("\"/>\n"); break; case FailStatus::FAILED: m_resultBuffer.append(" status=\"FAILED\">\n"); diff --git a/src/framework/src/test_runner.cpp b/src/framework/src/test_runner.cpp index ae9bbc52..dd649681 100644 --- a/src/framework/src/test_runner.cpp +++ b/src/framework/src/test_runner.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -286,10 +286,8 @@ TestRunner::Status TestRunner::RunTestCase(const TestCaseStruct& testCase) CollectResult(testCase.name, TestResultsCollectorBase::FailStatus::NONE, - "", - testCase.m_isPerformanceTest, - testCase.m_performanceTestDurationTime, - testCase.m_performanceMaxTime); + std::string(), + testCase.performance); setCurrentTestCase(nullptr); // Everything OK @@ -360,56 +358,41 @@ void TestRunner::setCurrentTestCase(TestCaseStruct* testCase) m_currentTestCase = testCase; } -void TestRunner::beginPerformanceTestTime(std::chrono::system_clock::duration maxTimeInMicroseconds) +void TestRunner::beginPerformance(std::chrono::system_clock::duration maxDurationInMicroseconds) { TestCaseStruct* testCase = getCurrentTestCase(); if (!testCase) return; - testCase->m_isPerformanceTest = true; - testCase->m_performanceMaxTime = maxTimeInMicroseconds; - testCase->m_performanceTestStartTime = std::chrono::system_clock::now(); - - // Set result to 0 microseconds. Display 0ms result when end macro is missing. - testCase->m_performanceTestDurationTime = std::chrono::microseconds::zero(); + if (!testCase->performance) + testCase->performance.reset(new PerformanceResult(maxDurationInMicroseconds)); } -void TestRunner::endPerformanceTestTime() +void TestRunner::endPerformance() { TestCaseStruct* testCase = getCurrentTestCase(); if (!testCase) return; - testCase->m_performanceTestDurationTime = std::chrono::system_clock::now() - - testCase->m_performanceTestStartTime; + testCase->performance->Finish(); } -void TestRunner::getCurrentTestCasePerformanceResult(bool& isPerformanceTest, - std::chrono::system_clock::duration& result, - std::chrono::system_clock::duration& resultMax) +ConstPerformanceResultPtr TestRunner::getCurrentTestCasePerformanceResult() { TestCaseStruct* testCase = getCurrentTestCase(); - if (!testCase || !(testCase->m_isPerformanceTest)){ - isPerformanceTest = false; - return; - } + if (!testCase) + return nullptr; - isPerformanceTest = testCase->m_isPerformanceTest; - result = testCase->m_performanceTestDurationTime; - resultMax = testCase->m_performanceMaxTime; + return testCase->performance; } -void TestRunner::setCurrentTestCasePerformanceResult(bool isPerformanceTest, - std::chrono::system_clock::duration result, - std::chrono::system_clock::duration resultMax) +void TestRunner::setCurrentTestCasePerformanceResult(const PerformanceResultPtr &performance) { TestCaseStruct* testCase = getCurrentTestCase(); if (!testCase) return; - testCase->m_isPerformanceTest = isPerformanceTest; - testCase->m_performanceTestDurationTime = result; - testCase->m_performanceMaxTime = resultMax; + testCase->performance = performance; } void TestRunner::addFailReason(const std::string &reason) @@ -432,9 +415,7 @@ void TestRunner::CollectResult( const std::string& id, const TestResultsCollectorBase::FailStatus status, const std::string& reason, - const bool& isPerformanceTest, - const std::chrono::system_clock::duration& performanceTestDurationTime, - const std::chrono::system_clock::duration& performanceMaxTime) + const ConstPerformanceResultPtr &performance) { std::for_each(m_collectors.begin(), m_collectors.end(), @@ -443,9 +424,7 @@ void TestRunner::CollectResult( collector.second->CollectResult(id, status, reason, - isPerformanceTest, - performanceTestDurationTime, - performanceMaxTime); + performance); }); } diff --git a/src/framework/src/test_runner_child.cpp b/src/framework/src/test_runner_child.cpp index 19ed08c5..b4157bae 100644 --- a/src/framework/src/test_runner_child.cpp +++ b/src/framework/src/test_runner_child.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2013-2015 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -47,9 +47,6 @@ const int CHILD_TEST_FAIL = 0; const int CHILD_TEST_PASS = 1; const int CHILD_TEST_IGNORED = 2; -const int MSG_TYPE_MESSAGE = 0; // sizeof(Message) + Message -const int MSG_TYPE_PERF_TIME = 1; // perfTime + maxTime - int closeOutput() { int devnull; int retcode = -1; @@ -113,7 +110,6 @@ PipeWrapper::Status PipeWrapper::send(int code, std::string &message) std::ostringstream output; output << toBinaryString(code); - output << toBinaryString(MSG_TYPE_MESSAGE); output << toBinaryString(static_cast(message.size())); output << message; @@ -129,21 +125,16 @@ PipeWrapper::Status PipeWrapper::send(int code, std::string &message) return SUCCESS; } -PipeWrapper::Status PipeWrapper::sendTime(int code, - std::chrono::system_clock::duration time, - std::chrono::system_clock::duration timeMax) +PipeWrapper::Status PipeWrapper::sendPerformance(const ConstPerformanceResultPtr &performance) { + int foo = 0; if (m_pipefd[1] == PIPE_CLOSED) { return ERROR; } + if (!performance) + return writeHelp(&foo, sizeof(int)); - std::ostringstream output; - output << toBinaryString(code); - output << toBinaryString(MSG_TYPE_PERF_TIME); - output << toBinaryString(time); - output << toBinaryString(timeMax); - - std::string binary = output.str(); + std::string binary = performance->ToBinaryString(); int size = binary.size(); if ((writeHelp(&size, @@ -156,10 +147,8 @@ PipeWrapper::Status PipeWrapper::sendTime(int code, } PipeWrapper::Status PipeWrapper::receive(int &code, - int &msgType, std::string &data, - std::chrono::system_clock::duration &time, - std::chrono::system_clock::duration &timeMax, + PerformanceResultPtr &performance, time_t deadline) { if (m_pipefd[0] == PIPE_CLOSED) { @@ -185,24 +174,34 @@ PipeWrapper::Status PipeWrapper::receive(int &code, queue.AppendCopy(&buffer[0], size); queue.FlattenConsume(&code, sizeof(int)); - queue.FlattenConsume(&msgType, sizeof(int)); - - switch (msgType) { - case MSG_TYPE_MESSAGE: - queue.FlattenConsume(&size, sizeof(int)); - - buffer.resize(size); - - queue.FlattenConsume(&buffer[0], size); - data.assign(buffer.begin(), buffer.end()); - break; - case MSG_TYPE_PERF_TIME: - queue.FlattenConsume(&time, sizeof(std::chrono::system_clock::duration)); - queue.FlattenConsume(&timeMax, sizeof(std::chrono::system_clock::duration)); - break; - default: - return ERROR; + queue.FlattenConsume(&size, sizeof(int)); + + buffer.resize(size); + + queue.FlattenConsume(&buffer[0], size); + data.assign(buffer.begin(), buffer.end()); + + if (code != CHILD_TEST_PASS) + return SUCCESS; + + if ((ret = readHelp(&size, sizeof(int), deadline)) != SUCCESS) { + return ret; + } + + if (size == 0) { + performance = nullptr; + return SUCCESS; + } + + buffer.resize(size); + + if ((ret = readHelp(buffer.data(), size, deadline)) != SUCCESS) { + return ret; } + + queue.AppendCopy(buffer.data(), size); + + performance.reset(new PerformanceResult(queue)); } catch (DPL::BinaryQueue::Exception::Base &e) { return ERROR; } @@ -222,13 +221,6 @@ std::string PipeWrapper::toBinaryString(int data) return std::string(buffer, buffer + sizeof(int)); } -std::string PipeWrapper::toBinaryString(std::chrono::system_clock::duration data) -{ - char buffer[sizeof(std::chrono::system_clock::duration)]; - memcpy(buffer, &data, sizeof(std::chrono::system_clock::duration)); - return std::string(buffer, buffer + sizeof(std::chrono::system_clock::duration)); -} - void PipeWrapper::closeHelp(int desc) { if (m_pipefd[desc] != PIPE_CLOSED) { @@ -311,12 +303,10 @@ void RunChildProc(TestRunner::TestCase procChild) pipe.setUsage(PipeWrapper::READONLY); int code; - int msgType; - std::chrono::system_clock::duration time_m; - std::chrono::system_clock::duration timeMax_m; std::string message; + PerformanceResultPtr performance; - int pipeReturn = pipe.receive(code, msgType, message, time_m, timeMax_m, time(0) + 10); + int pipeReturn = pipe.receive(code, message, performance, time(0) + 10); if (pipeReturn != PipeWrapper::SUCCESS) { // Timeout or reading error pipe.closeAll(); @@ -334,11 +324,7 @@ void RunChildProc(TestRunner::TestCase procChild) throw TestRunner::TestFailed("Reading pipe error"); } - if (code == CHILD_TEST_PASS && msgType == MSG_TYPE_PERF_TIME) { - DPL::Test::TestRunnerSingleton::Instance().setCurrentTestCasePerformanceResult(true, - time_m, - timeMax_m); - } + TestRunnerSingleton::Instance().setCurrentTestCasePerformanceResult(performance); if (code == CHILD_TEST_FAIL) { throw TestRunner::TestFailed(message); @@ -353,9 +339,6 @@ void RunChildProc(TestRunner::TestCase procChild) int code = CHILD_TEST_PASS; std::string msg; - bool isPerformanceTest; - std::chrono::system_clock::duration time_m; - std::chrono::system_clock::duration timeMax_m; bool allowLogs = TestRunnerSingleton::Instance().GetAllowChildLogs(); @@ -383,16 +366,10 @@ void RunChildProc(TestRunner::TestCase procChild) closeOutput(); } - DPL::Test::TestRunnerSingleton::Instance().getCurrentTestCasePerformanceResult(isPerformanceTest, - time_m, - timeMax_m); - - if (code == CHILD_TEST_PASS && isPerformanceTest){ - pipe.sendTime(code, - time_m, - timeMax_m); - } else { - pipe.send(code, msg); + pipe.send(code, msg); + if (code == CHILD_TEST_PASS){ + pipe.sendPerformance(TestRunnerSingleton::Instance() \ + .getCurrentTestCasePerformanceResult()); } } }