<commit_before>package com.zaxxer.hikari.performance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

import javax.sql.DataSource;

import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.BoneCPDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class Benchmarks
{
    private static final int THREADS = Integer.getInteger("threads", 100);

    private DataSource ds;

    public static void main(String... args)
    {
        if (args.length == 0)
        {
            System.err.println("Start with one of: hikari, bone, c3p0, dbcp");
            System.exit(0);
        }

        Benchmarks benchmarks = new Benchmarks();
        if (args[0].equals("hikari"))
        {
            benchmarks.ds = benchmarks.setupHikari();
            System.out.println("Benchmarking HikariCP");
        }
        else if (args[0].equals("bone"))
        {
            benchmarks.ds = benchmarks.setupBone();
            System.out.println("Benchmarking BoneCP");
        }

        System.out.println("\nMixedBench");
        System.out.println(" Warming up JIT");
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();
        System.out.println(" MixedBench Final Timing Runs");
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();

        System.out.println("\nBoneBench");
        System.out.println(" Warming up JIT");
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
        System.out.println(" BoneBench Final Timing Run");
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
    }

    private DataSource setupHikari()
    {
        HikariConfig config = new HikariConfig();
        config.setAcquireIncrement(5);
        config.setMinimumPoolSize(20);
        config.setMaximumPoolSize(200);
        config.setConnectionTimeoutMs(5000);
        config.setJdbc4ConnectionTest(true);
        config.setDataSourceClassName("com.zaxxer.hikari.performance.StubDataSource");
        config.setProxyFactoryType(System.getProperty("testProxy", "javassist"));

        HikariDataSource ds = new HikariDataSource();
        ds.setConfiguration(config);
        return ds;
    }

    private DataSource setupBone()
    {
        try
        {
            Class.forName("com.zaxxer.hikari.performance.StubDriver");
        }
        catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }

        BoneCPConfig config = new BoneCPConfig();
        config.setAcquireIncrement(5);
        config.setMinConnectionsPerPartition(20);
        config.setMaxConnectionsPerPartition(200);
        config.setConnectionTimeoutInMs(5000);
        config.setConnectionTestStatement("VALUES 1");
        config.setCloseOpenStatements(true);
        config.setDisableConnectionTracking(true);
        config.setJdbcUrl("jdbc:stub");
        config.setUsername("nobody");
        config.setPassword("<PASSWORD>");

        BoneCPDataSource ds = new BoneCPDataSource(config);
        return ds;
    }

    private void startMixedBench()
    {
        CyclicBarrier barrier = new CyclicBarrier(THREADS);
        CountDownLatch latch = new CountDownLatch(THREADS);

        Measurable[] runners = new Measurable[THREADS];
        for (int i = 0; i < THREADS; i++)
        {
            runners[i] = new MixedRunner(barrier, latch);
        }

        runAndMeasure(runners, latch, "ms");
    }

    private void startSillyBench()
    {
        CyclicBarrier barrier = new CyclicBarrier(THREADS);
        CountDownLatch latch = new CountDownLatch(THREADS);

        Measurable[] runners = new Measurable[THREADS];
        for (int i = 0; i < THREADS; i++)
        {
            runners[i] = new SillyRunner(barrier, latch);
        }

        runAndMeasure(runners, latch, "ns");
    }

    private void runAndMeasure(Measurable[] runners, CountDownLatch latch, String timeUnit)
    {
        for (int i = 0; i < THREADS; i++)
        {
            Thread t = new Thread(runners[i]);
            t.start();
        }

        try
        {
            latch.await();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        int i = 0;
        long[] track = new long[THREADS];
        long min = Integer.MAX_VALUE, max = 0;
        for (Measurable runner : runners)
        {
            long elapsed = runner.getElapsed();
            track[i++] = elapsed;
            min = Math.min(min, elapsed);
            max = Math.max(max, elapsed);
        }

        long avg = min + ((max - min) / 2);
        Arrays.sort(track);
        long med = track[THREADS / 2];
        System.out.printf("  max=%d%4$s, avg=%d%4$s, med=%d%4$s\n", max, avg, med, timeUnit);
    }

    private class MixedRunner implements Measurable
    {
        private CyclicBarrier barrier;
        private CountDownLatch latch;<commit_msg>Fixed the average execution time calculation.<commit_after>package com.zaxxer.hikari.performance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

import javax.sql.DataSource;

import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.BoneCPDataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

public class Benchmarks
{
    private static final int THREADS = Integer.getInteger("threads", 100);

    private DataSource ds;

    public static void main(String... args)
    {
        if (args.length == 0)
        {
            System.err.println("Start with one of: hikari, bone, c3p0, dbcp");
            System.exit(0);
        }

        Benchmarks benchmarks = new Benchmarks();
        if (args[0].equals("hikari"))
        {
            benchmarks.ds = benchmarks.setupHikari();
            System.out.println("Benchmarking HikariCP");
        }
        else if (args[0].equals("bone"))
        {
            benchmarks.ds = benchmarks.setupBone();
            System.out.println("Benchmarking BoneCP");
        }

        System.out.println("\nMixedBench");
        System.out.println(" Warming up JIT");
        benchmarks.startMixedBench();
        System.out.println(" MixedBench Final Timing Runs");
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();
        benchmarks.startMixedBench();

        System.out.println("\nBoneBench");
        System.out.println(" Warming up JIT");
        benchmarks.startSillyBench();
        System.out.println(" BoneBench Final Timing Run");
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
        benchmarks.startSillyBench();
    }

    private DataSource setupHikari()
    {
        HikariConfig config = new HikariConfig();
        config.setAcquireIncrement(5);
        config.setMinimumPoolSize(20);
        config.setMaximumPoolSize(200);
        config.setConnectionTimeout(5000);
        config.setJdbc4ConnectionTest(true);
        config.setDataSourceClassName("com.zaxxer.hikari.performance.StubDataSource");
        config.setProxyFactoryType(System.getProperty("testProxy", "javassist"));

        HikariDataSource ds = new HikariDataSource();
        ds.setConfiguration(config);
        return ds;
    }

    private DataSource setupBone()
    {
        try
        {
            Class.forName("com.zaxxer.hikari.performance.StubDriver");
        }
        catch (ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }

        BoneCPConfig config = new BoneCPConfig();
        config.setAcquireIncrement(5);
        config.setMinConnectionsPerPartition(20);
        config.setMaxConnectionsPerPartition(200);
        config.setConnectionTimeoutInMs(5000);
        config.setConnectionTestStatement("VALUES 1");
        config.setCloseOpenStatements(true);
        config.setDisableConnectionTracking(true);
        config.setJdbcUrl("jdbc:stub");
        config.setUsername("nobody");
        config.setPassword("<PASSWORD>");

        BoneCPDataSource ds = new BoneCPDataSource(config);
        return ds;
    }

    private void startMixedBench()
    {
        CyclicBarrier barrier = new CyclicBarrier(THREADS);
        CountDownLatch latch = new CountDownLatch(THREADS);

        Measurable[] runners = new Measurable[THREADS];
        for (int i = 0; i < THREADS; i++)
        {
            runners[i] = new MixedRunner(barrier, latch);
        }

        runAndMeasure(runners, latch, "ms");
    }

    private void startSillyBench()
    {
        CyclicBarrier barrier = new CyclicBarrier(THREADS);
        CountDownLatch latch = new CountDownLatch(THREADS);

        Measurable[] runners = new Measurable[THREADS];
        for (int i = 0; i < THREADS; i++)
        {
            runners[i] = new SillyRunner(barrier, latch);
        }

        runAndMeasure(runners, latch, "ns");
    }

    private void runAndMeasure(Measurable[] runners, CountDownLatch latch, String timeUnit)
    {
        for (int i = 0; i < THREADS; i++)
        {
            Thread t = new Thread(runners[i]);
            t.start();
        }

        try
        {
            latch.await();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        int i = 0;
        long[] track = new long[THREADS];
        long max = 0, avg = 0, med = 0;
        for (Measurable runner : runners)
        {
            long elapsed = runner.getElapsed();
            track[i++] = elapsed;
            max = Math.max(max, elapsed);
            avg = (avg + elapsed) / 2;
        }

        Arrays.sort(track);
        med = track[THREADS / 2];
        System.out.printf("  max=%d%4$s, avg=%d%4$s, med=%d%4$s\n", max, avg, med, timeUnit);
    }

    private class MixedRunner implements Measurable
    {
        private CyclicBarrier barrier;
        private CountDownLatch latch;
{'fraction_non_alphanumeric': 0.11145661257409387, 'fraction_numerical': 0.008162472062967642, 'mean_word_length': 2.3448163795905104, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '2367483', 'n_tokens_mistral': 3190, 'n_tokens_neox': 3033, 'n_words': 592}