V1_0.java [src/java/d/ps] Revision:   Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package d.ps;

import csip.ModelDataService;
import csip.PayloadParameter;
import csip.PayloadResults;
import csip.ServiceException;
import csip.annotations.Description;
import csip.annotations.Name;
import csip.annotations.State;
import static csip.annotations.State.RELEASED;
import csip.annotations.VersionInfo;
import java.lang.management.ManagementFactory;
import javax.ws.rs.Path;
import org.apache.commons.math3.random.JDKRandomGenerator;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
@Name("ps-test")
@Description("Delay CPU for a number of milliseconds and report current CPU and memory stats.")
@VersionInfo("1.0")
@State(RELEASED)
@Path("d/ps/1.0")
public class V1_0 extends ModelDataService {

    private int timeDelay = 10000; // Default to 10 second delay
    private long currentMills;
    private int cores;
    private long maxMemory;
    private long totalMemory;
    private long stopMills;
    private int oneMB = (1000 * 1024);
    private JDKRandomGenerator random = new JDKRandomGenerator((int) (System.currentTimeMillis() / 1000));

    @Override
    protected void preProcess() throws ServiceException {
        PayloadParameter params = parameter();
        if (params.has("time_delay")) {
            timeDelay = params.getInt("time_delay");
        }

        currentMills = System.currentTimeMillis();
        stopMills = currentMills + timeDelay;
        cores = Runtime.getRuntime().availableProcessors();
        maxMemory = Runtime.getRuntime().maxMemory();
        totalMemory = Runtime.getRuntime().totalMemory();
    }

    @Override
    protected void doProcess() throws ServiceException {
        double tempa, tempb, tempc, tempd;

        while (System.currentTimeMillis() < stopMills) {
            tempa = random.nextDouble();
            tempb = random.nextDouble();
            tempc = random.nextGaussian() + 1;

            tempd = tempa * tempb / tempc;

            String testStr = "";
            int maxLength = random.nextInt(oneMB) + 1;
            for (int i = 0; i < maxLength; i++) {
                if (System.currentTimeMillis() >= stopMills) {
                    break;
                }
                testStr += random.nextInt(256);
            }
        }
    }

    @Override
    protected void postProcess() throws ServiceException {
        PayloadResults results = results();
        results.put("num_cores", cores);
        results.put("num_active_threads_on_JVM", Thread.activeCount());
        results.put("num_active_threads_on_CPU", ManagementFactory.getThreadMXBean().getThreadCount());
        results.put("maximum_memory", String.format("%.4f", ((double)maxMemory/oneMB)) + "Mb" );
        results.put("total_memory", String.format("%.4f", ((double)totalMemory/oneMB)) + "Mb");
        results.put("start_time_mills", currentMills);
        results.put("stop_time_mills", stopMills);
        results.put("post_process_time_mills", System.currentTimeMillis());

    }
}