@@ -33,7 +33,13 @@ public class AlterBenchmarkTest extends PLTestBase {
3333 "year integer," +
3434 "month integer);" ;
3535
36+ private static String selectSQL ;
37+ private static String alterAdd ;
38+ private static String alterDrop ;
39+ private static String alterChangeInline ;
40+ private static String alterChangeNotInline ;
3641
42+ private static enum DBMS {MySQL , Postgres , Peloton };
3743 @ Rule
3844 public ExpectedException thrown = ExpectedException .none ();
3945
@@ -50,21 +56,53 @@ public static Connection makePostgresConnection(String host,
5056 int port ,
5157 String username ,
5258 String pass ) throws SQLException {
53- String url = String .format ("jdbc:postgresql://%s:%d/postgres" ,
54- host , port );
59+ String url = String .format ("jdbc:postgresql://%s:%d/postgres" , host , port );
60+ Connection conn = DriverManager .getConnection (url , username , pass );
61+ return conn ;
62+ }
63+
64+ private static Connection makeMySQLConnection (String host ,
65+ int port ,
66+ String username ,
67+ String pass ) throws SQLException {
68+ String url = String .format ("jdbc:mysql://%s:%d/mysql" , host , port );
5569 Connection conn = DriverManager .getConnection (url , username , pass );
5670 return conn ;
5771 }
5872
5973 /**
6074 * Setup the connection to peloton or other DBMS
75+ * If you want to run other
6176 * @throws SQLException
6277 */
6378 @ Before
6479 public void Setup () throws SQLException {
65- //connection to Postgres
66- //conn = makePostgresConnection("localhost", 5432, "dingshilun", "");
67- conn = makeDefaultConnection ();
80+ DBMS testingDB = DBMS .Peloton ;
81+ String userName = "" ;
82+ String passWD = "" ;
83+ switch (testingDB ){
84+ case Peloton :
85+ alterAdd = "alter table tbl add day integer;" ;
86+ alterDrop = "alter table tbl drop month;" ;
87+ alterChangeInline = "alter table tbl alter year type varchar" ;
88+ alterChangeNotInline = "alter table tbl alter year type integer USING year::INTEGER" ;
89+ conn = makeDefaultConnection ();
90+ break ;
91+ case Postgres :
92+ alterAdd = "alter table tbl add day integer;" ;
93+ alterDrop = "alter table tbl drop month;" ;
94+ alterChangeInline = "alter table tbl alter year type varchar" ;
95+ alterChangeNotInline = "alter table tbl alter year type integer USING year::INTEGER" ;
96+ conn = makePostgresConnection ("localhost" , 5432 , userName , passWD );
97+ break ;
98+ case MySQL :
99+ alterAdd = "alter table tbl add day integer;" ;
100+ alterDrop = "alter table tbl drop month;" ;
101+ alterChangeInline = "alter table tbl modify year type varchar" ;
102+ alterChangeNotInline = "alter table tbl modify year type integer" ;
103+ conn = makeMySQLConnection ("localhost" , 3306 , userName , passWD );
104+ break ;
105+ }
68106 conn .setAutoCommit (true );
69107 InitDatabase ();
70108 }
@@ -82,6 +120,7 @@ public void Teardown() throws SQLException {
82120 */
83121 @ Test
84122 public void test_tuple_number_varies () throws SQLException {
123+ //define tuple number
85124 int [] workload = {};
86125 for (int i = 0 ; i < workload .length ;i ++) {
87126 // firstly use select * to make sure all tuples are in memory
@@ -96,29 +135,31 @@ public void test_tuple_number_varies() throws SQLException {
96135 } catch (Exception e ) {
97136 e .printStackTrace ();
98137 }
99-
100- String alterSql1 = "alter table tbl add day integer;" ;
138+
139+ String alterSql1 = alterAdd ;
101140 long startTime1 = System .currentTimeMillis ();
102141 conn .createStatement ().execute (alterSql1 );
103142 long endTime1 = System .currentTimeMillis ();
104143
105- String alterSql2 = "alter table tbl drop month;" ;
144+ String alterSql2 = alterDrop ;
106145 long startTime2 = System .currentTimeMillis ();
107146 conn .createStatement ().execute (alterSql2 );
108147 long endTime2 = System .currentTimeMillis ();
109148
110- String alterSql3 = "alter table tbl alter year type varchar" ;
149+ String alterSql3 = alterChangeInline ;
111150 long startTime3 = System .currentTimeMillis ();
112151 conn .createStatement ().execute (alterSql3 );
113152 long endTime3 = System .currentTimeMillis ();
114153
115- String alterSql4 = "alter table tbl alter year type integer USING year::INTEGER" ;
154+ String alterSql4 = alterChangeNotInline ;
116155 long startTime4 = System .currentTimeMillis ();
117156 conn .createStatement ().execute (alterSql4 );
118157 long endTime4 = System .currentTimeMillis ();
119158
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" );
159+ System .out .println ("Alter add column " + workload [i ] + " tuples took: "
160+ + (endTime1 - startTime1 ) + " milliseconds" );
161+ System .out .println ("Alter drop column " + workload [i ] + " tuples took: "
162+ + (endTime2 - startTime2 ) + " milliseconds" );
122163 System .out .println ("Alter change type from inline to not inline " + workload [i ] + " tuples took: " +
123164 (endTime3 - startTime3 ) + " milliseconds" );
124165 System .out .println ("Alter change type from not inline to inline " + workload [i ] + " tuples took: " +
@@ -128,33 +169,30 @@ public void test_tuple_number_varies() throws SQLException {
128169 }
129170
130171 private void NumVarInsertHelper (int insertNum ) throws SQLException {
131- String sql = "INSERT INTO tbl VALUES (?, ?, ?);" ;
132- PreparedStatement pstmt = conn .prepareStatement (sql );
172+ String sql ;
133173 for (int i = 0 ; i < insertNum ; i ++) {
134- setValues ( pstmt , new int [] { i , i +1 , i +2 } );
135- pstmt . addBatch ( );
174+ sql = String . format ( "INSERT INTO tbl VALUES (%d, %d, %d);" , i , i +1 , i +2 );
175+ conn . createStatement (). execute ( sql );
136176 }
137- pstmt .executeBatch ();
138177 }
139178
140179 /**
141- * Insert 10000 tuple, and test performance under different
142- * length of the tuple
180+ * Insert 'tupleNum' tuple, and test performance under different
181+ * length of the tuple defined by workload{}
143182 * @throws SQLException
144183 */
145184 @ Test
146185 public void test_tuple_length_variance () throws SQLException {
186+ //define tuple length
147187 int [] workload = {};
148188 int tupleNum = 10000 ;
149- String dropSQL = "DROP TABLE IF EXISTS tbl" ;
150- String sql = "" ;
151- conn .createStatement ().execute (dropSQL );
189+ conn .createStatement ().execute (SQL_DROP_TABLE );
152190 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 );
191+ String createSql = "CREATE TABLE tbl(id INTEGER PRIMARY KEY, " +
192+ "month VARCHAR(" + workload [i ] + ")," +
193+ "hour VARCHAR(" + workload [i ] + ")," +
194+ "year INTEGER);" ;
195+ conn .createStatement ().execute (createSql );
158196 LengthVarInsertHelper (tupleNum , workload [i ]);
159197
160198 try {
@@ -164,25 +202,28 @@ public void test_tuple_length_variance() throws SQLException {
164202 }
165203
166204 long startTime1 = System .currentTimeMillis ();
167- conn .createStatement ().execute ("ALTER TABLE tbl add payload4 integer;" );
205+ conn .createStatement ().execute (alterAdd );
168206 long endTime1 = System .currentTimeMillis ();
169207
170208 long startTime2 = System .currentTimeMillis ();
171- conn .createStatement ().execute ("ALTER TABLE tbl drop payload1;" );
209+ conn .createStatement ().execute (alterDrop );
172210 long endTime2 = System .currentTimeMillis ();
173211
174212 long startTime3 = System .currentTimeMillis ();
175- conn .createStatement ().execute ("ALTER TABLE tbl alter payload3 type varchar;" );
213+ conn .createStatement ().execute (alterChangeInline );
176214 long endTime3 = System .currentTimeMillis ();
177215
178- System .out .println ("Alter add column " + workload [i ] + " length took: " + (endTime1 - startTime1 )
216+ System .out .println ("Alter add column " + workload [i ] * 2 +
217+ " length took: " + (endTime1 - startTime1 )
179218 + " milliseconds" );
180- System .out .println ("Alter drop column " + workload [i ] + " length took: " + (endTime2 - startTime2 )
219+ System .out .println ("Alter drop column " + workload [i ] * 2 +
220+ " length took: " + (endTime2 - startTime2 )
181221 + " milliseconds" );
182- System .out .println ("Alter change type from not inline to inline " + workload [i ] + " length took: " +
183- (endTime3 - startTime3 ) + " milliseconds" );
222+ System .out .println ("Alter change type from not inline to inline " + workload [i ] * 2
223+ + " length took: " + (endTime3 - startTime3 ) +
224+ " milliseconds" );
184225
185- conn .createStatement ().execute (dropSQL );
226+ conn .createStatement ().execute (SQL_DROP_TABLE );
186227 }
187228 }
188229
0 commit comments