Skip to main content
added 148 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I am trying to simply measure my code execution time.

public class PerfMeasureUtil {

    private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
    private long startTime;
    private long duration;
    private static final EnvironmentProperties env = new EnvironmentProperties();

    /**
     * start counting the time
     *
     * @return self
     */
    public PerfMeasureUtil start() {
        if (env.isDev()) {// || env.isTest()) {
            LOG.log(Level.INFO, "====================================== measurement starts");
            this.startTime = System.currentTimeMillis();
        }
        return this;
    }

    /**
     * stops counting the time and returns the result
     *
     * @param methodName
     * @return
     */
    public long stop(String methodName) {
        if (env.isDev()) {// || env.isTest()) {
            this.duration = System.currentTimeMillis() - startTime;
            LOG.log(Level.INFO, "====================================== {0}", methodName);
            LOG.log(Level.INFO, "time spend: {0}ms", duration);
            LOG.log(Level.INFO, "======================================");
            return duration;
        }
        return -1;
    }
}

}

EnvironmentPropertiesEnvironmentProperties has a booleanboolean property based on the Maven build type. The measurement should be executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed:

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed.

And of course the pollution of code is pretty bad with it, but this way I have more control on the code amount measured. Maybe annotation could help?

I am trying to simply measure my code execution time.

public class PerfMeasureUtil {

private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();

/**
 * start counting the time
 *
 * @return self
 */
public PerfMeasureUtil start() {
    if (env.isDev()) {// || env.isTest()) {
        LOG.log(Level.INFO, "====================================== measurement starts");
        this.startTime = System.currentTimeMillis();
    }
    return this;
}

/**
 * stops counting the time and returns the result
 *
 * @param methodName
 * @return
 */
public long stop(String methodName) {
    if (env.isDev()) {// || env.isTest()) {
        this.duration = System.currentTimeMillis() - startTime;
        LOG.log(Level.INFO, "====================================== {0}", methodName);
        LOG.log(Level.INFO, "time spend: {0}ms", duration);
        LOG.log(Level.INFO, "======================================");
        return duration;
    }
    return -1;
}

}

EnvironmentProperties has a boolean property based on the Maven build type. The measurement should be executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed:

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed.

And of course the pollution of code is pretty bad with it, but this way I have more control on the code amount measured. Maybe annotation could help?

I am trying to simply measure my code execution time.

public class PerfMeasureUtil {

    private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
    private long startTime;
    private long duration;
    private static final EnvironmentProperties env = new EnvironmentProperties();

    /**
     * start counting the time
     *
     * @return self
     */
    public PerfMeasureUtil start() {
        if (env.isDev()) {// || env.isTest()) {
            LOG.log(Level.INFO, "====================================== measurement starts");
            this.startTime = System.currentTimeMillis();
        }
        return this;
    }

    /**
     * stops counting the time and returns the result
     *
     * @param methodName
     * @return
     */
    public long stop(String methodName) {
        if (env.isDev()) {// || env.isTest()) {
            this.duration = System.currentTimeMillis() - startTime;
            LOG.log(Level.INFO, "====================================== {0}", methodName);
            LOG.log(Level.INFO, "time spend: {0}ms", duration);
            LOG.log(Level.INFO, "======================================");
            return duration;
        }
        return -1;
    }
}

EnvironmentProperties has a boolean property based on the Maven build type. The measurement should be executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed:

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed.

And of course the pollution of code is pretty bad with it, but this way I have more control on the code amount measured. Maybe annotation could help?

deleted 38 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I am trying to simply measure my code execution time. I wrote this class

public class PerfMeasureUtil {

private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();

/**
 * start counting the time
 *
 * @return self
 */
public PerfMeasureUtil start() {
    if (env.isDev()) {// || env.isTest()) {
        LOG.log(Level.INFO, "====================================== measurement starts");
        this.startTime = System.currentTimeMillis();
    }
    return this;
}

/**
 * stops counting the time and returns the result
 *
 * @param methodName
 * @return
 */
public long stop(String methodName) {
    if (env.isDev()) {// || env.isTest()) {
        this.duration = System.currentTimeMillis() - startTime;
        LOG.log(Level.INFO, "====================================== {0}", methodName);
        LOG.log(Level.INFO, "time spend: {0}ms", duration);
        LOG.log(Level.INFO, "======================================");
        return duration;
    }
    return -1;
}

}

EnvironmentProperties has a boolean property based on maventhe Maven build type. The measurement should bybe executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed.:

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed. An

And of course the pollution of code is pretty bad whitwith it, but this way I have more control on the code amount measured. Maybe annotation could help? Thank you for any advices.

I am trying to simply measure my code execution time. I wrote this class

public class PerfMeasureUtil {

private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();

/**
 * start counting the time
 *
 * @return self
 */
public PerfMeasureUtil start() {
    if (env.isDev()) {// || env.isTest()) {
        LOG.log(Level.INFO, "====================================== measurement starts");
        this.startTime = System.currentTimeMillis();
    }
    return this;
}

/**
 * stops counting the time and returns the result
 *
 * @param methodName
 * @return
 */
public long stop(String methodName) {
    if (env.isDev()) {// || env.isTest()) {
        this.duration = System.currentTimeMillis() - startTime;
        LOG.log(Level.INFO, "====================================== {0}", methodName);
        LOG.log(Level.INFO, "time spend: {0}ms", duration);
        LOG.log(Level.INFO, "======================================");
        return duration;
    }
    return -1;
}

}

EnvironmentProperties has boolean property based on maven build type. The measurement should by executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed.

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed. An of course the pollution of code is pretty bad whit it, but this way I have more control on the code amount measured. Maybe annotation could help? Thank you for any advices.

I am trying to simply measure my code execution time.

public class PerfMeasureUtil {

private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();

/**
 * start counting the time
 *
 * @return self
 */
public PerfMeasureUtil start() {
    if (env.isDev()) {// || env.isTest()) {
        LOG.log(Level.INFO, "====================================== measurement starts");
        this.startTime = System.currentTimeMillis();
    }
    return this;
}

/**
 * stops counting the time and returns the result
 *
 * @param methodName
 * @return
 */
public long stop(String methodName) {
    if (env.isDev()) {// || env.isTest()) {
        this.duration = System.currentTimeMillis() - startTime;
        LOG.log(Level.INFO, "====================================== {0}", methodName);
        LOG.log(Level.INFO, "time spend: {0}ms", duration);
        LOG.log(Level.INFO, "======================================");
        return duration;
    }
    return -1;
}

}

EnvironmentProperties has a boolean property based on the Maven build type. The measurement should be executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed:

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed.

And of course the pollution of code is pretty bad with it, but this way I have more control on the code amount measured. Maybe annotation could help?

Source Link
Zavael
  • 171
  • 1
  • 10

Simple java code measuring

I am trying to simply measure my code execution time. I wrote this class

public class PerfMeasureUtil {

private static final Logger LOG = Logger.getLogger(PerfMeasureUtil.class.getName());
private long startTime;
private long duration;
private static final EnvironmentProperties env = new EnvironmentProperties();

/**
 * start counting the time
 *
 * @return self
 */
public PerfMeasureUtil start() {
    if (env.isDev()) {// || env.isTest()) {
        LOG.log(Level.INFO, "====================================== measurement starts");
        this.startTime = System.currentTimeMillis();
    }
    return this;
}

/**
 * stops counting the time and returns the result
 *
 * @param methodName
 * @return
 */
public long stop(String methodName) {
    if (env.isDev()) {// || env.isTest()) {
        this.duration = System.currentTimeMillis() - startTime;
        LOG.log(Level.INFO, "====================================== {0}", methodName);
        LOG.log(Level.INFO, "time spend: {0}ms", duration);
        LOG.log(Level.INFO, "======================================");
        return duration;
    }
    return -1;
}

}

EnvironmentProperties has boolean property based on maven build type. The measurement should by executed only on dev, or maybe testing environment, not in production. Now I call it like this everywhere, where it is needed.

    PerfMeasureUtil measureUtil = new PerfMeasureUtil().start();
    foo = readFooFromSomewhere();
    bar.applyFoo();
    printResult(bar);
    measureUtil.stop("FooBarApplication.aplyFooOnBar");

Is there something that could be made clearly better? I am not sure about the permanent if-check even on other built types when it is not needed. An of course the pollution of code is pretty bad whit it, but this way I have more control on the code amount measured. Maybe annotation could help? Thank you for any advices.