[typo] Fixup: Rename StopWatches -> Stopwatches, StopWatched -> Timed

Change-Id: Ife9ea6eb262e04d9b04ca871fb5a2d849eddbddf
This commit is contained in:
Sebastian Zarnekow 2013-01-29 10:12:21 +01:00
parent 03ccfae348
commit 23699d44eb
2 changed files with 0 additions and 226 deletions

View file

@ -1,98 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.junit4.internal;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.xtext.util.internal.Stopwatches;
import org.eclipse.xtext.util.internal.Stopwatches.NumbersForTask;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class StopwatchRule implements TestRule {
private boolean watchAll = false;
public StopwatchRule() {
}
/**
* @param watchAll <code>true</code> if all tests should be timed, <code>false</code>
* if only tests should be considered which are annotated with {@link Timed @Timed}
*/
public StopwatchRule(boolean watchAll) {
this.watchAll = watchAll;
}
public void printStopwatchData(Description description, Map<String, Stopwatches.NumbersForTask> data) {
String property = System.getProperty("stopwatch.file");
PrintStream out = System.out;
FileOutputStream outputStream = null;
if (property != null) {
try {
outputStream = new FileOutputStream(new File(property), true);
out = new PrintStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
out.println("-------------------------------------------------------------------------------------------------------------------------\n");
out.println("Test '" + description.getDisplayName() + "' :");
out.println(getStopwatchDataAsReadableString(data));
} finally {
// out.flush();
if (outputStream != null)
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getStopwatchDataAsReadableString(Map<String, NumbersForTask> data) {
StringBuilder sb = new StringBuilder();
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
for (Entry<String, NumbersForTask> task : data.entrySet()) {
sb.append("Task '" + task.getKey() + "' took " + task.getValue().getMilliseconds() + "ms ("
+ task.getValue().getNumberOfMeasurements() + " measurements).\n");
}
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
return sb.toString();
}
public Statement apply(final Statement base, final Description description) {
if (!watchAll && description.getAnnotation(Timed.class) == null)
return base;
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
Stopwatches.setEnabled(true);
Stopwatches.resetAll();
base.evaluate();
} finally {
printStopwatchData(description, Stopwatches.allNumbers());
Stopwatches.resetAll();
Stopwatches.setEnabled(false);
}
}
};
}
}

View file

@ -1,128 +0,0 @@
/*******************************************************************************
* Copyright (c) 2012 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.util.internal;
import static com.google.common.collect.Maps.*;
import static java.util.Collections.*;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author Sven Efftinge - Initial contribution and API
*/
public class Stopwatches {
public interface StoppedTask {
public void start();
public void stop();
}
public static class StopwatchForTask implements StoppedTask {
final static long notRunning = -1;
private NumbersForTask numbers;
private long lastStart = notRunning;
private int reentrant = 0;
StopwatchForTask(NumbersForTask numbers) {
this.numbers = numbers;
}
public void start() {
if (!isRunning())
lastStart = System.currentTimeMillis();
reentrant++;
}
public boolean isRunning() {
return lastStart != notRunning;
}
public void stop() {
if (isRunning()) {
reentrant--;
if (reentrant == 0) {
long currentTimeMillis = System.currentTimeMillis();
numbers.addMilliseconds(currentTimeMillis - lastStart);
numbers.increaseMeasurements();
lastStart = notRunning;
}
}
}
}
public static class NumbersForTask {
long milliseconds = 0;
int numberOfMeasurements = 0;
public long getMilliseconds() {
return milliseconds;
}
synchronized void addMilliseconds(long milliseconds) {
this.milliseconds += milliseconds;
}
public int getNumberOfMeasurements() {
return numberOfMeasurements;
}
synchronized void increaseMeasurements() {
this.numberOfMeasurements++;
}
}
private static Map<String, NumbersForTask> data = newLinkedHashMap();
private static boolean enabled = false;
private static StoppedTask NULLIMPL = new StoppedTask() {
public void start() {}
public void stop() {}
};
public static StoppedTask forTask(String task) {
if (!enabled)
return NULLIMPL;
synchronized (data) {
NumbersForTask numbers = data.get(task);
if (numbers == null) {
numbers = new NumbersForTask();
data.put(task, numbers);
}
return new StopwatchForTask(numbers);
}
}
public static void setEnabled(boolean isEnabled) {
enabled = isEnabled;
}
public static void resetAll() {
synchronized (data) {
data.clear();
}
}
public static Map<String, NumbersForTask> allNumbers() {
synchronized (data) {
return unmodifiableMap(data);
}
}
public static String getPrintableStopwatchData() {
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
for (Entry<String, NumbersForTask> task : Stopwatches.allNumbers().entrySet()) {
sb.append("Task '"+task.getKey()+"' took "+task.getValue().getMilliseconds()+"ms ("+task.getValue().getNumberOfMeasurements()+" measurements).\n");
}
sb.append("-------------------------------------------------------------------------------------------------------------------------\n");
return sb.toString();
}
}