blob: 7cdaca26194e15d9b217f94ad91ab070ad4fdc4a [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.sling.performance;
import java.util.LinkedList;
import java.util.List;
/**
* Abstract base class for individual performance benchmarks.
*/
public abstract class AbstractTest {
protected static int getScale(int def) {
int scale = Integer.getInteger("scale", 0);
if (scale == 0) {
scale = def;
}
return scale;
}
private volatile boolean running;
private List<Thread> threads;
/**
* Adds a background thread that repeatedly executes the given job
* until all the iterations of this test have been executed.
*
* @param job background job
*/
protected void addBackgroundJob(final Runnable job) {
Thread thread = new Thread() {
@Override
public void run() {
while (running) {
job.run();
}
}
};
thread.start();
threads.add(thread);
}
/**
* Run after all iterations of this test have been executed. Subclasses can
* override this method to clean up static test content.
*
* @throws Exception if an error occurs
*/
protected void afterSuite() throws Exception {
}
protected void afterTest() throws Exception {
}
/**
* Run before any iterations of this test get executed. Subclasses can
* override this method to set up static test content.
*
* @throws Exception if an error occurs
*/
protected void beforeSuite() throws Exception {
}
protected void beforeTest() throws Exception {
}
/**
* Executes a single iteration of this test.
*
* @return number of milliseconds spent in this iteration
* @throws Exception if an error occurs
*/
public long execute() throws Exception {
beforeTest();
try {
long start = System.currentTimeMillis();
runTest();
return System.currentTimeMillis() - start;
} finally {
afterTest();
}
}
protected abstract void runTest() throws Exception;
/**
* Prepares this performance benchmark.
*
* @param repository the repository to use
* @param credentials credentials of a user with write access
* @throws Exception if the benchmark can not be prepared
*/
public void setUp()
throws Exception {
this.threads = new LinkedList<Thread>();
this.running = true;
beforeSuite();
}
/**
* Cleans up after this performance benchmark.
*
* @throws Exception if the benchmark can not be cleaned up
*/
public void tearDown() throws Exception {
this.running = false;
for (Thread thread : threads) {
thread.join();
}
afterSuite();
this.threads = null;
}
public String toString() {
String name = getClass().getName();
return name.substring(name.lastIndexOf('.') + 1);
}
}