Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014 GraphAware
   *
   * This file is part of GraphAware.
   *
   * GraphAware is free software: you can redistribute it and/or modify it under the terms of
   * the GNU General Public License as published by the Free Software Foundation, either
   * version 3 of the License, or (at your option) any later version.
   *
  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public License for more details. You should have received a copy of
  * the GNU General Public License along with this program.  If not, see
  * <http://www.gnu.org/licenses/>.
  */
 
 package com.graphaware.test.performance;
 
 import org.junit.Test;
 
 import java.util.*;

Base class for suites of PerformanceTests.
 
 public abstract class PerformanceTestSuite {
     private static final Logger LOG = LoggerFactory.getLogger(PerformanceTestSuite.class);
 
     private TemporaryFolder temporaryFolder;
     private GraphDatabaseService database;

    
Get the performance tests run as a part of this test suite.

Returns:
performance tests, which will be run in this order.
 
     protected abstract PerformanceTest[] getPerfTests();

    
Perform the actual measurements and report results.
 
     @Test
     public void measurePerformance() {
         .info("Performance test suite started.");
 
         for (PerformanceTest performanceTest : getPerfTests()) {
             .info("Started performance test: " + performanceTest.longName());
 
             TestResults testResults = run(performanceTest);
             testResults.printToFile(performanceTest.longName(),  performanceTest.shortName() + "-" + System.currentTimeMillis() + ".txt");
 
             .info("Finished performance test: " + performanceTest.longName());
         }
 
         .info("Performance test suite finished.");
     }

    
Run the performance test a number of times (specified by the test itself) for each combination of parameters.

Parameters:
performanceTest to run.
Returns:
test results.
 
     private TestResults run(PerformanceTest performanceTest) {
         List<Map<StringObject>> parameterCombinations = generateParameterCombinations(performanceTest.parameters());
         TestResults testResults = new TestResultsImpl();
 
         for (Map<StringObjectparams : parameterCombinations) {
             run(performanceTestparamstestResults);
         }
 
         if (performanceTest.rebuildDatabase().equals(..)) {
             closeDatabase();
         }
 
         return testResults;
     }

    
Run the performance test a number of times (specified by the test itself) for a concrete combination of parameters.

Parameters:
performanceTest to run.
params with which to run.
testResults of tests run thus far.
 
     private void run(PerformanceTest performanceTestMap<StringObjectparamsTestResults testResults) {
         .info("Will run tests with " + paramsToString(params));
 
         createDatabaseIfNeeded(performanceTestparams);
 
         .info("Starting dry runs...");
 
        //dry runs - ignore results
        for (int i = 0; i < performanceTest.dryRuns(params); i++) {
            .debug("Dry run " + (i + 1));
            run(performanceTestparams);
        }
        .info("Starting measured runs...");
        //real runs
        for (int i = 0; i < performanceTest.measuredRuns(); i++) {
            .debug("Measured run " + (i + 1));
            testResults.acceptResult(paramsrun(performanceTestparams));
        }
        .info("Finished test runs.");
        if (performanceTest.rebuildDatabase().equals(..)) {
            closeDatabase();
        }
    }

    
Run the performance test once.

Parameters:
performanceTest to run.
params with which to run.
Returns:
time the test has taken in microseconds.
    private long run(PerformanceTest performanceTestMap<StringObjectparams) {
        if (performanceTest.rebuildDatabase().equals(..)
                && performanceTest.rebuildDatabase(params)) {
            closeDatabase();
        }
        createDatabaseIfNeeded(performanceTestparams);
        long result = performanceTest.run(params);
        if (performanceTest.rebuildDatabase().equals(..)) {
            closeDatabase();
        }
        return result;
    }

    
Create the database for the given performance test, if needed.

Parameters:
performanceTest to create a database for.
params with which the test will be run.
    private void createDatabaseIfNeeded(PerformanceTest performanceTestMap<StringObjectparams) {
        if ( != null) {
            return;
        }
        .info("(Re)creating database...");
        createTemporaryFolder();
        createDatabase(performanceTestparams);
    }

    
Create the database for the given performance test.

Parameters:
performanceTest to create a database for.
params with which the test will be run.
    private void createDatabase(PerformanceTest performanceTestMap<StringObjectparams) {
        Map<StringStringdbConfig = performanceTest.databaseParameters(params);
        if (dbConfig != null) {
            graphDatabaseBuilder = graphDatabaseBuilder.setConfig(dbConfig);
        }
         = graphDatabaseBuilder.newGraphDatabase();
        performanceTest.prepareDatabase(params);
    }

    
Create a temporary folder.
    private void createTemporaryFolder() {
         = new TemporaryFolder();
        try {
            .create();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    
Close the database and delete its data.
    private void closeDatabase() {
        if ( != null) {
            .info("Closing database...");
            .shutdown();
            .delete();
             = null;
        }
    }

    
Given a list of test parameters, generate all parameter combinations that tests will be run with.

Parameters:
parameters params.
Returns:
all parameter combination.
    private List<Map<StringObject>> generateParameterCombinations(List<Parameterparameters) {
        if (parameters.isEmpty()) {
            return Collections.singletonList(Collections.<StringObject>emptyMap());
        }
        Parameter parameter = parameters.remove(parameters.size() - 1);
        List<Map<StringObject>> result = new LinkedList<>();
        for (Map<StringObjectexisting : generateParameterCombinations(parameters)) {
            for (Object value : parameter.getValues()) {
                Map<StringObjecttoAdd = new LinkedHashMap<>(existing);
                toAdd.put(parameter.getName(), value);
                result.add(toAdd);
            }
        }
        return result;
    }
    private String paramsToString(Map<StringObjectparams) {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<StringObjectentry : params.entrySet()) {
            result.append(entry.getKey()).append("=").append(entry.getValue().toString()).append(", ");
        }
        return result.toString();
    }
New to GrepCode? Check out our FAQ X