[jboss-cvs] CacheBenchFwk/src/org/cachebench ...

Manik Surtani manik at jboss.org
Thu May 17 03:37:44 EDT 2007


  User: msurtani
  Date: 07/05/17 03:37:44

  Added:       src/org/cachebench   CacheBenchmarkSlave.java
                        CacheBenchmarkRunner.java
  Log:
  Many changes
  
  Revision  Changes    Path
  1.1      date: 2007/05/17 07:37:44;  author: msurtani;  state: Exp;CacheBenchFwk/src/org/cachebench/CacheBenchmarkSlave.java
  
  Index: CacheBenchmarkSlave.java
  ===================================================================
  package org.cachebench;
  
  import org.apache.commons.digester.Digester;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.cachebench.config.Configuration;
  import org.cachebench.config.TestCase;
  import org.cachebench.utils.Instantiator;
  
  import java.io.File;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.List;
  
  /**
   * @author Manik Surtani (manik at surtani.org)
   * @version $Id: CacheBenchmarkSlave.java,v 1.1 2007/05/17 07:37:44 msurtani Exp $
   */
  public class CacheBenchmarkSlave
  {
  
     private Configuration conf;
     private Log logger = LogFactory.getLog("org.cachebench.CacheBenchmarkRunner");
     private Log errorLogger = LogFactory.getLog("CacheException");
  
     public static void main(String[] args)
     {
        String conf = null;
        if (args.length == 1)
        {
           conf = args[0];
        }
        if (conf != null && conf.toLowerCase().endsWith(".xml"))
        {
           new CacheBenchmarkSlave(conf);
        }
        else
        {
           new CacheBenchmarkSlave();
        }
     }
  
     private CacheBenchmarkSlave()
     {
        this("cachebench.xml");
     }
  
     private CacheBenchmarkSlave(String s)
     {
        // first, try and find the configuration on the filesystem.
        URL confFile = findOnFS(s);
        final Object o = new Object();
        if (confFile == null)
        {
           confFile = findInClasspath(s);
        }
        if (confFile == null)
        {
           logger.warn("Unable to locate a configuration file; Application terminated");
        }
        else
        {
           if (logger.isDebugEnabled()) logger.debug("Using configuration " + confFile);
           logger.debug("Parsing configuration");
           try
           {
              conf = parseConfiguration(confFile);
              logger.info("Starting Slave....");
  
              // will only start the first valid test.  Slaves don't support more than one test at a time
              List<TestCase> cases = conf.getTestCases();
              if (cases.size() == 0) throw new RuntimeException("Unable to proceed; no tests configured!");
              if (cases.size() != 1)
                 logger.warn("Slaves only support running one test case at a time.  You have " + cases.size() + " cases configured.  Will only attempt the first one.");
  
              CacheWrapper c = getCacheWrapperInstance(cases.get(0));
              c.init(cases.get(0).getParams());
              c.setUp();
  
              logger.info("Slave is listening.  CTRL-C to kill.");
              while (true)
              {
                 Thread.sleep(60000);
              }
  
           }
           catch (Exception e)
           {
              logger.warn("Unable to parse configuration file " + confFile + ". Application terminated.", e);
              errorLogger.fatal("Unable to parse configuration file " + confFile, e);
           }
        }
     }
  
     private Configuration parseConfiguration(URL url) throws Exception
     {
        Digester digester = new Digester();
        // set up the digester rules.
        digester.setValidating(false);
        digester.addObjectCreate("cachebench", "org.cachebench.config.Configuration");
        digester.addSetProperties("cachebench");
        digester.addObjectCreate("cachebench/testcase", "org.cachebench.config.TestCase");
        digester.addSetProperties("cachebench/testcase");
  
        digester.addObjectCreate("cachebench/testcase/test", "org.cachebench.config.TestConfig");
        digester.addSetProperties("cachebench/testcase/test");
        digester.addSetNext("cachebench/testcase/test", "addTest", "org.cachebench.config.TestConfig");
  
        digester.addObjectCreate("cachebench/testcase/param", "org.cachebench.config.NVPair");
        digester.addSetProperties("cachebench/testcase/param");
  
        digester.addSetNext("cachebench/testcase/param", "addParam", "org.cachebench.config.NVPair");
        digester.addSetNext("cachebench/testcase", "addTestCase", "org.cachebench.config.TestCase");
  
        digester.addObjectCreate("cachebench/report", "org.cachebench.config.Report");
        digester.addSetProperties("cachebench/report");
        digester.addSetNext("cachebench/report", "addReport", "org.cachebench.config.Report");
        return (Configuration) digester.parse(url.openStream());
     }
  
     /**
      * Util method to locate a resource on the filesystem as a URL
      *
      * @param filename
      * @return The URL object of the file
      */
     private URL findOnFS(String filename)
     {
        File f = new File(filename);
        try
        {
           if (f.exists()) return f.toURL();
        }
        catch (MalformedURLException mue)
        {
           // bad URL
        }
        return null;
     }
  
     /**
      * Util method to locate a resource in your classpath
      *
      * @param filename
      * @return The URL object of the file
      */
     private URL findInClasspath(String filename)
     {
        return getClass().getClassLoader().getResource(filename);
     }
  
     private CacheWrapper getCacheWrapperInstance(TestCase testCaseClass)
     {
        CacheWrapper cache = null;
        try
        {
           cache = (CacheWrapper) Instantiator.getInstance().createClass(testCaseClass.getCacheWrapper());
  
        }
        catch (Exception e)
        {
           logger.warn("Unable to instantiate CacheWrapper class: " + testCaseClass.getCacheWrapper() + " - Not Running any tests");
           errorLogger.error("Unable to instantiate CacheWrapper class: " + testCaseClass.getCacheWrapper(), e);
           errorLogger.error("Skipping this test");
        }
        return cache;
     }
  }
  
  
  
  1.1      date: 2007/05/17 07:37:44;  author: msurtani;  state: Exp;CacheBenchFwk/src/org/cachebench/CacheBenchmarkRunner.java
  
  Index: CacheBenchmarkRunner.java
  ===================================================================
  package org.cachebench;
  
  
  import org.apache.commons.digester.Digester;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.cachebench.config.Configuration;
  import org.cachebench.config.Report;
  import org.cachebench.config.TestCase;
  import org.cachebench.config.TestConfig;
  import org.cachebench.reportgenerators.ReportGenerator;
  import org.cachebench.tests.CacheTest;
  import org.cachebench.utils.Instantiator;
  
  import java.io.File;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  
  /**
   * @author Manik Surtani (manik at surtani.org)
   * @version $Id: CacheBenchmarkRunner.java,v 1.1 2007/05/17 07:37:44 msurtani Exp $
   */
  public class CacheBenchmarkRunner
  {
  
     private Configuration conf;
     private Log logger = LogFactory.getLog("org.cachebench.CacheBenchmarkRunner");
     private Log errorLogger = LogFactory.getLog("CacheException");
  
     public static void main(String[] args)
     {
        String conf = null;
        if (args.length == 1)
        {
           conf = args[0];
        }
        if (conf != null && conf.toLowerCase().endsWith(".xml"))
        {
           new CacheBenchmarkRunner(conf);
        }
        else
        {
           new CacheBenchmarkRunner();
        }
     }
  
     private CacheBenchmarkRunner()
     {
        this("cachebench.xml");
     }
  
     private CacheBenchmarkRunner(String s)
     {
        // first, try and find the configuration on the filesystem.
        URL confFile = findOnFS(s);
        if (confFile == null)
        {
           confFile = findInClasspath(s);
        }
        if (confFile == null)
        {
           logger.warn("Unable to locate a configuration file; Application terminated");
        }
        else
        {
           if (logger.isDebugEnabled()) logger.debug("Using configuration " + confFile);
           logger.debug("Parsing configuration");
           try
           {
              conf = parseConfiguration(confFile);
              logger.info("Starting Benchmarking....");
              List<TestResult> results = runTests(); // Run the tests from this point.
              if (results != null && results.size() != 0)
              {
                 generateReports(results); // Run the reports...
              }
              else
              {
                 logger.warn("No Results to be reported");
              }
              logger.info("Benchmarking Completed.  Hope you enjoyed using this!");
           }
           catch (Exception e)
           {
              logger.warn("Unable to parse configuration file " + confFile + ". Application terminated");
              errorLogger.fatal("Unable to parse configuration file " + confFile, e);
           }
        }
     }
  
     private Configuration parseConfiguration(URL url) throws Exception
     {
        Digester digester = new Digester();
        // set up the digester rules.
        digester.setValidating(false);
        digester.addObjectCreate("cachebench", "org.cachebench.config.Configuration");
        digester.addSetProperties("cachebench");
        digester.addObjectCreate("cachebench/testcase", "org.cachebench.config.TestCase");
        digester.addSetProperties("cachebench/testcase");
  
        digester.addObjectCreate("cachebench/testcase/test", "org.cachebench.config.TestConfig");
        digester.addSetProperties("cachebench/testcase/test");
        digester.addSetNext("cachebench/testcase/test", "addTest", "org.cachebench.config.TestConfig");
  
        digester.addObjectCreate("cachebench/testcase/param", "org.cachebench.config.NVPair");
        digester.addSetProperties("cachebench/testcase/param");
  
        digester.addSetNext("cachebench/testcase/param", "addParam", "org.cachebench.config.NVPair");
        digester.addSetNext("cachebench/testcase", "addTestCase", "org.cachebench.config.TestCase");
  
        digester.addObjectCreate("cachebench/report", "org.cachebench.config.Report");
        digester.addSetProperties("cachebench/report");
        digester.addSetNext("cachebench/report", "addReport", "org.cachebench.config.Report");
        return (Configuration) digester.parse(url.openStream());
     }
  
     /**
      * Executes each test case and returns the result.
      *
      * @return The Array of TestResult objects with the results of the tests.
      */
     private List<TestResult> runTests()
     {
        List<TestResult> results = new ArrayList<TestResult>();
        for (TestCase test : conf.getTestCases())
        {
           CacheWrapper cache;
           try
           {
              cache = getCacheWrapperInstance(test);
              if (cache != null)
              {
                 cache.init(test.getParams());
                 cache.setUp();
                 List<TestResult> resultsForCache = runTestsOnCache(cache, test);
                 shutdownCache(cache);
                 results.addAll(resultsForCache);
              }
           }
           catch (Exception e)
           {
              logger.warn("Unable to Initialize or Setup the Cache - Not performing any tests", e);
              errorLogger.error("Unable to Initialize or Setup the Cache: " + test.getCacheWrapper(), e);
              errorLogger.error("Skipping this test");
           }
        }
        return results;
     }
  
     /**
      * Peforms the necessary external tasks for cache benchmarking.
      * These external tasks are defined in the cachebench.xml and would
      * be executed against the cache under test.
      *
      * @param cache      The CacheWrapper for the cache in test.
      * @param testResult The TestResult of the test to which the tasks are executed.
      */
     private TestResult executeTestTasks(CacheWrapper cache, TestResult testResult)
     {
        try
        {
           if (conf.isEmptyCacheBetweenTests())
           {
              cache.empty();
           }
           if (conf.isGcBetweenTestsEnabled())
           {
              System.gc();
              Thread.sleep(conf.getSleepBetweenTests());
           }
        }
        catch (InterruptedException e)
        {
           // Nothing doing here...
        }
        catch (Exception e)
        {
           // The Empty process of the cache failed. Add a foot note for the TestResult here.
           testResult.setFootNote("The Cache Empty process failed after test case: " + testResult.getTestName() + " : " + testResult.getTestType());
           errorLogger.error("The Cache Empty process failed after test case : " + testResult.getTestName() + ", " + testResult.getTestType(), e);
        }
  
        return testResult;
     }
  
     private List<TestResult> runTestsOnCache(CacheWrapper cache, TestCase testCase)
     {
        List<TestResult> results = new ArrayList<TestResult>();
        for (TestConfig testConfig: testCase.getTests())
        {
           CacheTest testConfigClass = getCacheTestWrapper(testConfig);
           if (testConfigClass != null)
           {
              TestResult result;
              String testName = testConfig.getName();
              String testCaseName = testCase.getName();
              try
              {
                 result = testConfigClass.doTest(testCaseName, cache, testName, conf.getSampleSize(), conf.getNumThreads());
              }
              catch (Exception e)
              {
                 // The test failed. We should add a test result object with a error message and indicate that it failed.
                 result = new TestResult();
                 result.setTestName(testCaseName);
                 result.setTestTime(new Date());
                 result.setTestType(testName);
  
                 result.setTestPassed(false);
                 result.setErrorMsg("Failed to Execute - See logs for details : " + e.getMessage());
                 logger.warn("Test case : " + testCaseName + ", Test : " + testName + " - Failed");
                 errorLogger.error("Test case : " + testCaseName + ", Test : " + testName + " - Failed : " + e.getMessage(), e);
              }
              executeTestTasks(cache, result);
              results.add(result);
           }
        }
        return results;
     }
  
     private void generateReports(List<TestResult> results)
     {
        logger.info("Generating Reports...");
        for (Report report : conf.getReports())
        {
           ReportGenerator generator;
           try
           {
              generator = getReportWrapper(report);
              if (generator != null)
              {
                 generator.setResults(results);
                 generator.setOutputFile(new File(report.getOutputFile()));
                 generator.generate();
                 logger.info("Report Generation Complted");
              }
              else
              {
                 logger.info("Report not Generated - See logs for reasons");
              }
           }
           catch (Exception e)
           {
              logger.warn("Unable to generate Report : " + report.getGenerator() + " - See logs for reasons");
              logger.warn("Skipping this report");
              errorLogger.error("Unable to generate Report : " + report.getGenerator(), e);
              errorLogger.error("Skipping this report");
           }
        }
     }
  
     /**
      * Util method to locate a resource on the filesystem as a URL
      *
      * @param filename
      * @return The URL object of the file
      */
     private URL findOnFS(String filename)
     {
        File f = new File(filename);
        try
        {
           if (f.exists()) return f.toURL();
        }
        catch (MalformedURLException mue)
        {
           // bad URL
        }
        return null;
     }
  
     /**
      * Util method to locate a resource in your classpath
      *
      * @param filename
      * @return The URL object of the file
      */
     private URL findInClasspath(String filename)
     {
        return getClass().getClassLoader().getResource(filename);
     }
  
     private CacheWrapper getCacheWrapperInstance(TestCase testCaseClass)
     {
        CacheWrapper cache = null;
        try
        {
           cache = (CacheWrapper) Instantiator.getInstance().createClass(testCaseClass.getCacheWrapper());
  
        }
        catch (Exception e)
        {
           logger.warn("Unable to instantiate CacheWrapper class: " + testCaseClass.getCacheWrapper() + " - Not Running any tests");
           errorLogger.error("Unable to instantiate CacheWrapper class: " + testCaseClass.getCacheWrapper(), e);
           errorLogger.error("Skipping this test");
        }
        return cache;
     }
  
     private ReportGenerator getReportWrapper(Report reportClass)
     {
        ReportGenerator report = null;
        try
        {
           report = (ReportGenerator) Instantiator.getInstance().createClass(reportClass.getGenerator());
  
        }
        catch (Exception e)
        {
           logger.warn("Unable to instantiate ReportGenerator class: " + reportClass.getGenerator() + " - Not generating the report");
           errorLogger.error("Unable to instantiate ReportGenerator class: " + reportClass.getGenerator(), e);
           errorLogger.error("Skipping this report");
        }
        return report;
  
     }
  
     private CacheTest getCacheTestWrapper(TestConfig testConfig)
     {
        CacheTest cacheTestClass = null;
        try
        {
           cacheTestClass = (CacheTest) Instantiator.getInstance().createClass(testConfig.getTestClass());
  
        }
        catch (Exception e)
        {
           logger.warn("Unable to instantiate CacheTest class: " + testConfig.getTestClass() + " - Not Running any tests");
           errorLogger.error("Unable to instantiate CacheTest class: " + testConfig.getTestClass(), e);
           errorLogger.error("Skipping this Test");
        }
        return cacheTestClass;
  
     }
  
     private void shutdownCache(CacheWrapper cache)
     {
        try
        {
           cache.tearDown();
        }
        catch (Exception e)
        {
           logger.warn("Cache Shutdown - Failed.");
           errorLogger.error("Cache Shutdown failed : ", e);
        }
     }
  }
  
  



More information about the jboss-cvs-commits mailing list