1 package org.netbeans.mdr.test; 2 3 import java.net.InetAddress ; 4 9 10 import java.util.Properties ; 11 import java.util.Enumeration ; 12 import java.net.MalformedURLException ; 13 import java.sql.*; 14 import java.util.ResourceBundle ; 15 16 21 public class PerformanceServerConnection { 22 23 static { 24 try { 25 Class.forName("org.gjt.mm.mysql.Driver"); 26 } catch (ClassNotFoundException ex) { 27 ex.printStackTrace(); 28 } 29 } 30 31 private static PerformanceServerConnection connection; 32 33 private Connection conn=null; 34 private String URL; 35 private String username; 36 private String password; 37 private String IDEbranch; 38 private String buildNumber; 39 private String testSuite; 40 private int totalTestCases; 41 private int executionId; 42 43 44 50 private static String readProperty (final Properties p, final String key) throws Exception { 51 String value=null; 52 if (p!=null) value=p.getProperty(key,System.getProperty(key)); 53 else value=System.getProperty(key); 54 if (value==null) throw new Exception ("PerformanceServerConnection is missing option : "+key); 55 return value; 56 } 57 58 60 public PerformanceServerConnection() { 61 this(System.getProperties()); 62 } 63 64 78 public PerformanceServerConnection(final Properties p) { 79 try { 80 executionId=0; 81 URL=readProperty(p,"netbeans.performance.serverURL"); 82 username=readProperty(p,"netbeans.performance.serverUsername"); 83 password=readProperty(p,"netbeans.performance.serverPassword"); 84 IDEbranch=readProperty(p,"netbeans.build.branch"); 85 buildNumber=readProperty(p,"org.openide.version"); 86 testSuite=readProperty(p,"netbeans.performance.testSuite"); 87 totalTestCases=Integer.parseInt(readProperty(p,"netbeans.performance.totalTestCases")); 88 String systemInfo=readProperty(p,"os.name")+" "+readProperty(p,"os.version")+" "+readProperty(p,"os.arch"); 89 String javaInfo=readProperty(p,"java.vendor")+" "+readProperty(p,"java.version"); 90 InetAddress local=InetAddress.getLocalHost(); 91 String computer=local.getHostName()+" "+local.getHostAddress(); 92 System.out.println(testSuite+", "+systemInfo+", "+javaInfo+", "+IDEbranch+", "+buildNumber+", "+String.valueOf(totalTestCases)); 93 executionId=getExecutionId(testSuite, computer, systemInfo, javaInfo, IDEbranch, buildNumber, totalTestCases); 94 } catch (Exception ex) { 95 ex.printStackTrace(); 96 executionId=-1; 97 } 98 } 99 100 104 private boolean equalOptions(final Properties p) { 105 try { 106 return 107 URL.equals(readProperty(p,"netbeans.performance.serverURL"))&& 108 username.equals(readProperty(p,"netbeans.performance.serverUsername"))&& 109 password.equals(readProperty(p,"netbeans.performance.serverPassword"))&& 110 IDEbranch.equals(readProperty(p,"netbeans.build.branch"))&& 111 buildNumber.equals(readProperty(p,"org.openide.version"))&& 112 testSuite.equals(readProperty(p,"netbeans.performance.testSuite"))&& 113 (totalTestCases==Integer.parseInt(readProperty(p,"netbeans.performance.totalTestCases"))); 114 } catch (Exception ex) { 115 ex.printStackTrace(); 116 return false; 117 } 118 } 119 120 123 public static PerformanceServerConnection getConnection() { 124 return getConnection(System.getProperties()); 125 } 126 127 131 public static PerformanceServerConnection getConnection(final Properties p) { 132 if (connection==null) { 133 connection=new PerformanceServerConnection(p); 134 } else { 135 if (!connection.equalOptions(p)) { 136 connection=new PerformanceServerConnection(p); 137 } 138 } 139 return connection; 140 } 141 142 147 public static PerformanceServerConnection getConnection(final Class clazz, final String propertiesFile) { 148 Properties props=new Properties (); 149 try { 150 props.load(clazz.getClassLoader().getResourceAsStream(propertiesFile)); 151 } catch (Exception ex) { 152 ex.printStackTrace(); 153 } 154 return getConnection(props); 155 } 156 157 158 161 public static void setConnection(PerformanceServerConnection newConnection) { 162 connection=newConnection; 163 } 164 165 169 public void logTestCase(final String testCase, final long time) { 170 if (executionId>0) { 171 try { 172 logTestCase(executionId, testCase, time); 173 } catch (Exception ex) { 174 ex.printStackTrace(); 175 } 176 } 177 System.out.println(testCase+": "+String.valueOf(time)+" ms."); 178 } 179 180 183 public void logTestCases(final Properties testCases) { 184 if (executionId>0) { 185 Enumeration keys=testCases.propertyNames(); 186 String key; 187 while (keys.hasMoreElements()) { 188 try { 189 key=(String )keys.nextElement(); 190 logTestCase(executionId, key, Integer.parseInt(testCases.getProperty(key))); 191 } catch (Exception ex) { 192 ex.printStackTrace(); 193 } 194 } 195 } 196 } 197 198 private int getExecutionId(final String testSuite, final String computer, final String systemInfo, final String javaInfo, final String IDEbranch, final String buildNumber, final int totalTestCases) throws SQLException { 199 try { 200 if (conn==null) 201 conn=DriverManager.getConnection(URL, username, password); 202 PreparedStatement stat=conn.prepareStatement("insert into executions (branch,build,computer,java,rundate,system,testsuite,totalkeys) values (?,?,?,?,?,?,?,?)"); 203 stat.setString(1,IDEbranch); 204 stat.setString(2,buildNumber); 205 stat.setString(3,computer); 206 stat.setString(4,javaInfo); 207 stat.setTimestamp(5,new Timestamp((new java.util.Date ()).getTime())); 208 stat.setString(6,systemInfo); 209 stat.setString(7,testSuite); 210 stat.setInt(8,totalTestCases); 211 stat.executeUpdate(); 212 SQLWarning w; 213 while ((w=stat.getWarnings())!=null) { 214 System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+w.getMessage()); 215 } 216 ResultSet res=stat.executeQuery("select last_insert_id()"); 217 while ((w=res.getWarnings())!=null) { 218 System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+w.getMessage()); 219 } 220 res.first(); 221 return res.getInt(1); 223 } catch (SQLException ex) { 224 System.err.println("["+testSuite+" "+systemInfo+" "+javaInfo+" "+IDEbranch+" "+buildNumber+" "+totalTestCases+"]: "+ex.getMessage()); 225 ex.printStackTrace(); 226 throw ex; 227 } 228 } 229 230 private void logTestCase(final int executionId, final String testCase, final long time) throws SQLException { 231 try { 232 if (conn==null) 233 conn=DriverManager.getConnection(URL, username, password); 234 PreparedStatement stat=conn.prepareStatement("insert into data (id,testcase,time) values (?,?,?)"); 235 stat.setInt(1,executionId); 236 stat.setString(2,testCase); 237 stat.setLong(3,time); 238 stat.executeUpdate(); 239 SQLWarning w; 240 while ((w=stat.getWarnings())!=null) { 241 System.err.println("["+executionId+" "+testCase+" "+time+"]: "+w.getMessage()); 242 } 243 stat=conn.prepareStatement("update executions set finishedkeys=finishedkeys+1 where id=?"); 244 stat.setInt(1,executionId); 245 stat.executeUpdate(); 246 while ((w=stat.getWarnings())!=null) { 247 System.err.println("["+executionId+" "+testCase+" "+time+"]: "+w.getMessage()); 248 } 249 } catch (SQLException ex) { 250 System.err.println("["+executionId+" "+testCase+" "+time+"]: "+ex.getMessage()); 251 ex.printStackTrace(); 252 throw ex; 253 } 254 } 255 256 257 260 public static void main(String args[]) { 261 System.setProperty("netbeans.performance.serverPassword",""); 262 System.setProperty("netbeans.performance.serverUsername","perfserver"); 263 System.setProperty("netbeans.performance.serverURL","jdbc:mysql://beetle.czech.sun.com:3306/performance"); 264 System.setProperty("netbeans.performance.testSuite","test"); 265 System.setProperty("netbeans.build.branch","test"); 266 System.setProperty("netbeans.performance.totalTestCases","1"); 267 268 getConnection().logTestCase("test value",100); 269 } 270 } 271 | Popular Tags |