Last active
May 2, 2024 16:03
-
-
Save killme2008/f2396aa1985beee98d36a615d9df67a7 to your computer and use it in GitHub Desktop.
Test creating 100k tables in greptimedb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.greptime; | |
import static org.junit.Assert.assertTrue; | |
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.concurrent.CyclicBarrier; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.atomic.AtomicInteger; | |
import org.junit.Test; | |
/** | |
* Unit test for simple App. | |
*/ | |
public class CreateTablesTest { | |
@Test | |
public void testGreptimeDB() throws Exception { | |
int tables = 100000; | |
int tags = 100; | |
int tagsPerTable = 15; | |
// Execute "CREATE TABLE phy (ts timestamp time index, val double) engine=metric | |
// with ("physical_metric_table" = "");" | |
boolean isMetricsTable = true; | |
String[] demoTags = new String[tags]; | |
for (int i = 0; i < tags; i++) { | |
demoTags[i] = "tag_" + i; | |
} | |
Class.forName("com.mysql.cj.jdbc.Driver"); | |
final ThreadLocalRandom rand = ThreadLocalRandom.current(); | |
if (isMetricsTable) { | |
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:4002/public", "root", "password"); | |
Statement stmt = conn.createStatement(); | |
stmt.execute( | |
"CREATE TABLE IF NOT EXISTS phy (ts timestamp time index, val double) engine=metric with (\"physical_metric_table\" = \"\")"); | |
stmt.close(); | |
conn.close(); | |
} | |
int threads = 10; | |
int partitons = tables / threads; | |
CyclicBarrier barrier = new CyclicBarrier(threads + 1); | |
AtomicInteger counter = new AtomicInteger(0); | |
for (int t = 0; t < threads; t++) { | |
final int threadsNumber = t; | |
Thread thread = new Thread() { | |
public void run() { | |
try { | |
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:4002/public", "root", | |
"password"); | |
Statement stmt = conn.createStatement(); | |
barrier.await(); | |
for (int i = threadsNumber * partitons; i < threadsNumber * partitons + partitons; i++) { | |
StringBuilder sql = new StringBuilder("create table test_") // | |
.append(i) // | |
.append("("); // | |
StringBuilder priKeys = new StringBuilder(); | |
Set<String> tagSet = new HashSet<String>(); | |
for (int j = 0; j < tagsPerTable; j++) { | |
String tag = null; | |
while (tag == null || tagSet.contains(tag)) { | |
tag = demoTags[rand.nextInt(tags)]; | |
} | |
tagSet.add(tag); | |
sql.append(tag).append(" string").append(","); | |
priKeys.append(tag); | |
if (j != tagsPerTable - 1) { | |
priKeys.append(","); | |
} | |
} | |
sql.append("ts timestamp time index, val double, primary key(") // | |
.append(priKeys)// | |
.append("))");// | |
if (isMetricsTable) { | |
sql.append(" engine = metric with (\"on_physical_table\" = \"phy\")"); | |
} else { | |
sql.append(" engine = mito"); | |
} | |
stmt.execute(sql.toString()); | |
int c = counter.incrementAndGet(); | |
if (c % 1000 == 0) { | |
System.out.println("Created " + c + " tables..."); | |
} | |
} | |
stmt.close(); | |
conn.close(); | |
barrier.await(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
System.exit(1); | |
} | |
} | |
}; | |
thread.start(); | |
System.out.println("Threads " + t + " started"); | |
} | |
long start = System.currentTimeMillis(); | |
barrier.await(); | |
barrier.await(); | |
System.out.println("Created " + tables + "tables with " + tags + " tags, cost:" | |
+ ((System.currentTimeMillis() - start) / 1000.0) + " seconds."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment