<commit_before>/*
   Copyright 2008-2009 <NAME>

   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 net.nanopool.benchmark;

import biz.source_code.miniConnectionPoolManager.MiniConnectionPoolManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.WrapperConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import net.nanopool.AbstractDataSource;
import net.nanopool.Configuration;
import net.nanopool.NanoPoolDataSource;
import net.nanopool.contention.DefaultContentionHandler;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.junit.Test;

public class Benchmark {
    private static final boolean PRE_WARM_POOLS = true;
    private static PoolFactory poolFactory;

    @Test
    public void executeBenchmark() throws InterruptedException {
        main(null);
    }
    
    public static void main(String[] args) throws InterruptedException {
        System.out.println("--------------------------------");
        System.out.println("  thr = thread");
        System.out.println("  con = connection");
        System.out.println("  cyc = connect-query-close cycle");
        System.out.println("  tot = total");
        System.out.println("  sec = a second");
        System.out.println("--------------------------------");

        System.out.println("### Testing NanoPool");
        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                return new NanoPoolDataSource(cpds, buildConfig(size, ttl));
            }

            public void closePool(DataSource pool) {
                if (pool instanceof NanoPoolDataSource) {
                    List<SQLException> sqles = ((NanoPoolDataSource)pool).shutdown();
                    for (SQLException sqle : sqles) {
                        sqle.printStackTrace();
                    }
                }
            }
        };
        runTestSet();

        System.out.println("### Testing C3P0");
        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                ComboPooledDataSource cmds = new ComboPooledDataSource();
                try {
                    WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource();
                    cmds.setConnectionPoolDataSource(cpds);
                    cmds.setMaxPoolSize(size);
                    return cmds;
                } catch (PropertyVetoException ex) {
                    throw new RuntimeException(ex);
                }
            }

            public void closePool(DataSource pool) {
                try {
                    DataSources.destroy(pool);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        };
        //runTestSet();

        System.out.println("### Testing Commons DBCP");
        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                SharedPoolDataSource spds = new SharedPoolDataSource();
                spds.setConnectionPoolDataSource(cpds);
                spds.setMaxActive(size);
                return spds;
            }

            public void closePool(DataSource pool) {
                SharedPoolDataSource spds = (SharedPoolDataSource)pool;
                try {
                    spds.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        runTestSet();

        System.out.println("### Testing MiniConnectionPoolManager");
        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                final MiniConnectionPoolManager mcpm = new MiniConnectionPoolManager(cpds, size);
                return new AbstractDataSource() {
                    public Connection getConnection() throws SQLException {
                        return mcpm.getConnection();
                    }

                    @Override
                    public List<SQLException> shutdown() {
                        List<SQLException> sqles = new ArrayList<SQLException>();
                        try {
                            mcpm.dispose();
                        } catch (SQLException ex) {
                            sqles.add(ex);
                        }
                        return sqles;
                    }
                };
            }

            public void closePool(DataSource pool) {
                AbstractDataSource ads = (AbstractDataSource)pool;
                List<SQLException> sqles = ads.shutdown();
                for (SQLException sqle : sqles) {
                    sqle.printStackTrace();
                }
            }
        };
        runTestSet();
    }
    
    private static void runTestSet() throws InterruptedException {
        try {
            benchmark(50, 20);
            benchmark(2, 1);
        System.out.println("------Warmup's over-------------");<commit_msg>Added a "pools" sys property to the benchmark so you can control wich pools to test.<commit_after>/*
   Copyright 2008-2009 <NAME>

   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 net.nanopool.benchmark;

import biz.source_code.miniConnectionPoolManager.MiniConnectionPoolManager;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.mchange.v2.c3p0.WrapperConnectionPoolDataSource;
import com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import net.nanopool.AbstractDataSource;
import net.nanopool.Configuration;
import net.nanopool.NanoPoolDataSource;
import net.nanopool.contention.DefaultContentionHandler;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.junit.Test;

public class Benchmark {
    private static final boolean PRE_WARM_POOLS = true;
    private static PoolFactory poolFactory;

    @Test
    public void executeBenchmark() throws InterruptedException {
        main(null);
    }
    
    public static void main(String[] args) throws InterruptedException {
        String pools =
                "," + System.getProperty("pools", "np,c3p0,dbcp,mcpm") + ",";

        System.out.println("--------------------------------");
        System.out.println("  thr = thread");
        System.out.println("  con = connection");
        System.out.println("  cyc = connect-query-close cycle");
        System.out.println("  tot = total");
        System.out.println("  sec = a second");
        System.out.println("--------------------------------");

        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                return new NanoPoolDataSource(cpds, buildConfig(size, ttl));
            }

            public void closePool(DataSource pool) {
                if (pool instanceof NanoPoolDataSource) {
                    List<SQLException> sqles = ((NanoPoolDataSource)pool).shutdown();
                    for (SQLException sqle : sqles) {
                        sqle.printStackTrace();
                    }
                }
            }
        };
        if (pools.contains(",np,")) {
            System.out.println("### Testing NanoPool");
            runTestSet();
        }

        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                ComboPooledDataSource cmds = new ComboPooledDataSource();
                try {
                    WrapperConnectionPoolDataSource wcpds = new WrapperConnectionPoolDataSource();
                    cmds.setConnectionPoolDataSource(cpds);
                    cmds.setMaxPoolSize(size);
                    return cmds;
                } catch (PropertyVetoException ex) {
                    throw new RuntimeException(ex);
                }
            }

            public void closePool(DataSource pool) {
                try {
                    DataSources.destroy(pool);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
        };
        if (pools.contains(",c3p0,")) {
            System.out.println("### Testing C3P0");
            //runTestSet();
        }

        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                SharedPoolDataSource spds = new SharedPoolDataSource();
                spds.setConnectionPoolDataSource(cpds);
                spds.setMaxActive(size);
                return spds;
            }

            public void closePool(DataSource pool) {
                SharedPoolDataSource spds = (SharedPoolDataSource)pool;
                try {
                    spds.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        };
        if (pools.contains(",dbcp,")) {
            System.out.println("### Testing Commons DBCP");
            runTestSet();
        }

        poolFactory = new PoolFactory() {
            public DataSource buildPool(ConnectionPoolDataSource cpds, int size, long ttl) {
                final MiniConnectionPoolManager mcpm = new MiniConnectionPoolManager(cpds, size);
                return new AbstractDataSource() {
                    public Connection getConnection() throws SQLException {
                        return mcpm.getConnection();
                    }

                    @Override
                    public List<SQLException> shutdown() {
                        List<SQLException> sqles = new ArrayList<SQLException>();
                        try {
                            mcpm.dispose();
                        } catch (SQLException ex) {
                            sqles.add(ex);
                        }
                        return sqles;
                    }
                };
            }

            public void closePool(DataSource pool) {
                AbstractDataSource ads = (AbstractDataSource)pool;
                List<SQLException> sqles = ads.shutdown();
                for (SQLException sqle : sqles) {
                    sqle.printStackTrace();
                }
            }
        };
        if (pools.contains(",mcpm,")) {
            System.out.println("### Testing MiniConnectionPoolManager");
            runTestSet();
        }
    }
    
    private static void runTestSet() throws InterruptedException {
        try {
            benchmark(50, 20);
            benchmark(2, 1);
        System.out.println("------Warmup's over-------------");
{'fraction_non_alphanumeric': 0.09793855145723086, 'fraction_numerical': 0.005054892978437722, 'mean_word_length': 1.8263392857142857, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1557574', 'n_tokens_mistral': 3167, 'n_tokens_neox': 2992, 'n_words': 758}