|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Peloton |
| 4 | +// |
| 5 | +// AlterTableTest.java |
| 6 | +// |
| 7 | +// Identification: script/testing/junit/AlterBenchmarkTest.java |
| 8 | +// |
| 9 | +// Copyright (c) 2015-2018, Carnegie Mellon University Database Group |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import java.sql.*; |
| 14 | +import org.junit.*; |
| 15 | +import org.junit.rules.ExpectedException; |
| 16 | +import org.postgresql.util.PSQLException; |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
| 19 | +/* |
| 20 | + * Test case that compare performance under different workload |
| 21 | + * Will need to contact with different local SQL |
| 22 | + */ |
| 23 | +public class AlterBenchmarkTest extends PLTestBase { |
| 24 | + private Connection conn; |
| 25 | + private Connection conn2; |
| 26 | + |
| 27 | + private static final String SQL_DROP_TABLE = |
| 28 | + "DROP TABLE IF EXISTS tbl;"; |
| 29 | + |
| 30 | + private static final String SQL_CREATE_TABLE = |
| 31 | + "CREATE TABLE tbl (" + |
| 32 | + "id integer, " + |
| 33 | + "year integer," + |
| 34 | + "month integer);"; |
| 35 | + |
| 36 | + |
| 37 | + @Rule |
| 38 | + public ExpectedException thrown = ExpectedException.none(); |
| 39 | + |
| 40 | + /** |
| 41 | + * Initialize the database and table for testing |
| 42 | + */ |
| 43 | + private void InitDatabase() throws SQLException { |
| 44 | + Statement stmt = conn.createStatement(); |
| 45 | + stmt.execute(SQL_DROP_TABLE); |
| 46 | + stmt.execute(SQL_CREATE_TABLE); |
| 47 | + } |
| 48 | + |
| 49 | + public static Connection makePostgresConnection(String host, |
| 50 | + int port, |
| 51 | + String username, |
| 52 | + String pass) throws SQLException { |
| 53 | + String url = String.format("jdbc:postgresql://%s:%d/postgres", |
| 54 | + host, port); |
| 55 | + Connection conn = DriverManager.getConnection(url, username, pass); |
| 56 | + return conn; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Setup the connection to peloton or other DBMS |
| 61 | + * @throws SQLException |
| 62 | + */ |
| 63 | + @Before |
| 64 | + public void Setup() throws SQLException { |
| 65 | + //connection to Postgres |
| 66 | + //conn = makePostgresConnection("localhost", 5432, "dingshilun", ""); |
| 67 | + conn = makeDefaultConnection(); |
| 68 | + conn.setAutoCommit(true); |
| 69 | + InitDatabase(); |
| 70 | + } |
| 71 | + |
| 72 | + @After |
| 73 | + public void Teardown() throws SQLException { |
| 74 | + Statement stmt = conn.createStatement(); |
| 75 | + stmt.execute(SQL_DROP_TABLE); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Insert workload{} tuples into the table |
| 80 | + * In order to test performancce variance under different workload |
| 81 | + * @throws SQLException |
| 82 | + */ |
| 83 | + @Test |
| 84 | + public void test_tuple_number_varies() throws SQLException { |
| 85 | + int[] workload = {}; |
| 86 | + for (int i = 0; i< workload.length;i++) { |
| 87 | + // firstly use select * to make sure all tuples are in memory |
| 88 | + // for postgres and other disk based DBMS |
| 89 | + InitDatabase(); |
| 90 | + NumVarInsertHelper(workload[i]); |
| 91 | + String sql = "select * from tbl;"; |
| 92 | + conn.createStatement().execute(sql); |
| 93 | + |
| 94 | + try { |
| 95 | + Thread.sleep(1000); |
| 96 | + } catch (Exception e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + |
| 100 | + String alterSql1 = "alter table tbl add day integer;"; |
| 101 | + long startTime1 = System.currentTimeMillis(); |
| 102 | + conn.createStatement().execute(alterSql1); |
| 103 | + long endTime1 = System.currentTimeMillis(); |
| 104 | + |
| 105 | + String alterSql2 = "alter table tbl drop month;"; |
| 106 | + long startTime2 = System.currentTimeMillis(); |
| 107 | + conn.createStatement().execute(alterSql2); |
| 108 | + long endTime2 = System.currentTimeMillis(); |
| 109 | + |
| 110 | + String alterSql3 = "alter table tbl alter year type varchar"; |
| 111 | + long startTime3 = System.currentTimeMillis(); |
| 112 | + conn.createStatement().execute(alterSql3); |
| 113 | + long endTime3 = System.currentTimeMillis(); |
| 114 | + |
| 115 | + String alterSql4 = "alter table tbl alter year type integer USING year::INTEGER"; |
| 116 | + long startTime4 = System.currentTimeMillis(); |
| 117 | + conn.createStatement().execute(alterSql4); |
| 118 | + long endTime4 = System.currentTimeMillis(); |
| 119 | + |
| 120 | + System.out.println("Alter add column " + workload[i] + " tuples took: " + (endTime1 - startTime1) + " milliseconds"); |
| 121 | + System.out.println("Alter drop column " + workload[i] + " tuples took: " + (endTime2 - startTime2) + " milliseconds"); |
| 122 | + System.out.println("Alter change type from inline to not inline " + workload[i] + " tuples took: " + |
| 123 | + (endTime3 - startTime3) + " milliseconds"); |
| 124 | + System.out.println("Alter change type from not inline to inline " + workload[i] + " tuples took: " + |
| 125 | + (endTime4 - startTime4) + " milliseconds"); |
| 126 | + |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void NumVarInsertHelper(int insertNum) throws SQLException { |
| 131 | + String sql = "INSERT INTO tbl VALUES (?, ?, ?);"; |
| 132 | + PreparedStatement pstmt = conn.prepareStatement(sql); |
| 133 | + for (int i = 0; i < insertNum; i++) { |
| 134 | + setValues(pstmt, new int [] {i, i+1, i+2}); |
| 135 | + pstmt.addBatch(); |
| 136 | + } |
| 137 | + pstmt.executeBatch(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Insert 10000 tuple, and test performance under different |
| 142 | + * length of the tuple |
| 143 | + * @throws SQLException |
| 144 | + */ |
| 145 | + @Test |
| 146 | + public void test_tuple_length_variance() throws SQLException { |
| 147 | + int[] workload = {}; |
| 148 | + int tupleNum = 10000; |
| 149 | + String dropSQL = "DROP TABLE IF EXISTS tbl"; |
| 150 | + String sql = ""; |
| 151 | + conn.createStatement().execute(dropSQL); |
| 152 | + for (int i = 0; i < workload.length; i++) { |
| 153 | + sql = "CREATE TABLE tbl(id INTEGER PRIMARY KEY, " + |
| 154 | + "payload1 VARCHAR(" + workload[i] + ")," + |
| 155 | + "payload2 VARCHAR(" + workload[i] + ")," + |
| 156 | + "payload3 INTEGER);"; |
| 157 | + conn.createStatement().execute(sql); |
| 158 | + LengthVarInsertHelper(tupleNum, workload[i]); |
| 159 | + |
| 160 | + try { |
| 161 | + Thread.sleep(1000); |
| 162 | + } catch (Exception e) { |
| 163 | + e.printStackTrace(); |
| 164 | + } |
| 165 | + |
| 166 | + long startTime1 = System.currentTimeMillis(); |
| 167 | + conn.createStatement().execute("ALTER TABLE tbl add payload4 integer;"); |
| 168 | + long endTime1 = System.currentTimeMillis(); |
| 169 | + |
| 170 | + long startTime2 = System.currentTimeMillis(); |
| 171 | + conn.createStatement().execute("ALTER TABLE tbl drop payload1;"); |
| 172 | + long endTime2 = System.currentTimeMillis(); |
| 173 | + |
| 174 | + long startTime3 = System.currentTimeMillis(); |
| 175 | + conn.createStatement().execute("ALTER TABLE tbl alter payload3 type varchar;"); |
| 176 | + long endTime3 = System.currentTimeMillis(); |
| 177 | + |
| 178 | + System.out.println("Alter add column " + workload[i] + " length took: " + (endTime1 - startTime1) |
| 179 | + + " milliseconds"); |
| 180 | + System.out.println("Alter drop column " + workload[i] + " length took: " + (endTime2 - startTime2) |
| 181 | + + " milliseconds"); |
| 182 | + System.out.println("Alter change type from not inline to inline " + workload[i] + " length took: " + |
| 183 | + (endTime3 - startTime3) + " milliseconds"); |
| 184 | + |
| 185 | + conn.createStatement().execute(dropSQL); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // will simply generate string with length and return |
| 190 | + private String PayloadGenerate(int length) { |
| 191 | + long seed = System.currentTimeMillis() % 26 + 'a'; |
| 192 | + StringBuilder builder = new StringBuilder(); |
| 193 | + for (int i = 0; i < length; i++) { |
| 194 | + builder.append((char)seed); |
| 195 | + } |
| 196 | + return builder.toString(); |
| 197 | + } |
| 198 | + |
| 199 | + private void LengthVarInsertHelper(int insertNum, int varLen) throws SQLException { |
| 200 | + String payload1 = PayloadGenerate(varLen); |
| 201 | + String payload2 = PayloadGenerate(varLen); |
| 202 | + for (int i = 0; i<insertNum; i++){ |
| 203 | + if( i%1000 == 0) { |
| 204 | + payload1 = PayloadGenerate(varLen); |
| 205 | + payload2 = PayloadGenerate(varLen); |
| 206 | + } |
| 207 | + String sql = String.format("INSERT INTO tbl VALUES (%d, '%s', '%s', %d);", i, payload1, payload2, i+1); |
| 208 | + conn.createStatement().execute(sql); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments