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