change
This commit is contained in:
parent
ee236c2088
commit
df6bc52856
|
@ -1,5 +1,6 @@
|
|||
package com.taosdata.demo;
|
||||
|
||||
import com.taosdata.demo.common.InsertTask;
|
||||
import com.taosdata.demo.pool.C3p0Builder;
|
||||
import com.taosdata.demo.pool.DbcpBuilder;
|
||||
import com.taosdata.demo.pool.DruidPoolBuilder;
|
||||
|
@ -10,12 +11,16 @@ 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 final String dbName = "pool_test";
|
||||
|
||||
private static long totalSize = 1_000_000l;
|
||||
private static int batchSize = 10;
|
||||
private static int sleep = 1000;
|
||||
private static int poolSize = 50;
|
||||
|
@ -24,36 +29,18 @@ public class ConnectionPoolDemo {
|
|||
private static String poolType = "hikari";
|
||||
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
public static void main(String[] args) {
|
||||
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 ("-poolType".equalsIgnoreCase(args[i]) && i < args.length - 1) {
|
||||
poolType = args[++i];
|
||||
}
|
||||
}
|
||||
if (host == null) {
|
||||
System.out.println("Usage: java -jar XXX.jar " +
|
||||
"-host <hostname> " +
|
||||
"-batchSize <batchSize> " +
|
||||
"-sleep <sleep> " +
|
||||
"-poolSize <poolSize> " +
|
||||
"-tableSize <tableSize>" +
|
||||
"-poolType <c3p0| dbcp| druid| hikari>");
|
||||
System.out.println("Usage: java -jar XXX.jar -host <hostname> -poolType <c3p0| dbcp| druid| hikari>");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -75,30 +62,34 @@ public class ConnectionPoolDemo {
|
|||
}
|
||||
|
||||
logger.info(">>>>>>>>>>>>>> connection pool Type: " + poolType);
|
||||
|
||||
init(dataSource);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
// ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
// while (true) {
|
||||
// executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
|
||||
// if (sleep > 0)
|
||||
// TimeUnit.MILLISECONDS.sleep(sleep);
|
||||
// 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();
|
||||
// }
|
||||
|
||||
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
|
||||
for (long i = 0; i < totalSize / batchSize; i++) {
|
||||
executor.execute(new InsertTask(dataSource, dbName, tableSize, batchSize));
|
||||
// sleep few seconds
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(sleep);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
executor.shutdown();
|
||||
|
||||
}
|
||||
|
||||
private static void init(DataSource dataSource) {
|
||||
|
|
|
@ -26,52 +26,21 @@ public class InsertTask implements Runnable {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
Connection conn = null;
|
||||
Statement stmt = null;
|
||||
int affectedRows = 0;
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
try {
|
||||
conn = ds.getConnection();
|
||||
stmt = conn.createStatement();
|
||||
|
||||
try (Connection conn = ds.getConnection(); Statement stmt = conn.createStatement()) {
|
||||
for (int tb_index = 1; tb_index <= tableSize; tb_index++) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("insert into ");
|
||||
sb.append(dbName);
|
||||
sb.append(".t_");
|
||||
sb.append(tb_index);
|
||||
sb.append("(ts, temperature, humidity) values ");
|
||||
sb.append("insert into ").append(dbName).append(".t_").append(tb_index).append("(ts, temperature, humidity) values ");
|
||||
for (int i = 0; i < batchSize; i++) {
|
||||
sb.append("(");
|
||||
sb.append(start + i);
|
||||
sb.append(", ");
|
||||
sb.append(random.nextFloat() * 30);
|
||||
sb.append(", ");
|
||||
sb.append(random.nextInt(70));
|
||||
sb.append(") ");
|
||||
sb.append("(").append(start + i).append(", ").append(random.nextFloat() * 30).append(", ").append(random.nextInt(70)).append(") ");
|
||||
}
|
||||
logger.info("SQL >>> " + sb.toString());
|
||||
affectedRows += stmt.executeUpdate(sb.toString());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
logger.info(">>> affectedRows:" + affectedRows + " TimeCost:" + (System.currentTimeMillis() - start) + " ms");
|
||||
}
|
||||
logger.info(">>> affectedRows:" + affectedRows + " TimeCost:" + (System.currentTimeMillis() - start) + " ms");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue