KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > db > DBHelpers


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.internal.performance.db;
12
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.FileNotFoundException JavaDoc;
15 import java.io.FileOutputStream JavaDoc;
16 import java.io.PrintStream JavaDoc;
17 import java.sql.Connection JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.ResultSetMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23
24 import org.eclipse.test.internal.performance.PerformanceTestPlugin;
25 import org.eclipse.test.internal.performance.data.Dim;
26
27
28 public class DBHelpers {
29         
30     private Connection JavaDoc fConnection;
31     
32     
33     public static void main(String JavaDoc[] args) throws SQLException JavaDoc {
34         
35         //System.setProperty("eclipse.perf.dbloc", "net://localhost");
36

37         DBHelpers db= new DBHelpers();
38         
39         String JavaDoc outFile= null;
40         //outFile= "out.txt"; //$NON-NLS-1$
41
PrintStream JavaDoc ps= null;
42         if (outFile != null) {
43             try {
44                 ps= new PrintStream JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outFile)));
45             } catch (FileNotFoundException JavaDoc e) {
46                 System.err.println("can't create output file"); //$NON-NLS-1$
47
}
48         }
49         if (ps == null)
50             ps= System.out;
51
52         long start= System.currentTimeMillis();
53         
54         
55         db.dumpSizes(ps);
56         //db.renameVariation("|build=3.0.0_200410130800||config=relengbuildwin2|", "|build=3.0.0_200406251208_200410130800||config=relengbuildwin2|");
57
//db.dumpTable(ps, "VARIATION", 1000); //$NON-NLS-1$
58
//db.countSamplesWithNullVariations();
59

60         //Variations v= new Variations();
61
//v.put(PerformanceTestPlugin.CONFIG, "relengbuildwin2"); //$NON-NLS-1$
62
//v.put(PerformanceTestPlugin.BUILD, "I20041104%"); //$NON-NLS-1$
63

64         //db.dumpSummaries(v, null);
65
//db.removeSamples(v);
66
//db.countSamples(ps, v);
67
//db.view(ps, v, "org.eclipse.jdt.core.tests.performance.FullSourceWorkspaceTests#testPerfFullBuild()");
68

69         
70         System.out.println("time: " + ((System.currentTimeMillis()-start)/1000.0)); //$NON-NLS-1$
71

72         if (ps != System.out)
73             ps.close();
74     }
75
76     public DBHelpers() {
77         fConnection= DB.getConnection();
78     }
79     
80     void renameVariation(String JavaDoc oldName, String JavaDoc newName) throws SQLException JavaDoc {
81         PreparedStatement JavaDoc update= fConnection.prepareStatement("update VARIATION set KEYVALPAIRS = ? where KEYVALPAIRS = ? "); //$NON-NLS-1$
82
update.setString(1, newName); //$NON-NLS-1$
83
update.setString(2, oldName); //$NON-NLS-1$
84
update.executeUpdate();
85         update.close();
86     }
87     
88     void dumpSummaries(Variations variations, String JavaDoc scenarioPattern) {
89         SummaryEntry[] summries= DB.querySummaries(variations, scenarioPattern);
90         for (int i= 0; i < summries.length; i++)
91             System.out.println(summries[i]);
92     }
93     
94     void count(PrintStream JavaDoc ps) throws SQLException JavaDoc {
95         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select count(*) from SCALAR where DATAPOINT_ID not in (select DATAPOINT.ID from DATAPOINT)"); //$NON-NLS-1$
96
ResultSet JavaDoc set= stmt.executeQuery();
97         if (set.next())
98             ps.println("count: " + set.getInt(1)); //$NON-NLS-1$
99
set.close();
100         stmt.close();
101     }
102
103     void countDimension(PrintStream JavaDoc ps, Dim dim) throws SQLException JavaDoc {
104         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select count(*) from SCALAR where DIM_ID = ?"); //$NON-NLS-1$
105
stmt.setInt(1, dim.getId());
106         ResultSet JavaDoc set= stmt.executeQuery();
107         if (set.next())
108             ps.println("dimension " + dim + ": " + set.getInt(1)); //$NON-NLS-1$ //$NON-NLS-2$
109
}
110     
111     void countAllDimensions(PrintStream JavaDoc ps) throws SQLException JavaDoc {
112         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select distinct DIM_ID from SCALAR"); //$NON-NLS-1$
113
ResultSet JavaDoc set= stmt.executeQuery();
114         while (set.next()) {
115             Dim dimension= Dim.getDimension(set.getInt(1));
116             if (dimension != null)
117                 countDimension(ps, dimension);
118         }
119     }
120
121     int countSamples(PrintStream JavaDoc ps, Variations v) throws SQLException JavaDoc {
122         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select count(*) from SAMPLE, VARIATION where VARIATION.KEYVALPAIRS = ? and SAMPLE.VARIATION_ID = VARIATION.ID"); //$NON-NLS-1$
123
stmt.setString(1, v.toExactMatchString());
124         ResultSet JavaDoc set= stmt.executeQuery();
125         int n= 0;
126         if (set.next())
127             n= set.getInt(1);
128         ps.println("samples with variation " + v + ": " + n); //$NON-NLS-1$ //$NON-NLS-2$
129
return n;
130     }
131     
132     void countDatapoints(PrintStream JavaDoc ps, Variations v) throws SQLException JavaDoc {
133         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select count(*) from DATAPOINT, SAMPLE, VARIATION where VARIATION.KEYVALPAIRS = ? and SAMPLE.VARIATION_ID = VARIATION.ID and DATAPOINT.SAMPLE_ID= SAMPLE.ID"); //$NON-NLS-1$
134
stmt.setString(1, v.toExactMatchString());
135         ResultSet JavaDoc set= stmt.executeQuery();
136         if (set.next())
137             ps.println("datapoints with variation " + v + ": " + set.getInt(1)); //$NON-NLS-1$ //$NON-NLS-2$
138
}
139     
140     void countScalars(PrintStream JavaDoc ps, Variations v) throws SQLException JavaDoc {
141         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select count(*) from SCALAR, DATAPOINT, SAMPLE, VARIATION where VARIATION.KEYVALPAIRS = ? and SAMPLE.VARIATION_ID = VARIATION.ID and DATAPOINT.SAMPLE_ID= SAMPLE.ID and DATAPOINT.ID = SCALAR.DATAPOINT_ID"); //$NON-NLS-1$
142
stmt.setString(1, v.toExactMatchString());
143         ResultSet JavaDoc set= stmt.executeQuery();
144         if (set.next())
145             ps.println("scalars with variation " + v + ": " + set.getInt(1)); //$NON-NLS-1$ //$NON-NLS-2$
146
}
147     
148     void removeSamples(Variations v) throws SQLException JavaDoc {
149         
150         boolean delete= true;
151         
152         int n= countSamples(System.out, v);
153                 
154         int variation_id= 0;
155         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select ID from VARIATION where KEYVALPAIRS = ?"); //$NON-NLS-1$
156
stmt.setString(1, v.toExactMatchString());
157         ResultSet JavaDoc set= stmt.executeQuery();
158         if (set.next()) {
159             variation_id= set.getInt(1);
160             System.err.println("variation_id: " + variation_id); //$NON-NLS-1$
161
}
162         
163         if (variation_id <= 0) {
164             System.err.println("nothing found for variation " + v); //$NON-NLS-1$
165
return;
166             }
167
168         PreparedStatement JavaDoc iterSamples= fConnection.prepareStatement("select SAMPLE.ID, SAMPLE.SCENARIO_ID from SAMPLE where SAMPLE.VARIATION_ID = ?"); //$NON-NLS-1$
169
PreparedStatement JavaDoc iterDatapoints= fConnection.prepareStatement("select DATAPOINT.ID from DATAPOINT where DATAPOINT.SAMPLE_ID = ?"); //$NON-NLS-1$
170

171         PreparedStatement JavaDoc deleteScalars= fConnection.prepareStatement("delete from SCALAR where DATAPOINT_ID = ?"); //$NON-NLS-1$
172
PreparedStatement JavaDoc deleteDatapoints= fConnection.prepareStatement("delete from DATAPOINT where SAMPLE_ID = ?"); //$NON-NLS-1$
173
PreparedStatement JavaDoc deleteSamples= fConnection.prepareStatement("delete from SAMPLE where SAMPLE.ID = ?"); //$NON-NLS-1$
174
PreparedStatement JavaDoc deleteScenario= fConnection.prepareStatement("delete from SCENARIO where SCENARIO.ID = ?"); //$NON-NLS-1$
175

176         ResultSet JavaDoc samples= null, datapoints= null, configs= null;
177         iterSamples.setInt(1, variation_id);
178         samples= iterSamples.executeQuery();
179         while (samples.next()) {
180             int sample_id= samples.getInt(1);
181             int scenario_id= samples.getInt(2);
182             System.out.print(n + ": sample(" + sample_id + "):"); //$NON-NLS-1$ //$NON-NLS-2$
183
iterDatapoints.setInt(1, sample_id);
184             datapoints= iterDatapoints.executeQuery();
185             int dps= 0;
186             while (datapoints.next()) {
187                 int dp_id= datapoints.getInt(1);
188                 //ps.println(" dp: " + dp_id); //$NON-NLS-1$
189
if (delete) {
190                     deleteScalars.setInt(1, dp_id);
191                     try {
192                         deleteScalars.executeUpdate();
193                         fConnection.commit();
194                         dps++;
195                     } catch (SQLException JavaDoc e) {
196                         System.err.println("removing scalars: " + e); //$NON-NLS-1$
197
}
198                 }
199             }
200             System.out.println(" dps: " + dps); //$NON-NLS-1$
201
if (delete) {
202                 deleteDatapoints.setInt(1, sample_id);
203                 try {
204                     deleteDatapoints.executeUpdate();
205                     fConnection.commit();
206                 } catch (SQLException JavaDoc e1) {
207                     System.err.println("removing datapoints: " + e1); //$NON-NLS-1$
208
}
209                 
210                 deleteSamples.setInt(1, sample_id);
211                 try {
212                     deleteSamples.executeUpdate();
213                     fConnection.commit();
214                 } catch (SQLException JavaDoc e) {
215                     System.err.println("removing sample: " + e); //$NON-NLS-1$
216
}
217                 
218                 deleteScenario.setInt(1, scenario_id);
219                 try {
220                     deleteScenario.executeUpdate();
221                     fConnection.commit();
222                 } catch (SQLException JavaDoc e) {
223                     // System.err.println("removing scenario: " + e); //$NON-NLS-1$
224
}
225             }
226             n--;
227         }
228         if (delete) {
229             PreparedStatement JavaDoc deleteSummaries= fConnection.prepareStatement("delete from SUMMARYENTRY where VARIATION_ID = ?"); //$NON-NLS-1$
230
deleteSummaries.setInt(1, variation_id);
231             deleteSummaries.executeUpdate();
232             deleteSummaries.close();
233             
234             PreparedStatement JavaDoc deleteVariation= fConnection.prepareStatement("delete from VARIATION where ID = ?"); //$NON-NLS-1$
235
deleteVariation.setInt(1, variation_id);
236             try {
237                 deleteVariation.executeUpdate();
238             } catch (SQLException JavaDoc e) {
239                 System.err.println("removing variation: " + e); //$NON-NLS-1$
240
}
241             deleteVariation.close();
242         }
243
244         if (configs != null) configs.close();
245         if (samples != null) samples.close();
246         if (datapoints != null) datapoints.close();
247         
248         if (iterSamples != null) iterSamples.close();
249         if (iterDatapoints != null) iterDatapoints.close();
250         
251         if (deleteSamples != null) deleteSamples.close();
252         if (deleteScenario != null) deleteScenario.close();
253         if (deleteScalars != null) deleteScalars.close();
254         if (deleteDatapoints != null) deleteDatapoints.close();
255     }
256
257     void countSamplesWithNullVariations() throws SQLException JavaDoc {
258         Statement JavaDoc stmt= fConnection.createStatement();
259         ResultSet JavaDoc rs= stmt.executeQuery("select count(*) from SAMPLE where SAMPLE.VARIATION_ID is null"); //$NON-NLS-1$
260
while (rs.next()) {
261             int config_id= rs.getInt(1);
262             System.out.println("samples with NULL variation: " + config_id); //$NON-NLS-1$
263
}
264         rs.close();
265         stmt.close();
266     }
267    
268     void removeDimension(Dim dim) throws SQLException JavaDoc {
269         PreparedStatement JavaDoc q= fConnection.prepareStatement("delete from SCALAR where SCALAR.DIM_ID = ?"); //$NON-NLS-1$
270
q.setInt(1, dim.getId());
271         q.executeUpdate();
272         q.close();
273     }
274     
275     void dumpScenarios(PrintStream JavaDoc ps, String JavaDoc pattern) throws SQLException JavaDoc {
276         PreparedStatement JavaDoc stmt= fConnection.prepareStatement("select NAME from SCENARIO where NAME like ? order by NAME"); //$NON-NLS-1$
277
stmt.setString(1, pattern);
278         ResultSet JavaDoc rs= stmt.executeQuery();
279         while (rs.next())
280             ps.println(rs.getString(1));
281         rs.close();
282         stmt.close();
283     }
284     
285     void dumpSizes(PrintStream JavaDoc ps) throws SQLException JavaDoc {
286         if (fConnection == null)
287             return;
288         Statement JavaDoc stmt= fConnection.createStatement();
289         try {
290             ResultSet JavaDoc rs= stmt.executeQuery("SELECT sys.systables.tablename FROM sys.systables WHERE sys.systables.tablename NOT LIKE 'SYS%' "); //$NON-NLS-1$
291
while (rs.next())
292                 dumpSize(ps, rs.getString(1));
293             rs.close();
294         } finally {
295             stmt.close();
296         }
297     }
298
299     void dumpSize(PrintStream JavaDoc ps, String JavaDoc table) throws SQLException JavaDoc {
300         Statement JavaDoc stmt= fConnection.createStatement();
301         ResultSet JavaDoc rs= stmt.executeQuery("select Count(*) from " + table); //$NON-NLS-1$
302
if (rs.next())
303             ps.println(table + ": " + rs.getInt(1)); //$NON-NLS-1$
304
rs.close();
305         stmt.close();
306     }
307     
308     public void dumpAll(PrintStream JavaDoc ps, int maxRow) throws SQLException JavaDoc {
309         if (fConnection == null)
310             return;
311         if (maxRow < 0)
312             maxRow= 1000000;
313         Statement JavaDoc stmt= fConnection.createStatement();
314         try {
315             ResultSet JavaDoc rs= stmt.executeQuery("select SYS.SYSTABLES.TABLENAME from SYS.SYSTABLES where SYS.SYSTABLES.TABLENAME not like 'SYS%' "); //$NON-NLS-1$
316
while (rs.next()) {
317                 dumpTable(ps, rs.getString(1), maxRow);
318                 ps.println();
319             }
320             rs.close();
321         } finally {
322             stmt.close();
323         }
324     }
325     
326     void dumpTable(PrintStream JavaDoc ps, String JavaDoc tableName, int maxRow) throws SQLException JavaDoc {
327         ps.print(tableName + '(');
328         Statement JavaDoc select= fConnection.createStatement();
329         ResultSet JavaDoc result= select.executeQuery("select * from " + tableName); //$NON-NLS-1$
330
ResultSetMetaData JavaDoc metaData= result.getMetaData();
331         int n= metaData.getColumnCount();
332         for (int i= 0; i < n; i++) {
333             ps.print(metaData.getColumnLabel(i+1));
334             if (i < n-1)
335                 ps.print(", "); //$NON-NLS-1$
336
}
337         ps.println("):"); //$NON-NLS-1$
338
for (int r= 0; result.next() && r < maxRow; r++) {
339             for (int i= 0; i < n; i++)
340                 ps.print(' ' + result.getString(i+1));
341             ps.println();
342         }
343         select.close();
344     }
345
346     void view(PrintStream JavaDoc ps, Variations v, String JavaDoc scenarioPattern) throws SQLException JavaDoc {
347         Scenario[] scenarios= DB.queryScenarios(v, scenarioPattern, PerformanceTestPlugin.BUILD, null);
348         ps.println(scenarios.length + " Scenarios"); //$NON-NLS-1$
349
ps.println();
350         for (int s= 0; s < scenarios.length; s++)
351             scenarios[s].dump(ps, PerformanceTestPlugin.BUILD);
352     }
353 }
354
Popular Tags