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

com.github.jlangch.venice.impl.specialforms.util.Benchmark Maven / Gradle / Ivy

/*   __    __         _
 *   \ \  / /__ _ __ (_) ___ ___
 *    \ \/ / _ \ '_ \| |/ __/ _ \
 *     \  /  __/ | | | | (_|  __/
 *      \/ \___|_| |_|_|\___\___|
 *
 *
 * Copyright 2017-2024 Venice
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.jlangch.venice.impl.specialforms.util;

import java.util.ArrayList;
import java.util.List;

import com.github.jlangch.venice.impl.IFormEvaluator;
import com.github.jlangch.venice.impl.InterruptChecker;
import com.github.jlangch.venice.impl.env.Env;
import com.github.jlangch.venice.impl.thread.ThreadContext;
import com.github.jlangch.venice.impl.types.Constants;
import com.github.jlangch.venice.impl.types.VncFunction;
import com.github.jlangch.venice.impl.types.VncJust;
import com.github.jlangch.venice.impl.types.VncKeyword;
import com.github.jlangch.venice.impl.types.VncLong;
import com.github.jlangch.venice.impl.types.VncString;
import com.github.jlangch.venice.impl.types.VncVal;
import com.github.jlangch.venice.impl.types.collections.VncList;


public class Benchmark {

    public static VncList benchmark(
            final long warmUpIterations,
            final long gcRuns,
            final long iterations,
            final VncVal expr,
            final VncFunction statusFn,
            final Env env,
            final IFormEvaluator evaluator
    ) {
        try {
            // warmup
            statusFn.applyOf(new VncString("Warmup..."));
            final VncList warmupSamples = samples(warmUpIterations, expr, env, evaluator);
            storeToHole(warmupSamples);

            long batchSize = 1;

//            final VncList sortedUp = (VncList)CoreFunctions.sort.applyOf(warmupSamples);
//            final VncList samples = (VncList)TransducerFunctions.take.applyOf(
//                                                new VncLong(Math.min(100, sortedUp.size())),
//                                                sortedUp);
//            final long warmupElapsed = ((VncNumber)MathFunctions.mean.apply(samples)).toJavaLong();
//            if (warmupElapsed < 1000) batchSize = 1000;
//            else if (warmupElapsed < 10000) batchSize = 100;

            // GC
            statusFn.applyOf(new VncString("GC..."));
            runGCs(gcRuns);
            sleep(1000L);

            // measure overhead
            final long overheadPerSample = measureSampleOverhead();
            runGCs(1);

            // sampling
            statusFn.applyOf(new VncString("Sampling..."));
            final VncList iterationSamples = samples(iterations, batchSize, overheadPerSample, expr, env, evaluator);
            return iterationSamples;
        }
        finally {
            ThreadContext.removeValue(benchVal);
        }
    }

    private static VncList samples(
            final long iterations,
            final VncVal expr,
            final Env env,
            final IFormEvaluator evaluator
    ) {
        return samples(iterations, 1L, 0L, expr, env, evaluator);
    }

    private static VncList samples(
            final long iterations,
            final long batchSize,
            final long overheadPerSample,
            final VncVal expr,
            final Env env,
            final IFormEvaluator evaluator
    ) {
        if (batchSize == 1) {
            final List elapsed = new ArrayList<>();
            for(int ii=0; ii elapsed = new ArrayList<>();
            final long batchedIterations = iterations/batchSize + 1L;
            for(int ii=0; ii




© 2015 - 2024 Weber Informatics LLC | Privacy Policy