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

org.nohope.test.stress.PooledMeasureProvider Maven / Gradle / Ivy

The newest version!
package org.nohope.test.stress;

import org.nohope.test.stress.functors.Get;
import org.nohope.test.stress.functors.Call;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.function.Function;

/**
 * @author Ketoth Xupack
 * @since 2013-12-29 18:23
 */
public final class PooledMeasureProvider extends AbstractMeasureData {
    private final Function poolLoader;
    private final Function statLoader;

    PooledMeasureProvider(final int threadId,
                          final int operationNumber,
                          final Function poolLoader,
                          final Function statLoader) {
        super(threadId, operationNumber);
        this.poolLoader = poolLoader;
        this.statLoader = statLoader;
    }

    @Override
    public long getThreadId() {
        return Thread.currentThread().getId();
    }

    public  Future getFuture(final String name, final Get getter) throws Exception {
        final ActionStatsAccumulator calc = statLoader.apply(name);
        return poolLoader.apply(name).submit(() -> calc.measure(getThreadId(), getter));
    }

    public Future callFuture(final String name, final Call call) throws Exception {
        final ActionStatsAccumulator calc = statLoader.apply(name);
        return poolLoader.apply(name).submit(() -> {
            try {
                calc.measure(getThreadId(), call);
            } catch (final InvocationException ignored) { // FIXME: already accounted?
            }
        });
    }

    @Override
    public  T get(final String name, final Get getter) throws Exception {
        return getFuture(name, getter).get();
    }

    @Override
    public void call(final String name, final Call getter) throws Exception {
        callFuture(name, getter).get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy