<commit_before>package ch.ethz.globis.distindex.phtree;


import ch.ethz.globis.pht.PhTree;
import ch.ethz.globis.pht.PhTreeHelper;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@BenchmarkMode({Mode.Throughput})
@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 15, time = 10, timeUnit = TimeUnit.MILLISECONDS)
public class ConcurrencyBenchmark {

    @Benchmark
    public Object deleteRandom_noConcurrent(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_COW(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = true;

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_BigLock(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;
        try {
            state.l.lock();
            return delete(state);
        } finally {
            state.l.unlock();
        }
    }

    private boolean delete(BenchmarkState state) {
        PhTree tree = state.getTree();

        int dim = tree.getDIM();

        long[] key = createRandomKey(dim);
        return state.tree.delete(key);
    }

    @Benchmark
    public Object containsRandom_noConcurrent(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_COW(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = true;

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_BigLock(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;
        try {
            state.l.lock();
            return contains(state);
        } finally {
            state.l.unlock();
        }
    }

    private boolean contains(BenchmarkState state) {
        int dim = state.getTree().getDIM();

        long[] key = createRandomKey(dim);
        return state.getTree().contains(key);
    }

    @Benchmark
    public Object putRandom_NoConcurrent(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;

        return put(state);
    }

    @Benchmark
    public Object putRandom_COW(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = true;

        return put(state);
    }

    @Benchmark
    public Object putRandom_BigLock(BenchmarkState state) {
        PhTreeHelper.CONCURRENT = false;
        try {
            state.l.lock();
            return put(state);
        } finally {
            state.l.unlock();
        }
    }

    private Object put(BenchmarkState state) {
        int dim = state.getTree().getDIM();

        long[] key = createRandomKey(dim);
        return state.getTree().insert(key);
    }

    private long[] createRandomKey(int dim) {
        long[] key = new long[dim];
        Random random = new Random();
        for (int i = 0; i < dim; i++) {
            key[i] = random.nextInt();
        }
        return key;
    }

    @State(Scope.Benchmark)
    public static class BenchmarkState {

        PhTree tree;
        ReentrantLock l = new ReentrantLock(true);

        @Setup
        public void initTree() {
            tree = PhTree.create(2, 64);
        }

        public PhTree getTree() {
            return tree;
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" + ConcurrencyBenchmark.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}<commit_msg>#1: Index distribution Updated concurrency benchmarking code with the new PhOperations API. Currently, measuring no concurrency, big lock, hand-over-hand locking, optimistic locking and copy-on-write.<commit_after>package ch.ethz.globis.distindex.phtree;


import ch.ethz.globis.pht.v5.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Arrays;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

@BenchmarkMode({Mode.Throughput})
@Warmup(iterations = 5, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 15, time = 10, timeUnit = TimeUnit.MILLISECONDS)
@Threads(2)
public class ConcurrencyBenchmark {

    @Benchmark
    public Object deleteRandom_noConcurrent(BenchmarkState state) {
        state.setNoConcurrency();

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_COW(BenchmarkState state) {
        state.setCopyOnWrite();

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_HandOverHand(BenchmarkState state) {
        state.setHandOverHandLocking();

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_OL(BenchmarkState state) {
        state.setOptimisticLocking();

        return delete(state);
    }

    @Benchmark
    public Object deleteRandom_BigLock(BenchmarkState state) {
        state.setNoConcurrency();
        try {
            state.l.lock();
            return delete(state);
        } finally {
            state.l.unlock();
        }
    }

    private String delete(BenchmarkState state) {
        PhTree5<String> tree = state.getTree();

        int dim = tree.getDIM();

        long[] key = createRandomKey(dim);
        return state.tree.remove(key);
    }

    @Benchmark
    public Object containsRandom_noConcurrent(BenchmarkState state) {
        state.setNoConcurrency();

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_COW(BenchmarkState state) {
        state.setCopyOnWrite();

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_HandOverHand(BenchmarkState state) {
        state.setHandOverHandLocking();

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_OL(BenchmarkState state) {
        state.setOptimisticLocking();

        return contains(state);
    }

    @Benchmark
    public Object containsRandom_BigLock(BenchmarkState state) {
        state.setNoConcurrency();
        synchronized (state.lock) {
            return contains(state);
        }
    }

    private boolean contains(BenchmarkState state) {
        int dim = state.getTree().getDIM();

        long[] key = createRandomKey(dim);
        return state.getTree().contains(key);
    }

    @Benchmark
    public Object putRandom_NoConcurrent(BenchmarkState state) {
        state.setNoConcurrency();

        return put(state);
    }

    @Benchmark
    public Object putRandom_COW(BenchmarkState state) {
        state.setCopyOnWrite();

        return put(state);
    }

    @Benchmark
    public Object putRandom_HandOverHand(BenchmarkState state) {
        state.setHandOverHandLocking();

        return put(state);
    }

    @Benchmark
    public Object putRandom_OL(BenchmarkState state) {
        state.setOptimisticLocking();

        return put(state);
    }

    @Benchmark
    public Object putRandom_BigLock(BenchmarkState state) {
        state.setNoConcurrency();

        try {
            state.l.lock();
            return put(state);
        } finally {
            state.l.unlock();
        }
    }

    private Object put(BenchmarkState state) {
        int dim = state.getTree().getDIM();

        long[] key = createRandomKey(dim);
        return state.getTree().put(key, Arrays.toString(key));
    }

    private long[] createRandomKey(int dim) {
        long[] key = new long[dim];
        Random random = new Random();
        for (int i = 0; i < dim; i++) {
            key[i] = random.nextInt();
        }
        return key;
    }

    @State(Scope.Benchmark)
    public static class BenchmarkState {

        PhTree5<String> tree;
        final Object lock = new Object();
        final ReentrantLock l = new ReentrantLock();

        private PhOperations phOperationsOL;
        private PhOperations phOperationsHoH;
        private PhOperations phOperationsCOW;
        private PhOperations phOperationsSimple;

        @Setup
        public void initTree() {
            tree = new PhTree5<String>(2, 64);
            phOperationsOL = new PhOperationsOL(tree);
            phOperationsCOW = new PhOperationsCOW(tree);
            phOperationsHoH = new PhOperationsHandOverHand(tree);
            phOperationsSimple = new PhOperationsSimple(tree);
        }

        public PhTree5<String> getTree() {
            return tree;
        }

        public void setOptimisticLocking() {
            tree.setOperations(phOperationsOL);
        }

        public void setHandOverHandLocking() {
            tree.setOperations(phOperationsHoH);
        }

        public void setCopyOnWrite() {
            tree.setOperations(phOperationsCOW);
        }

        public void setNoConcurrency() {
            tree.setOperations(phOperationsSimple);
        }
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" + ConcurrencyBenchmark.class.getSimpleName())
                .forks(1)
                .build();
        new Runner(opt).run();
    }
}
{'fraction_non_alphanumeric': 0.09819013938007073, 'fraction_numerical': 0.003224464322862492, 'mean_word_length': 2.3975265017667846, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7324337', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2793, 'n_words': 575}