ConnectionPoolDemo.java 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
package com.taosdata.demo;

import com.taosdata.demo.common.InsertTask;
import com.taosdata.demo.pool.DbcpBuilder;
import com.taosdata.demo.pool.DruidPoolBuilder;
import com.taosdata.demo.pool.HikariCpBuilder;
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConnectionPoolDemo {

    private static Logger logger = Logger.getLogger(DruidPoolBuilder.class);

    private static int batchSize = 10;
    private static int sleep = 1000;
    private static int poolSize = 50;
    private static int tableSize = 1000;
    private static int threadCount = 50;
    private static final String dbName = "pool_test";

    public static void main(String[] args) throws InterruptedException {
        String host = null;

        for (int i = 0; i < args.length; i++) {
            if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                host = args[++i];
            }
            if ("-batchSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                batchSize = Integer.parseInt(args[++i]);
            }
            if ("-sleep".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                sleep = Integer.parseInt(args[++i]);
            }
            if ("-poolSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                poolSize = Integer.parseInt(args[++i]);
            }
            if ("-tableSize".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                tableSize = Integer.parseInt(args[++i]);
            }
        }
        if (host == null) {
            System.out.println("Usage: java -jar XXX.jar " +
                    "-host <hostname> " +
                    "-batchSize <batchSize> " +
                    "-sleep <sleep> " +
                    "-poolSize <poolSize> " +
                    "-tableSize <tableSize>");
            return;
        }

//        DataSource dataSource = DbcpBuilder.getDataSource(host, poolSize);
//        DataSource dataSource = DruidPoolBuilder.getDataSource(host, poolSize);
        DataSource dataSource = HikariCpBuilder.getDataSource(host, poolSize);

        init(dataSource);

        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        while (true) {
            executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
            if (sleep > 0)
                TimeUnit.MILLISECONDS.sleep(sleep);
        }
    }

    private static void init(DataSource dataSource) {
        try (Connection conn = dataSource.getConnection()) {
            execute(conn, "drop database if exists " + dbName + "");
            execute(conn, "create database if not exists " + dbName + "");
            execute(conn, "use " + dbName + "");
            execute(conn, "create table weather(ts timestamp, temperature float, humidity int) tags(location nchar(64), groupId int)");
            for (int tb_ind = 1; tb_ind <= tableSize; tb_ind++) {
                execute(conn, "create table t_" + tb_ind + " using weather tags('beijing'," + (tb_ind + 1) + ")");
            }
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> init finished.");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void execute(Connection con, String sql) {
        try (Statement stmt = con.createStatement()) {
            stmt.executeUpdate(sql);
            logger.info("SQL >>> " + sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}