Search in sources :

Example 1 with JobOrchestrator

use of net.nemerosa.ontrack.job.orchestrator.JobOrchestrator in project ontrack by nemerosa.

the class JobScatteringTest method scatteringInSameType.

@Test
public void scatteringInSameType() {
    // Scheduler
    DefaultJobScheduler scheduler = new DefaultJobScheduler(NOPJobDecorator.INSTANCE, new SynchronousScheduledExecutorService(), NOPJobListener.INSTANCE, false, true, 1.0);
    // Creates a list of jobs with a weak key
    List<TestJob> jobs = TestUtils.range(1, 100).stream().map(i -> TestJob.of(String.format("%d", i))).collect(Collectors.toList());
    // Orchestration of all those jobs every 6 hours
    Collection<JobOrchestratorSupplier> jobOrchestratorSupplier = Collections.singletonList(() -> jobs.stream().map(j -> JobRegistration.of(j).everyMinutes(6 * 60)));
    // Orchestrator
    JobOrchestrator orchestrator = new JobOrchestrator(scheduler, "Orchestrator", jobOrchestratorSupplier);
    // Scheduling the orchestrator (manual mode)
    scheduler.schedule(orchestrator, Schedule.NONE);
    // Launching the orchestrator (manually)
    orchestrator.orchestrate(JobRunListener.out());
    // Getting the actual schedules of the jobs
    List<Schedule> actualSchedules = jobs.stream().map(job -> scheduler.getJobStatus(job.getKey())).filter(Optional::isPresent).map(Optional::get).map(JobStatus::getActualSchedule).collect(Collectors.toList());
    List<Long> initialPeriods = actualSchedules.stream().map(Schedule::getInitialPeriod).collect(Collectors.toList());
    initialPeriods.forEach(l -> System.out.format("--> %d%n", l));
    // Checks that all jobs have been scheduled
    assertEquals("All jobs have been scheduled", jobs.size(), initialPeriods.size());
    // Checks that all schedules more or less different
    DescriptiveStatistics stats = new DescriptiveStatistics();
    initialPeriods.forEach(stats::addValue);
    // Gets the std deviation
    double standardDeviation = stats.getStandardDeviation();
    double max = stats.getMax();
    // Gets this in minutes (this was returned in ms)
    double stdDevMinutes = TimeUnit.MINUTES.convert((long) standardDeviation, TimeUnit.MILLISECONDS);
    double maxMinutes = TimeUnit.MINUTES.convert((long) max, TimeUnit.MILLISECONDS);
    // It must be >> 0
    assertTrue("Std deviation must be >> 0", stdDevMinutes > 60.0);
    System.out.println("Max = " + maxMinutes);
    assertTrue("Max is <= period", maxMinutes <= 6 * 60.0);
}
Also used : JobOrchestrator(net.nemerosa.ontrack.job.orchestrator.JobOrchestrator) MessageDigest(java.security.MessageDigest) Collection(java.util.Collection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TestUtils(net.nemerosa.ontrack.test.TestUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) JobOrchestratorSupplier(net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier) Optional(java.util.Optional) Assert(org.junit.Assert) Collections(java.util.Collections) net.nemerosa.ontrack.job(net.nemerosa.ontrack.job) JobOrchestrator(net.nemerosa.ontrack.job.orchestrator.JobOrchestrator) DescriptiveStatistics(org.apache.commons.math3.stat.descriptive.DescriptiveStatistics) Optional(java.util.Optional) JobOrchestratorSupplier(net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier) Test(org.junit.Test)

Aggregations

MessageDigest (java.security.MessageDigest)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 net.nemerosa.ontrack.job (net.nemerosa.ontrack.job)1 JobOrchestrator (net.nemerosa.ontrack.job.orchestrator.JobOrchestrator)1 JobOrchestratorSupplier (net.nemerosa.ontrack.job.orchestrator.JobOrchestratorSupplier)1 TestUtils (net.nemerosa.ontrack.test.TestUtils)1 DescriptiveStatistics (org.apache.commons.math3.stat.descriptive.DescriptiveStatistics)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1