Skip @Param values

Andrii Rodionov andrii.rodionov at gmail.com
Mon Jul 21 14:22:21 UTC 2014


Hi Aleksey,

Is it possible to specify that methods, which are not using @Param values,
should execute only once? Maybe some annotation like @SkipParam above the
method?

Below is a code snippet (part of example of integral calculation using
different approaches).

Thanks,
Andrii Rodionov
------------

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class IntegralCalcRunner1 {

    double step = 0.001;
    double start = 0.0;
    double end = 10_000.0;

    Function<Double, Double> func = x -> sin(x) * sin(x) + cos(x) * cos(x);
    Function<Double, Double> calcFunc = x -> step * x;
    Function<Double, Double> sqFunc = func.andThen(calcFunc);

    DoubleUnaryOperator funcD = x -> sin(x) * sin(x) + cos(x) * cos(x);
    DoubleUnaryOperator calcFuncD = x -> step * x;
    DoubleUnaryOperator sqFuncDouble = funcD.andThen(calcFuncD);


    @Param({"1", "2","4", "8", "16", "32"})
    private int threads;

    @Benchmark
    public double Simple_Threads() throws InterruptedException,
ExecutionException {
        return withSimpleThreads(sqFunc, start, end, step, threads);
    }

    @Benchmark
    public double Usual_Stream() {
        return withUsualStream(sqFunc, start, end, step);
    }

    @Benchmark
    public double Double_Stream() {
        return withDoubleStream(sqFuncDouble, start, end, step);
    }

    @Benchmark
    public double Double_Stream_with_Range_Parallel()  {
        return withDoubleStreamRangeParallel(sqFuncDouble, start, end,
step);
    }

    protected static double withSimpleThreads(Function<Double, Double>
sqFunc, double start, double end, double step, int chunks) throws
InterruptedException, ExecutionException {

        IntegralCalcThreads calc = new IntegralCalcThreads(sqFunc, start,
end, step);
        double sum = calc.calculate(chunks);

        return sum;
    }

    protected static double withUsualStream(Function<Double, Double>
sqFunc, double start, double end, double step) {

        double sum = Stream.
                iterate(0.0, s -> s + step).limit((int) ((end - start) /
step)).
                //parallel().
                map(sqFunc).
                reduce(0.0, Double::sum);
        return sum;
    }

    protected static double withDoubleStream(DoubleUnaryOperator
sqFuncDouble, double start, double end, double step) {

        double sum = DoubleStream.
                iterate(0.0, s -> s + step).limit((long) ((end - start) /
step)).
                //parallel().
                map(sqFuncDouble).
                sum();
        return sum;
    }

    protected static double
withDoubleStreamRangeParallel(DoubleUnaryOperator sqFuncDouble, double
start, double end, double step) {

        double sum = LongStream.
                range(0, (long) ((end - start) / step)).
                parallel().
                mapToDouble(i -> start + step * i).
                map(sqFuncDouble).
                sum();
        return sum;
    }

    public static void main(String[] args) throws RunnerException {

        System.out.println("CPU: " +
Runtime.getRuntime().availableProcessors());
        org.openjdk.jmh.runner.options.Options opt = new OptionsBuilder()
                .include(".*" + IntegralCalcRunner1.class.getSimpleName() +
".*")
                .warmupIterations(5)
                .measurementIterations(5)
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}


More information about the jmh-dev mailing list