提交 e6005b1e 编写于 作者: Z zyyang

change

上级 a2e62fd3
...@@ -13,17 +13,17 @@ import java.util.concurrent.TimeUnit; ...@@ -13,17 +13,17 @@ import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
public class BatchInsertTest extends BaseTest { public class BatchInsertTest {
static Connection connection = null; private Connection connection;
static Statement statement = null;
static String dbName = "test"; private static String dbName = "test";
static String stbName = "meters"; private static String stbName = "meters";
static String host = "localhost"; private static String host = "127.0.0.1";
static int numOfTables = 30; private static int numOfTables = 30;
final static int numOfRecordsPerTable = 1000; private static int numOfRecordsPerTable = 1000;
static long ts = 1496732686000l; private static long ts = 1496732686000l;
final static String tablePrefix = "t"; private static String tablePrefix = "t";
@Before @Before
public void createDatabase() throws SQLException { public void createDatabase() throws SQLException {
...@@ -32,65 +32,58 @@ public class BatchInsertTest extends BaseTest { ...@@ -32,65 +32,58 @@ public class BatchInsertTest extends BaseTest {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
return; return;
} }
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty(TSDBDriver.PROPERTY_KEY_HOST, host);
properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8"); properties.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties); connection = DriverManager.getConnection("jdbc:TAOS://" + host + ":0/", properties);
statement = connection.createStatement();
statement.executeUpdate("drop database if exists " + dbName);
statement.executeUpdate("create database if not exists " + dbName);
statement.executeUpdate("use " + dbName);
String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))"; Statement stmt = connection.createStatement();
statement.executeUpdate(createTableSql); stmt.execute("drop database if exists " + dbName);
stmt.execute("create database if not exists " + dbName);
stmt.execute("use " + dbName);
String createTableSql = "create table " + stbName + "(ts timestamp, f1 int, f2 int, f3 int) tags(areaid int, loc binary(20))";
stmt.execute(createTableSql);
for(int i = 0; i < numOfTables; i++) { for (int i = 0; i < numOfTables; i++) {
String loc = i % 2 == 0 ? "beijing" : "shanghai"; String loc = i % 2 == 0 ? "beijing" : "shanghai";
String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')"; String createSubTalbesSql = "create table " + tablePrefix + i + " using " + stbName + " tags(" + i + ", '" + loc + "')";
statement.executeUpdate(createSubTalbesSql); stmt.execute(createSubTalbesSql);
} }
stmt.close();
} }
@Test @Test
public void testBatchInsert() throws SQLException{ public void testBatchInsert() throws SQLException {
ExecutorService executorService = Executors.newFixedThreadPool(numOfTables);
ExecutorService executorService = Executors.newFixedThreadPool(numOfTables); for (int i = 0; i < numOfTables; i++) {
final int index = i;
for (int i = 0; i < numOfTables; i++) { executorService.execute(() -> {
final int index = i; try {
executorService.execute(new Runnable() { long startTime = System.currentTimeMillis();
@Override Statement statement = connection.createStatement(); // get statement
public void run() { StringBuilder sb = new StringBuilder();
try { sb.append("INSERT INTO " + tablePrefix + index + " VALUES");
long startTime = System.currentTimeMillis(); Random rand = new Random();
Statement statement = connection.createStatement(); // get statement for (int j = 1; j <= numOfRecordsPerTable; j++) {
StringBuilder sb = new StringBuilder(); sb.append("(" + (ts + j) + ", ");
sb.append("INSERT INTO " + tablePrefix + index + " VALUES"); sb.append(rand.nextInt(100) + ", ");
Random rand = new Random(); sb.append(rand.nextInt(100) + ", ");
for (int j = 1; j <= numOfRecordsPerTable; j++) { sb.append(rand.nextInt(100) + ")");
sb.append("(" + (ts + j) + ", ");
sb.append(rand.nextInt(100) + ", ");
sb.append(rand.nextInt(100) + ", ");
sb.append(rand.nextInt(100) + ")");
}
statement.addBatch(sb.toString());
statement.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds");
connection.commit();
statement.close();
} catch (Exception e) {
e.printStackTrace();
} }
statement.addBatch(sb.toString());
statement.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println("Thread " + index + " takes " + (endTime - startTime) + " microseconds");
connection.commit();
statement.close();
} catch (Exception e) {
e.printStackTrace();
} }
}); });
} }
executorService.shutdown(); executorService.shutdown();
try { try {
...@@ -100,21 +93,23 @@ public class BatchInsertTest extends BaseTest { ...@@ -100,21 +93,23 @@ public class BatchInsertTest extends BaseTest {
} }
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from meters"); ResultSet rs = statement.executeQuery("select * from meters");
int num = 0; int num = 0;
while (rs.next()) { while (rs.next()) {
num++; num++;
} }
assertEquals(num, numOfTables * numOfRecordsPerTable); assertEquals(num, numOfTables * numOfRecordsPerTable);
rs.close(); rs.close();
} }
@After @After
public void close() throws Exception { public void close() {
statement.close(); try {
connection.close(); if (connection != null)
Thread.sleep(10); connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
} }
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册