diff --git a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/TaosdemoApplication.java b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/TaosdemoApplication.java index db1b20527d88f5bca24ed5c6a3b477fa3a71bdf1..ec36705f6ea040f5de6e9504f313eabbf836e582 100644 --- a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/TaosdemoApplication.java +++ b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/TaosdemoApplication.java @@ -1,8 +1,13 @@ package com.taosdata.taosdemo; +import com.zaxxer.hikari.HikariDataSource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; + +import javax.sql.DataSource; @MapperScan(basePackages = {"com.taosdata.taosdemo.mapper"}) @SpringBootApplication @@ -10,6 +15,7 @@ public class TaosdemoApplication { public static void main(String[] args) { SpringApplication.run(TaosdemoApplication.class, args); + } } diff --git a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/components/TaosDemoCommandLineRunner.java b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/components/TaosDemoCommandLineRunner.java index d40bc1b5bfede04ec24b38cd97dd9b45763ea834..5d255d82d1aec8c5f3f3a1efd4dc29e8930048f0 100644 --- a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/components/TaosDemoCommandLineRunner.java +++ b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/components/TaosDemoCommandLineRunner.java @@ -15,10 +15,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; -import javax.sql.DataSource; import java.time.Duration; import java.time.Instant; import java.util.*; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; @Component @@ -33,17 +36,13 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { private SubTableService subTableService; private SuperTableMeta superTableMeta; -// private List subTableMetaList; -// private List subTableValueList; -// private List> dataList; - @Override public void run(String... args) throws Exception { // 读配置参数 JdbcTaosdemoConfig config = new JdbcTaosdemoConfig(args); boolean isHelp = Arrays.asList(args).contains("--help"); - if (isHelp || config.host == null || config.host.isEmpty()) { + if (isHelp) { JdbcTaosdemoConfig.printHelp(); System.exit(0); } @@ -100,8 +99,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { } private void insertTask(JdbcTaosdemoConfig config) { - long start = System.currentTimeMillis(); - long numOfTables = config.numOfTables; int numOfTablesPerSQL = config.numOfTablesPerSQL; long numOfRowsPerTable = config.numOfRowsPerTable; @@ -118,7 +115,11 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { if (numOfTables < numOfTablesPerSQL) numOfTablesPerSQL = (int) numOfTables; - long timeCost = 0; + + ExecutorService executors = Executors.newFixedThreadPool(config.numOfThreadsForInsert); + List> futureList = new ArrayList<>(); + long start = System.currentTimeMillis(); + long affectRows = 0; // row for (long rowCnt = 0; rowCnt < numOfRowsPerTable; ) { @@ -135,25 +136,22 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { } /***********************************************/ long startTime = config.startTime + rowCnt * config.timeGap; - -// for (int i = 0; i < tableSize; i++) { -// System.out.print(config.prefixOfTable + (tableCnt + i + 1) + ", tableSize: " + tableSize + ", rowSize: " + rowSize); -// System.out.println(", startTime: " + TimeStampUtil.longToDatetime(startTime) + ",timeGap: " + config.timeGap); -// } - // 生成数据 List data = SubTableValueGenerator.generate(superTableMeta, config.prefixOfTable, tableCnt, tableSize, rowSize, startTime, config.timeGap); -// List data = SubTableValueGenerator.generate(subTableMetaList, tableCnt, tableSize, rowSize, startTime, config.timeGap); // 乱序 if (config.order != 0) { SubTableValueGenerator.disrupt(data, config.rate, config.range); } // insert if (config.autoCreateTable) { - long a = System.currentTimeMillis(); - subTableService.insertAutoCreateTable(data, config.numOfThreadsForInsert, config.frequency); - long b = System.currentTimeMillis(); - timeCost += (b - a); + Future future = executors.submit(() -> subTableService.insertAutoCreateTable(data)); + try { + affectRows += future.get(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (ExecutionException e) { + e.printStackTrace(); + } } else { subTableService.insert(data, config.numOfThreadsForInsert, config.frequency); } @@ -162,8 +160,9 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { } rowCnt += rowSize; } - - + executors.shutdown(); + long end = System.currentTimeMillis(); + logger.info(">>> insert " + affectRows + " rows with time cost: " + (end - start) + "ms"); /*********************************************************************************/ // 批量插入,自动建表 // dataList.stream().forEach(subTableValues -> { @@ -178,8 +177,6 @@ public class TaosDemoCommandLineRunner implements CommandLineRunner { // subTableService.insert(subTableMetaList, config.numOfTables, config.tablePrefix, config.numOfThreadsForInsert, config.frequency); // } - long end = System.currentTimeMillis(); - logger.info(">>> total : " + (end - start) + " ms, insert : " + (timeCost) + " ms."); } private void prepareMetaData(JdbcTaosdemoConfig config) { diff --git a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/service/SubTableService.java b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/service/SubTableService.java index 1c4302e8baa5a53440bab555846162c96a7de77f..51e35898768fe16fe4185dbbb11a70356ad256a3 100644 --- a/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/service/SubTableService.java +++ b/tests/examples/JDBC/taosdemo/src/main/java/com/taosdata/taosdemo/service/SubTableService.java @@ -4,6 +4,7 @@ import com.taosdata.taosdemo.domain.*; import com.taosdata.taosdemo.mapper.SubTableMapper; import com.taosdata.taosdemo.service.data.SubTableMetaGenerator; import com.taosdata.taosdemo.utils.TimeStampUtil; +import com.zaxxer.hikari.pool.HikariPool; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -85,12 +86,17 @@ public class SubTableService extends AbstractService { } // 插入:多线程,多表, 自动建表 - public int insertAutoCreateTable(List subTableValues, int threadSize, int frequency) { - ExecutorService executor = Executors.newFixedThreadPool(threadSize); - Future future = executor.submit(() -> insertAutoCreateTable(subTableValues)); - executor.shutdown(); - return getAffectRows(future); - } +// public int insertAutoCreateTable(List subTableValues, int threadSize, int frequency) { +// long a = System.currentTimeMillis(); +// ExecutorService executor = Executors.newFixedThreadPool(threadSize); +// long b = System.currentTimeMillis(); +// Future future = executor.submit(() -> insertAutoCreateTable(subTableValues)); +// executor.shutdown(); +// int affectRows = getAffectRows(future); +// long c = System.currentTimeMillis(); +// logger.info(">>> total : " + (c - a) + " ms, thread: " + (b - a) + " ms, insert : " + (c - b) + " ms."); +// return affectRows; +// } // 插入:单表,insert into xxx values(),()... public int insert(SubTableValue subTableValue) { @@ -114,31 +120,18 @@ public class SubTableService extends AbstractService { // 插入:多表,自动建表, insert into xxx using XXX tags(...) values(),()... xxx using XXX tags(...) values(),()... public int insertAutoCreateTable(List subTableValues) { - Connection connection = null; - Statement statement = null; + int affectRows = 0; try { - connection = dataSource.getConnection(); -// String sql = sqlSessionFactory.getConfiguration() -// .getMappedStatement("com.taosdata.taosdemo.mapper.SubTableMapper.insertMultiTableMultiValuesUsingSuperTable") -// .getBoundSql(subTableValues) -// .getSql(); + Connection connection = dataSource.getConnection(); String sql = sql(subTableValues); - logger.info(">>> SQL : " + sql); - statement = connection.createStatement(); +// logger.info(">>> SQL : " + sql); + Statement statement = connection.createStatement(); affectRows = statement.executeUpdate(sql); + statement.close(); + connection.close(); } catch (SQLException e) { e.printStackTrace(); - } finally { - try { - if (statement != null) { - statement.close(); - } - if (connection != null) - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } } return affectRows; // return mapper.insertMultiTableMultiValuesUsingSuperTable(subTableValues); diff --git a/tests/examples/JDBC/taosdemo/src/main/resources/application.properties b/tests/examples/JDBC/taosdemo/src/main/resources/application.properties index 6280dfed165f54c666e60e8159e65feb731b9442..faea49c1ba04f6a7952f6345f23bacf625f58616 100644 --- a/tests/examples/JDBC/taosdemo/src/main/resources/application.properties +++ b/tests/examples/JDBC/taosdemo/src/main/resources/application.properties @@ -10,7 +10,9 @@ spring.datasource.password=taosdata #spring.datasource.driver-class-name=com.taosdata.jdbc.rs.RestfulDriver #spring.datasource.username=root #spring.datasource.password=taosdata -spring.datasource.hikari.maximum-pool-size=10 -spring.datasource.hikari.minimum-idle=10 +spring.datasource.hikari.maximum-pool-size=500 +spring.datasource.hikari.minimum-idle=500 spring.datasource.hikari.max-lifetime=0 -logging.level.com.taosdata.taosdemo.mapper=error \ No newline at end of file +logging.level.com.taosdata.taosdemo.mapper=error + +server.port=8888 \ No newline at end of file