/** * Create a connection to database * * @param driverClassName: Database access driver class * @param url: Database url * @param dbUid: Database user id * @param dbPasswd: Database user password */ public static Connection createConnection( String driverClassName, String url, String dbUid, String dbPasswd) { Connection con = null; try { Class.forName(driverClassName).newInstance(); con = DriverManager.getConnection(url, dbUid, dbPasswd); } catch (Exception e) { DotsLogging.logException("Dots.createConnection: " + e); } return con; }
/** Check if database connection can be established using the values from config file. */ public static void checkDBConn() { try { Connection con = null; Statement stmt = null; Class.forName(DotsConfig.DRIVER_CLASS_NAME).newInstance(); con = DriverManager.getConnection(DotsConfig.URL, DotsConfig.DB_UID, DotsConfig.DB_PASSWD); stmt = con.createStatement(); stmt.close(); con.close(); } catch (Exception e) { DotsLogging.logException("Dots.checkDBConn: " + e.getMessage()); System.out.println("DataBase Connect failed!"); e.printStackTrace(); System.exit(1); } }
/** * Main function of the DOTS. Read the config file,create dotslogging,start performance client, * check db connection,create keyboard thread. Create new testcase according the cpu_usage and * connections, Write test summary and exception */ public static void main(String[] args) { int sleepInterval = 1; int waitInterval = 0; int averageUsage = 0; boolean reached = false; Calendar currentTime; /* Load DotsConfig value */ loadConfig(args); /* Create the instance of DotsLogging */ DotsLogging logging = new DotsLogging(TESTCASENAME); DotsLogging.logMessage("Database Opensource Test Suite V1.0"); DotsLogging.logMessage("Start to run JDBC API Test Case - " + TESTCASENAME); DotsLogging.logSummary("Start to run JDBC API Test Case - " + TESTCASENAME); if (!DotsConfig.RUN_AUTO) { System.out.println("\nDatabase Opensource Test Suite V1.0"); System.out.println("\nStart to run JDBC API test case - " + TESTCASENAME); } DotsLogging.logMessage("Initialization started"); if (!DotsConfig.RUN_AUTO) { System.out.println("\nTo stop running the test case, type STOP then press Enter\n"); } /* Start performance monitor thread */ startPerfCtl(); /* Check database connection */ checkDBConn(); DotsLogging.logMessage("Testing Database Connections ...OK"); /* Start summary writer thread */ DotsSummary summary = new DotsSummary(); summary.start(); DotsLogging.logMessage("Starting Summary Writer ... OK "); /* Set begin and end time */ Calendar endTime = Calendar.getInstance(); DotsSummary.startTime = System.currentTimeMillis(); endTime.add(Calendar.SECOND, DotsConfig.DURATION * 60); /* Start keyboard thread */ if (!DotsConfig.RUN_AUTO) { startKeyboard(); DotsLogging.logMessage("Starting Keyboard Thread ... OK"); } boolean firstRun = true; // used for LOB manipulation String lobFileName[] = new String[10]; // used for LOB manipulation averageUsage = DotsConfig.CPU_USAGE; int targetCpu = DotsConfig.CPU_TARGET; try { while (true) { currentTime = Calendar.getInstance(); if (currentTime.after(endTime)) break; if (DotsConfig.TERMINATION) break; /* Check if need create a new database access thread*/ if ((DotsConfig.AUTO_MODE && (averageUsage < targetCpu)) || (!DotsConfig.AUTO_MODE && (DotsConfig.THRDGRP.activeCount() < DotsConfig.CONNECTIONS))) { Connection conn = createConnection( DotsConfig.DRIVER_CLASS_NAME, DotsConfig.URL, DotsConfig.DB_UID, DotsConfig.DB_PASSWD); if (conn != null) { /* 5 second is a unit*/ sleepInterval = DotsConfig.INTERVAL * 12; /* Create DB access thread */ switch (TESTCASENAME.charAt(4)) { case '1': if (TESTCASENAME.equals("BTCJ1")) { BTCJ1 btcj1 = new BTCJ1(conn); Thread thrdb1 = new Thread(DotsConfig.THRDGRP, btcj1); thrdb1.start(); } else { ATCJ1 atcj1 = new ATCJ1(conn); Thread thrda1 = new Thread(DotsConfig.THRDGRP, atcj1); thrda1.start(); } break; case '2': if (TESTCASENAME.equals("BTCJ2")) { BTCJ2 btcj2 = new BTCJ2(conn); Thread thrdb2 = new Thread(DotsConfig.THRDGRP, btcj2); thrdb2.start(); } else { ATCJ2 atcj2 = new ATCJ2(conn); Thread thrda2 = new Thread(DotsConfig.THRDGRP, atcj2); thrda2.start(); } break; case '3': BTCJ3 btcj3 = new BTCJ3(conn); Thread thrdb3 = new Thread(DotsConfig.THRDGRP, btcj3); thrdb3.start(); break; case '4': BTCJ4 btcj4 = new BTCJ4(conn); Thread thrdb4 = new Thread(DotsConfig.THRDGRP, btcj4); thrdb4.start(); break; case '5': BTCJ5 btcj5 = new BTCJ5(conn); Thread thrdb5 = new Thread(DotsConfig.THRDGRP, btcj5); thrdb5.start(); break; case '6': if (firstRun) { for (int i = 0; i < 10; i++) { lobFileName[i] = DotsGenerator.mdClob(DotsGenerator.mdInt(10, 100)); } firstRun = false; } BTCJ6 btcj6 = new BTCJ6(conn, lobFileName); Thread thrdb6 = new Thread(DotsConfig.THRDGRP, btcj6); thrdb6.start(); break; case '7': if (firstRun) { for (int i = 0; i < 10; i++) { lobFileName[i] = DotsGenerator.mdBlob(DotsGenerator.mdInt(10, 100)); } firstRun = false; } BTCJ7 btcj7 = new BTCJ7(conn, lobFileName); Thread thrdb7 = new Thread(DotsConfig.THRDGRP, btcj7); thrdb7.start(); break; case '8': BTCJ8 btcj8 = new BTCJ8(conn); Thread thrdb8 = new Thread(DotsConfig.THRDGRP, btcj8); thrdb8.start(); break; } } else { /* If createConnection failed,sleepInterval will double, but the maximum of sleepInterval is 1 hour */ sleepInterval = sleepInterval * 2; if (sleepInterval > 720) sleepInterval = 720; } try { averageUsage = 0; /* Wait 5*sleepInterval seconds,check TERMINATION per 5 second */ for (int j = 0; j < sleepInterval; j++) { averageUsage += DotsConfig.CPU_USAGE; Thread.sleep(5000); if ((j % 12) == 0) DotsLogging.logMessage( "Active Threads = " + DotsConfig.THRDGRP.activeCount() + " Average CPU Usage = " + DotsConfig.CPU_USAGE + "%"); if (DotsConfig.TERMINATION) break; currentTime = Calendar.getInstance(); if (currentTime.after(endTime)) { DotsConfig.TERMINATION = true; break; } } /* Calculate the average usage of this sleepInterval */ averageUsage = averageUsage / sleepInterval; DotsLogging.logMessage( sleepInterval / 12 + " Minutes Average CPU Usage = " + averageUsage + "%"); } catch (Exception e) { DotsLogging.logException("Dots.main: " + e); } } else { /* When CPU_TARGET is reached,DOTS will not create new thread until average usage is less than CPU_TARGET-10 */ if (DotsConfig.AUTO_MODE && !reached) { reached = true; targetCpu = DotsConfig.CPU_TARGET - 10; DotsLogging.logMessage("CPU Target " + DotsConfig.CPU_TARGET + "% is achieved now."); } /* Wait 5 minutes then check create connection condition */ try { averageUsage = 0; for (int i = 0; i < 60; i++) { averageUsage += DotsConfig.CPU_USAGE; Thread.sleep(5000); if ((i % 12) == 0) DotsLogging.logMessage( "Active Threads = " + DotsConfig.THRDGRP.activeCount() + " Average CPU Usage = " + DotsConfig.CPU_USAGE + "%"); if (DotsConfig.TERMINATION) break; currentTime = Calendar.getInstance(); if (currentTime.after(endTime)) { DotsConfig.TERMINATION = true; break; } } averageUsage = averageUsage / 60; DotsLogging.logMessage("5 Minutes Average CPU Usage = " + averageUsage + "%"); } catch (Exception e) { DotsLogging.logException("Dots.main: " + e); } } } } catch (Throwable t) { DotsLogging.logException("Dots.main: " + t); System.out.println("Dots.main:" + t); } /* DURATION is reached or TERMINATION is set by other threads */ DotsConfig.TERMINATION = true; DotsLogging.logMessage("Dots is Terminating ..."); /* Clear temporary file used by testcase 6&7 */ if ((TESTCASENAME.charAt(4) == '6') || (TESTCASENAME.charAt(4) == '7')) { DotsLogging.logMessage("Cleaning temporary file ..."); if (!DotsConfig.RUN_AUTO) { System.out.println("Cleaning temporary file ..."); } try { for (int j = 0; j < 10; j++) { File tmpFile = new File(lobFileName[j]); if (tmpFile.exists()) tmpFile.delete(); } } catch (Exception e) { DotsLogging.logException("Clean temporary file error."); } } /* Write summary file and wait DB thread to exit */ try { if (!DotsConfig.RUN_AUTO) { System.out.println("\nWriting summary file ..."); } summary.writeSummaryFile(); summary.canExit = true; DotsPerf.canExit = true; DotsLogging.logSummary("Waiting for the active threads to exit ......"); if (!DotsConfig.RUN_AUTO) { System.out.println("\nWaiting for the active threads to exit ......"); } while (DotsConfig.THRDGRP.activeCount() > 0) { waitInterval++; Thread.sleep(1000); if (!DotsConfig.RUN_AUTO) { System.out.println("Current active thread number is " + DotsConfig.THRDGRP.activeCount()); } if (waitInterval > 60) break; // Max wait time is 1 minute } } catch (InterruptedException e) { DotsLogging.logException("Dots.main:" + e); } DotsLogging.logSummary("All Database Access Threads exit."); DotsLogging.close(); if (!DotsConfig.RUN_AUTO) { System.out.println("DOTS closed successfully."); } System.exit(0); }