/* SchedulerService(Test) - component integration test for the scheduler Copyright (C) Lumiera.org 2023, Hermann Vosseler This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * *****************************************************/ /** @file scheduler-usage-test.cpp ** unit test \ref SchedulerService_test */ #include "lib/test/run.hpp" #include "activity-detector.hpp" #include "vault/gear/scheduler.hpp" #include "lib/time/timevalue.hpp" #include "lib/format-cout.hpp" #include "lib/test/microbenchmark.hpp" #include "lib/test/diagnostic-output.hpp"///////////////TODO //#include "lib/util.hpp" //#include #include using test::Test; //using std::move; //using util::isSameObject; namespace vault{ namespace gear { namespace test { // using lib::time::FrameRate; // using lib::time::Offset; using lib::time::Time; using std::this_thread::sleep_for; namespace { ////////////////////////////////////////////////////////////////////TICKET #1055 want to construct lumiera Time from std::chrono literals Time t100us = Time{FSecs{1, 10'000}}; Time t200us = t100us + t100us; Time t500us = t200us + t200us + t100us; Time t1ms = Time{1,0}; } /*************************************************************************//** * @test Scheduler component integration test: add and process dependent jobs. * @see SchedulerActivity_test * @see SchedulerInvocation_test * @see SchedulerCommutator_test * @see SchedulerLoadControl_test */ class SchedulerService_test : public Test { virtual void run (Arg) { simpleUsage(); invokeWorkFunction(); walkingDeadline(); } /** @test TODO demonstrate a simple usage scenario * @todo WIP 10/23 ✔ define ⟶ 🔁 implement */ void simpleUsage() { BlockFlowAlloc bFlow; EngineObserver watch; Scheduler{bFlow, watch}; } /** @test verify visible behaviour of the [work-pulling function](\ref Scheduler::getWork) * - use a rigged Activity probe to capture the schedule time on invocation * - additionally perform a timing measurement for invoking the work-function * - empty invocations cost ~5µs (-O3) rsp. ~25µs (debug) * - this implies we can show timing-delay effects in the millisecond range * - demonstrated behaviour * + an Activity already due will be dispatched immediately by post() * @todo WIP 10/23 🔁 define ⟶ implement */ void invokeWorkFunction() { BlockFlowAlloc bFlow; EngineObserver watch; Scheduler scheduler{bFlow, watch}; ActivityDetector detector; Activity& probe = detector.buildActivationProbe ("testProbe"); TimeVar start; int64_t delay_us; int64_t slip_us; activity::Proc res; auto post = [&](Time start) { // this test class is declared friend to get a backdoor to Scheduler internals... auto& schedCtx = Scheduler::ExecutionCtx::from(scheduler); schedCtx.post (start, &probe, schedCtx); }; auto pullWork = [&] { uint REPETITIONS = 1; delay_us = lib::test::benchmarkTime([&]{ res = scheduler.getWork(); }, REPETITIONS); slip_us = _raw(detector.invokeTime(probe)) - _raw(start); cout << "res:"<= start and wasClose (invoked, start); }; cout << "Scheduled right away..."< up-front delay"< 500); // this proves that there was a delay to wait for the next schedule CHECK (delay_us < 1000); pullWork(); // if we now re-invoke the work-Function as instructed... CHECK (wasInvoked(start)); // then the next schedule is already slightly overdue and immediately invoked CHECK (delay_us < 300); // Warning: this limit is dangerously tight CHECK (slip_us < 500); CHECK (activity::WAIT == res); // since there is nothing left in the Queue, we are instructed to sleep CHECK (scheduler.empty()); cout << "follow-up with some distance => follow-up delay"< 900); // yet this thread was afterwards kept in sleep to await the next one CHECK (activity::PASS == res); // instruction to re-invoke immediately CHECK (not scheduler.empty()); // since there is still work in the queue start += t1ms; // (just re-adjust the reference point to calculate slip_us) pullWork(); // re-invoke immediately as instructed CHECK (wasInvoked(start)); // Result: also the next Activity has been dispatched CHECK (delay_us < 300); // not much slip and delay CHECK (slip_us < 300); // Remark: here we often see cache-effects, since last EventLog call is way back, due to the long sleep CHECK (activity::WAIT == res); // since queue is empty, we are instructed to sleep CHECK (scheduler.empty()); cout << detector.showLog()<