// Path: src/main/java/com/pgasync/Connection.java
// public interface Connection extends QueryExecutor {
// 
//     CompletableFuture<PreparedStatement> prepareStatement(String sql, Oid... parametersTypes);
// 
//     /**
//      * The typical scenario of using notifications is as follows:
//      * - {@link #subscribe(String, Consumer)}
//      * - Some calls of @onNotification callback
//      * - {@link Listening#unlisten()}
//      *
//      * @param channel Channel name to listen to.
//      * @param onNotification Callback, thar is cinvoked every time notification arrives.
//      * @return CompletableFuture instance, completed when subscription will be registered at the backend.
//      */
//     CompletableFuture<Listening> subscribe(String channel, Consumer<String> onNotification);
// 
//     CompletableFuture<Void> close();
// }
// 
// Path: src/main/java/com/pgasync/Connectible.java
// public interface Connectible extends QueryExecutor {
// 
//     CompletableFuture<Connection> getConnection();
// 
//     CompletableFuture<Void> close();
// }
// 
// Path: src/test/java/com/github/pgasync/DatabaseRule.java
// static NettyConnectibleBuilder createPoolBuilder(int size) {
//     String db = getenv("PG_DATABASE");
//     String user = getenv("PG_USERNAME");
//     String pass = getenv("PG_PASSWORD");
// 
//     if (db == null && user == null && pass == null) {
//         return new EmbeddedConnectionPoolBuilder().maxConnections(size);
//     } else {
//         return new EmbeddedConnectionPoolBuilder()
//                 .database(envOrDefault("PG_DATABASE", "postgres"))
//                 .username(envOrDefault("PG_USERNAME", "postgres"))
//                 .password(envOrDefault("PG_PASSWORD", "postgres"))
//                 .ssl(true)
//                 .maxConnections(size);
//     }
// }


import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import com.pgasync.Connection;
import com.pgasync.Connectible;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static com.github.pgasync.DatabaseRule.createPoolBuilder;
        List<Object[]> testData = new ArrayList<>();
        for (int poolSize = 1; poolSize <= 8; poolSize *= 2) {
            for (int threads = 1; threads <= 8; threads *= 2) {
                testData.add(new Object[]{poolSize, threads});
            }
        }
        return testData;
    }

    private static final int batchSize = 1000;
    private static final int repeats = 5;
    private static SortedMap<Integer, SortedMap<Integer, Long>> simpleQueryResults = new TreeMap<>();
    private static SortedMap<Integer, SortedMap<Integer, Long>> preparedStatementResults = new TreeMap<>();

    private final int poolSize;
    private final int numThreads;
    private Connectible pool;

    public PerformanceTest(int poolSize, int numThreads) {
        this.poolSize = poolSize;
        this.numThreads = numThreads;
    }


    @Before
    public void setup() {
        pool = dbr.builder
                .password("async-pg")
                .maxConnections(poolSize)
                .pool(Executors.newFixedThreadPool(numThreads));
// Path: src/main/java/com/pgasync/Connection.java
// public interface Connection extends QueryExecutor {
// 
//     CompletableFuture<PreparedStatement> prepareStatement(String sql, Oid... parametersTypes);
// 
//     /**
//      * The typical scenario of using notifications is as follows:
//      * - {@link #subscribe(String, Consumer)}
//      * - Some calls of @onNotification callback
//      * - {@link Listening#unlisten()}
//      *
//      * @param channel Channel name to listen to.
//      * @param onNotification Callback, thar is cinvoked every time notification arrives.
//      * @return CompletableFuture instance, completed when subscription will be registered at the backend.
//      */
//     CompletableFuture<Listening> subscribe(String channel, Consumer<String> onNotification);
// 
//     CompletableFuture<Void> close();
// }
// 
// Path: src/main/java/com/pgasync/Connectible.java
// public interface Connectible extends QueryExecutor {
// 
//     CompletableFuture<Connection> getConnection();
// 
//     CompletableFuture<Void> close();
// }
// 
// Path: src/test/java/com/github/pgasync/DatabaseRule.java
// static NettyConnectibleBuilder createPoolBuilder(int size) {
//     String db = getenv("PG_DATABASE");
//     String user = getenv("PG_USERNAME");
//     String pass = getenv("PG_PASSWORD");
// 
//     if (db == null && user == null && pass == null) {
//         return new EmbeddedConnectionPoolBuilder().maxConnections(size);
//     } else {
//         return new EmbeddedConnectionPoolBuilder()
//                 .database(envOrDefault("PG_DATABASE", "postgres"))
//                 .username(envOrDefault("PG_USERNAME", "postgres"))
//                 .password(envOrDefault("PG_PASSWORD", "postgres"))
//                 .ssl(true)
//                 .maxConnections(size);
//     }
// }


// Path: src/test/java/com/github/pgasync/PerformanceTest.java
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
import com.pgasync.Connection;
import com.pgasync.Connectible;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import static com.github.pgasync.DatabaseRule.createPoolBuilder;

        List<Object[]> testData = new ArrayList<>();
        for (int poolSize = 1; poolSize <= 8; poolSize *= 2) {
            for (int threads = 1; threads <= 8; threads *= 2) {
                testData.add(new Object[]{poolSize, threads});
            }
        }
        return testData;
    }

    private static final int batchSize = 1000;
    private static final int repeats = 5;
    private static SortedMap<Integer, SortedMap<Integer, Long>> simpleQueryResults = new TreeMap<>();
    private static SortedMap<Integer, SortedMap<Integer, Long>> preparedStatementResults = new TreeMap<>();

    private final int poolSize;
    private final int numThreads;
    private Connectible pool;

    public PerformanceTest(int poolSize, int numThreads) {
        this.poolSize = poolSize;
        this.numThreads = numThreads;
    }


    @Before
    public void setup() {
        pool = dbr.builder
                .password("async-pg")
                .maxConnections(poolSize)
                .pool(Executors.newFixedThreadPool(numThreads));
        List<Connection> connections = IntStream.range(0, poolSize)