-
-
Save gothug/9de3fc1fa6a3a9e7b71c22ab0a3c7a0c to your computer and use it in GitHub Desktop.
examples.SelectQueryProfiler
This file contains 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 examples; | |
import com.github.housepower.jdbc.profiling.Profiler; | |
import java.sql.*; | |
import java.util.Enumeration; | |
import java.util.Properties; | |
import java.sql.Connection; | |
import java.sql.Driver; | |
import java.sql.DriverManager; | |
/** | |
*/ | |
public class SelectQueryProfiler { | |
public static void main(String[] args) throws Exception { | |
Profiler.getInstance().start(); | |
long statsOutputFrequency = Long.parseLong(args[0]); | |
int runGc = Integer.parseInt(args[1]); | |
String driver = args[2]; | |
String type = args[3]; | |
long n = Long.parseLong(args[4]); | |
Driver httpDriver = new ru.yandex.clickhouse.ClickHouseDriver(); | |
Driver nativeDriver = new com.github.housepower.jdbc.ClickHouseDriver(); | |
Enumeration<Driver> drivers = DriverManager.getDrivers(); | |
while (drivers.hasMoreElements()) { | |
DriverManager.deregisterDriver(drivers.nextElement()); | |
} | |
int port; | |
if (driver.equals("http")) { | |
System.out.println("Register http driver.."); | |
Class.forName("ru.yandex.clickhouse.ClickHouseDriver"); | |
DriverManager.registerDriver(httpDriver); | |
port = 8123; | |
} else { | |
System.out.println("Register native driver.."); | |
Class.forName("com.github.housepower.jdbc.ClickHouseDriver"); | |
DriverManager.registerDriver(nativeDriver); | |
port = 9000; | |
} | |
// pass credentials | |
Properties props = new Properties(); | |
props.put("database", ".."); | |
props.put("user", ".."); | |
props.put("password", ".."); | |
Connection connection = DriverManager.getConnection("jdbc:clickhouse://???:" + port, props); | |
String sql; | |
if (type.equals("int")) { | |
sql = String.format("SELECT number as n1 from numbers(%s)", n); | |
} else { | |
sql = String.format("SELECT number/rand64() as f1 from numbers(%s)", n); | |
} | |
Statement stmt = connection.createStatement(); | |
System.out.println("Executing sql: " + sql); | |
ResultSet rs = stmt.executeQuery(sql); | |
long sum = 0; | |
double sumDouble = 0; | |
long counter = 0; | |
Profiler.getInstance().logStat(0, 0); | |
while (rs.next()) { | |
counter++; | |
if (counter % (n / statsOutputFrequency) == 0) { | |
long percentComplete = (long) ((((double) counter) / n) * 100); | |
Profiler.getInstance().logStat(percentComplete, runGc); | |
} | |
if (type.equals("int")) { | |
sum += rs.getLong(1); | |
} else { | |
sumDouble += rs.getDouble(1); | |
} | |
} | |
if (type.equals("int")) { | |
System.out.println(sum); | |
} else { | |
System.out.println(sumDouble); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment