All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kie.perf.suite.SoakSuite Maven / Gradle / Ivy

There is a newer version: 7.74.1.Final
Show newest version
package org.kie.perf.suite;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.kie.perf.Executor;
import org.kie.perf.SharedMetricRegistry;
import org.kie.perf.TestConfig;
import org.kie.perf.TestConfig.Measure;
import org.kie.perf.metrics.CPUUsageHistogramSet;
import org.kie.perf.metrics.PerfRepoReporter;
import org.kie.perf.run.IRunType;
import org.kie.perf.scenario.IPerfTest;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.ScheduledReporter;
import com.codahale.metrics.Timer;

public class SoakSuite implements ITestSuite {

    protected int iterations;
    protected int expectedRate;
    protected IRunType run;

    public SoakSuite() {
        TestConfig tc = TestConfig.getInstance();
        iterations = tc.getIterations();
        expectedRate = tc.getExpectedRate();
        run = tc.getRunType().newInstance();
    }

    public void start() throws Exception {
        TestConfig tc = TestConfig.getInstance();
        Executor exec = Executor.getInstance();
        Set> scenarios = exec.getScenarios(tc.getTestPackage());
        List scenarioInstances = new ArrayList();
        for (Class c : scenarios) {
            IPerfTest scenario = c.newInstance();
            scenario.init();
            scenarioInstances.add(scenario);
        }
        boolean cpuusageEnabled = tc.getMeasure().contains(Measure.CPUUSAGE);
        CPUUsageHistogramSet cpuusage = null;
        if (cpuusageEnabled) {
            cpuusage = CPUUsageHistogramSet.getInstance(SoakSuite.class);
            cpuusage.start();
        }
        long timeForOneRun = Math.round(60.0 / expectedRate) * 1000; // expected rate is in runs per minute
        
        ScheduledReporter reporter = exec.getReporter();
        if (reporter instanceof PerfRepoReporter) {
            ((PerfRepoReporter) reporter).setScheduled(true);
        }
        reporter.start(tc.getPeriodicity(), TimeUnit.SECONDS);
        
        run.start(Integer.MAX_VALUE);
        while (!run.isEnd()) {
            long startTime = System.currentTimeMillis();
            for (IPerfTest scenario : scenarioInstances) {
                startScenario(scenario);
            }
            long endTime = System.currentTimeMillis();
            long pause = timeForOneRun - (endTime - startTime);
            if (pause > 0) {
                Thread.sleep(pause);
            }
        }
        reporter.stop();
        if (cpuusageEnabled) {
            cpuusage.stop();
        }
        for (IPerfTest scenario : scenarioInstances) {
            scenario.close();
        }
    }
    
    private void startScenario(IPerfTest scenario) {
        MetricRegistry metrics = SharedMetricRegistry.getInstance();

        Timer scenarioDuration = metrics.timer(MetricRegistry.name(scenario.getClass(), "scenario.single.duration"));
        scenario.init();
        scenario.initMetrics();
        Timer.Context context = scenarioDuration.time();
        try {
            scenario.execute();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        context.stop();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy