|
| 1 | +package com.databricks.jdbc.integration.benchmarking; |
| 2 | + |
| 3 | +import static com.databricks.jdbc.integration.IntegrationTestUtil.*; |
| 4 | + |
| 5 | +import java.sql.*; |
| 6 | +import java.time.LocalDateTime; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Enumeration; |
| 9 | +import java.util.Random; |
| 10 | +import org.junit.jupiter.api.AfterEach; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +public class LargeQueriesBenchmarkingTest { |
| 15 | + private Connection connection; |
| 16 | + |
| 17 | + private String SCHEMA_NAME = "main"; |
| 18 | + private String TABLE_NAME = "tpcds_sf100_delta.catalog_sales"; |
| 19 | + |
| 20 | + private static final String RESULTS_TABLE = |
| 21 | + "main.jdbc_large_queries_benchmarking_schema.benchmarking_results"; |
| 22 | + private int ATTEMPTS = 10; |
| 23 | + |
| 24 | + private int ROWS = 1000000; |
| 25 | + |
| 26 | + long timesForOSSDriver[] = new long[ATTEMPTS]; |
| 27 | + long timesForDatabricksDriver[] = new long[ATTEMPTS]; |
| 28 | + |
| 29 | + long totalTimeForOSSDriver = 0; |
| 30 | + long totalTimeForDatabricksDriver = 0; |
| 31 | + |
| 32 | + @BeforeEach |
| 33 | + void setUp() throws SQLException { |
| 34 | + connection = getBenchmarkingJDBCConnection(); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + void tearDown() throws SQLException { |
| 39 | + DriverManager.registerDriver(new com.databricks.jdbc.driver.DatabricksDriver()); |
| 40 | + insertBenchmarkingDataIntoBenchfood(); |
| 41 | + connection.close(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void testLargeQueries() throws SQLException { |
| 46 | + // Currently connection is held by OSS driver |
| 47 | + long startTime = System.currentTimeMillis(); |
| 48 | + measureLargeQueriesPerformance(1); |
| 49 | + long endTime = System.currentTimeMillis(); |
| 50 | + System.out.println( |
| 51 | + "Time taken to execute large queries by OSS Driver: " |
| 52 | + + (endTime - startTime) |
| 53 | + + "ms " |
| 54 | + + "for " |
| 55 | + + ROWS |
| 56 | + + " rows and " |
| 57 | + + ATTEMPTS |
| 58 | + + " attempts"); |
| 59 | + |
| 60 | + System.out.println( |
| 61 | + "Driver used : " |
| 62 | + + connection.getMetaData().getDriverVersion() |
| 63 | + + " " |
| 64 | + + connection.getMetaData().getDriverName()); |
| 65 | + |
| 66 | + connection.close(); |
| 67 | + |
| 68 | + Enumeration<Driver> drivers = DriverManager.getDrivers(); |
| 69 | + |
| 70 | + while (drivers.hasMoreElements()) { |
| 71 | + Driver driver = drivers.nextElement(); |
| 72 | + if (driver.getClass().getName().contains("DatabricksDriver")) { |
| 73 | + DriverManager.deregisterDriver(driver); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + connection = |
| 78 | + DriverManager.getConnection( |
| 79 | + getBenchmarkingJDBCUrl(), "token", getDatabricksBenchmarkingToken()); |
| 80 | + |
| 81 | + startTime = System.currentTimeMillis(); |
| 82 | + measureLargeQueriesPerformance(2); |
| 83 | + endTime = System.currentTimeMillis(); |
| 84 | + System.out.println( |
| 85 | + "Time taken to execute large queries by Databricks Driver: " |
| 86 | + + (endTime - startTime) |
| 87 | + + "ms " |
| 88 | + + "for " |
| 89 | + + ROWS |
| 90 | + + " rows and " |
| 91 | + + ATTEMPTS |
| 92 | + + " attempts"); |
| 93 | + |
| 94 | + System.out.println( |
| 95 | + "Driver used : " |
| 96 | + + connection.getMetaData().getDriverVersion() |
| 97 | + + " " |
| 98 | + + connection.getMetaData().getDriverName()); |
| 99 | + |
| 100 | + connection.close(); |
| 101 | + } |
| 102 | + |
| 103 | + void measureLargeQueriesPerformance(int recording) { |
| 104 | + Random random = new Random(); |
| 105 | + for (int i = 0; i < ATTEMPTS; i++) { |
| 106 | + System.out.println("Attempt: " + i); |
| 107 | + int offset = |
| 108 | + i * 1000000 + random.nextInt(1000000); // Randomization to avoid possible query caching |
| 109 | + try (Statement statement = connection.createStatement()) { |
| 110 | + long startTime = System.currentTimeMillis(); |
| 111 | + ResultSet rs = |
| 112 | + statement.executeQuery( |
| 113 | + "SELECT * FROM " |
| 114 | + + SCHEMA_NAME |
| 115 | + + "." |
| 116 | + + TABLE_NAME |
| 117 | + + " LIMIT " |
| 118 | + + ROWS |
| 119 | + + " OFFSET " |
| 120 | + + offset); |
| 121 | + while (rs.next()) {} |
| 122 | + long endTime = System.currentTimeMillis(); |
| 123 | + if (recording == 1) { |
| 124 | + timesForOSSDriver[i] = endTime - startTime; |
| 125 | + } else { |
| 126 | + timesForDatabricksDriver[i] = endTime - startTime; |
| 127 | + } |
| 128 | + } catch (SQLException e) { |
| 129 | + e.printStackTrace(); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private void insertBenchmarkingDataIntoBenchfood() throws SQLException { |
| 135 | + |
| 136 | + Arrays.sort(timesForOSSDriver); |
| 137 | + Arrays.sort(timesForDatabricksDriver); |
| 138 | + |
| 139 | + // Removing min and max times for better average calculation |
| 140 | + for (int i = 1; i < ATTEMPTS - 1; i++) { |
| 141 | + totalTimeForOSSDriver += timesForOSSDriver[i]; |
| 142 | + totalTimeForDatabricksDriver += timesForDatabricksDriver[i]; |
| 143 | + } |
| 144 | + |
| 145 | + connection = getBenchfoodJDBCConnection(); |
| 146 | + |
| 147 | + String sql = |
| 148 | + "INSERT INTO " |
| 149 | + + RESULTS_TABLE |
| 150 | + + "(DateTime, OSS_AVG, DATABRICKS_AVG, OSS_TOTAL, DATABRICKS_TOTAL) VALUES (?, ?, ?, ?, ?)"; |
| 151 | + |
| 152 | + try (PreparedStatement stmt = connection.prepareStatement(sql)) { |
| 153 | + stmt.setTimestamp(1, java.sql.Timestamp.valueOf(LocalDateTime.now())); |
| 154 | + stmt.setDouble(2, ((totalTimeForOSSDriver * 1.0) / (ATTEMPTS - 2))); |
| 155 | + stmt.setDouble(3, ((totalTimeForDatabricksDriver * 1.0) / (ATTEMPTS - 2))); |
| 156 | + stmt.setLong(4, totalTimeForOSSDriver); |
| 157 | + stmt.setLong(5, totalTimeForDatabricksDriver); |
| 158 | + stmt.executeUpdate(); |
| 159 | + } catch (SQLException e) { |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments