ConnectionPoolDemo.java 4.6 KB
Newer Older
Z
change  
zyyang 已提交
1
package com.taosdata.example;
2

Z
change  
zyyang 已提交
3
import com.taosdata.example.common.InsertTask;
Z
change  
zyyang 已提交
4 5 6 7
import com.taosdata.example.pool.C3p0Builder;
import com.taosdata.example.pool.DbcpBuilder;
import com.taosdata.example.pool.DruidPoolBuilder;
import com.taosdata.example.pool.HikariCpBuilder;
8 9 10 11 12 13
import org.apache.log4j.Logger;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
Z
change  
zyyang 已提交
14 15 16
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
17 18 19 20

public class ConnectionPoolDemo {

    private static Logger logger = Logger.getLogger(DruidPoolBuilder.class);
21
    private static final String dbName = "pool_test";
22

Z
change  
zyyang 已提交
23
    private static long totalSize = 1_000_000l;
24 25 26 27 28
    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;
29 30
    private static String poolType = "hikari";

31

Z
change  
zyyang 已提交
32
    public static void main(String[] args) {
33 34 35 36 37
        String host = null;
        for (int i = 0; i < args.length; i++) {
            if ("-host".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                host = args[++i];
            }
Z
change  
zyyang 已提交
38 39 40
            if ("-recordNumber".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                totalSize = Long.parseLong(args[++i]);
            }
41 42 43
            if ("-poolType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
                poolType = args[++i];
            }
Z
change  
zyyang 已提交
44

45 46
        }
        if (host == null) {
Z
change  
zyyang 已提交
47
            System.out.println("Usage: java -jar XXX.jar -host <hostname> -recordNumber <totalNumber> -poolType <c3p0| dbcp| druid| hikari>");
48 49 50
            return;
        }

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        DataSource dataSource;
        switch (poolType) {
            case "c3p0":
                dataSource = C3p0Builder.getDataSource(host, poolSize);
                break;
            case "dbcp":
                dataSource = DbcpBuilder.getDataSource(host, poolSize);
                break;
            case "druid":
                dataSource = DruidPoolBuilder.getDataSource(host, poolSize);
                break;
            case "hikari":
            default:
                dataSource = HikariCpBuilder.getDataSource(host, poolSize);
                poolType = "hikari";
        }

        logger.info(">>>>>>>>>>>>>> connection pool Type: " + poolType);
69 70
        init(dataSource);

Z
change  
zyyang 已提交
71 72 73 74 75 76 77 78 79 80 81 82
//        try {
//            Connection connection = dataSource.getConnection();
//            Statement statement = connection.createStatement();
//            String sql = "insert into " + dbName + ".t_1 values('2020-01-01 00:00:00.000',12.12,111)";
//            int affectRows = statement.executeUpdate(sql);
//            System.out.println("affectRows >>> " + affectRows);
//            affectRows = statement.executeUpdate(sql);
//            System.out.println("affectRows >>> " + affectRows);
//            statement.close();
//            connection.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
Z
change  
zyyang 已提交
83
//        }
Z
change  
zyyang 已提交
84 85 86 87 88 89 90 91 92 93 94 95

        ExecutorService executor = Executors.newFixedThreadPool(threadCount);
        for (long i = 0; i < totalSize / batchSize / tableSize; i++) {
            executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
            // sleep few seconds
            try {
                TimeUnit.MILLISECONDS.sleep(sleep);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
Z
change  
zyyang 已提交
96

97 98 99 100 101 102 103 104 105 106 107
    }

    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) + ")");
            }
108
            logger.info(">>>>>>>>>>>>>>>>>>>>>>>>>>>> init finished.");
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        } 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();
        }
    }

}