Commit 053abd78 authored by Alexander's avatar Alexander
Browse files

Move performance-test stuff to the Assignment2TestBase

parent 08fe90cd
......@@ -9,7 +9,6 @@ public class PerformanceTests extends Assignment1TestBase {
@Test
@DisplayName("PerformanceTest1")
void performanceTest1() {
enablePerformanceTest();
String path = "src/final_assignment/task_1/tests/data/performance_test_1.io";
TestPair[] pairs = loadTestPairsFromIOFile(path);
String args[] = {"torus", "20", "2"};
......
package final_assignment.task_2;
import edu.kit.informatik.Terminal;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.*;
import test.TestBase;
import test.TestPair;
import java.io.File;
import java.io.*;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import static org.junit.jupiter.api.Assertions.fail;
/**
* This class links the DummyMain of this task and the TestBase
......@@ -14,6 +19,10 @@ import java.io.File;
* @since 23.02.2018
*/
public class Assignment2TestBase extends TestBase {
private static String userId;
private boolean isPerformanceTest;
private long startTime;
/**
* Tests the program linked with the DummyMain with the specified io-file
*
......@@ -63,10 +72,120 @@ public class Assignment2TestBase extends TestBase {
@AfterAll
public static void finish() {
if (++runCount >= getTestClassCount()) {
int sum = numSucceeded + numFailed;
double percentage = (numSucceeded * 100.) / sum;
int sum = numSucceededTests + numFailedTests;
double percentage = (numSucceededTests * 100.) / sum;
System.out.printf("You mastered %.2f %% of the %d tests!%n", percentage, sum);
}
}
// Performance Test stuff
@BeforeEach
void disablePerformanceTest() {
isPerformanceTest = false;
}
private double getReferenceTime() {
long startTime = System.nanoTime();
int value = 1337;
for (int i = 0; i < Math.pow(2, 21); i++) {
value += i;
if (value > Math.pow(2, 21)) {
value %= i;
}
}
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000.0;
System.out.println("The reference time is: " + time + " ms. Ref-value: " + value + ".");
return time;
}
@AfterEach
public void measurePerformance(TestInfo testInfo) {
if (isPerformanceTest && testPassed) {
long endTime = System.nanoTime();
double runTime = ((double) (endTime - startTime)) / 1000000.0;
double refTime = getReferenceTime();
double standardRunTime = runTime / refTime * 30.0;
System.out.println("Your run time for test '" + getTestName(testInfo) + "' is " + runTime + " ms.");
System.out.println("Standard run time based on your run time is " + standardRunTime + " ms.");
try {
Socket socket = new Socket("vaelum.de", 12000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.print(userId + "\n");
out.flush();
out.print(getTestName(testInfo) + "\n");
out.flush();
out.print("" + standardRunTime + "\n");
out.flush();
System.out.println(in.readLine() + "\n");
in.close();
out.close();
socket.close();
} catch (IOException e2) {
System.out.println(e2 + "\n");
}
}
}
private static String getMacAddress() {
try {
Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
while (networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement();
byte[] mac = network.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
return sb.toString();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
fail("Unable to get mac address.");
return null;
}
@BeforeAll
public static void checkUserId() {
userId = getMacAddress();
final String USER_ID_FILENAME = "UserId.id";
try (final BufferedReader reader = new BufferedReader(new FileReader(USER_ID_FILENAME))) {
userId = reader.readLine();
reader.close();
} catch (final IOException e) {
try {
PrintWriter of = new PrintWriter(USER_ID_FILENAME);
of.println(userId);
of.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
protected void enablePerformanceTest() {
isPerformanceTest = true;
checkUserId();
startTime = System.nanoTime();
}
}
......@@ -4,13 +4,9 @@ import edu.kit.informatik.Terminal;
import org.junit.jupiter.api.*;
import java.io.*;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
......@@ -32,21 +28,17 @@ import static org.junit.jupiter.api.Assertions.fail;
*/
public abstract class TestBase {
public static final String ERROR_PREFIX = "Error, ";
private String testName;
public static final String IO_FILE_COMMAND_LINE_ARGS_PREFIX = "! ";
protected Logger log;
private List<LogRecord> logData = new ArrayList<>(100);
protected boolean testPassed = false;
protected static int numFailedTests = 0;
protected static int numSucceededTests = 0;
private List<LogRecord> logData = new ArrayList<>(100);
private String testName;
private boolean alwaysShowLog;
private boolean showAllProgramOutput = false;
private boolean isPerformanceTest;
private long startTime;
private static String userId;
public static final String IO_FILE_COMMAND_LINE_ARGS_PREFIX = "! ";
protected static int numFailed = 0;
protected static int numSucceeded = 0;
/**
* Initializes testing
*/
......@@ -55,54 +47,12 @@ public abstract class TestBase {
Terminal.isTest = true;
}
private static String getMacAddress() {
try {
Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
while (networks.hasMoreElements()) {
NetworkInterface network = networks.nextElement();
byte[] mac = network.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? ":" : ""));
}
return sb.toString();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
fail("Unable to get mac address.");
return null;
}
private static void checkUserId() {
userId = getMacAddress();
final String USER_ID_FILENAME = "UserId.id";
try (final BufferedReader reader = new BufferedReader(new FileReader(USER_ID_FILENAME))) {
userId = reader.readLine();
reader.close();
} catch (final IOException e) {
try {
PrintWriter of = new PrintWriter(USER_ID_FILENAME);
of.println(userId);
of.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
}
/**
* Print diff block.
*/
@BeforeEach
protected void initTest(TestInfo testInfo) {
alwaysShowLog = false;
isPerformanceTest = false;
testPassed = false;
testName = testInfo.getDisplayName();
log = Logger.getLogger(testInfo.getClass().getName());
......@@ -126,73 +76,20 @@ public abstract class TestBase {
log.info("Running test '" + getTestName(testInfo) + "' ...\n");
}
private String getTestName(TestInfo testInfo) {
protected String getTestName(TestInfo testInfo) {
return testInfo.getDisplayName() + " @ " + testInfo.getTestClass().toString().substring(15).replace("]", "");
}
private double getReferenceTime() {
long startTime = System.nanoTime();
int value = 1337;
for (int i = 0; i < Math.pow(2, 21); i++) {
value += i;
if (value > Math.pow(2, 21)) {
value %= i;
}
}
long endTime = System.nanoTime();
double time = (endTime - startTime) / 1000000.0;
System.out.println("The reference time is: " + time + " ms. Ref-value: " + value + ".");
return time;
}
/**
* Clears all data from previous testExamples
* This is automatically called after every test
*/
@AfterEach
protected void cleanUp(TestInfo testInfo) {
protected void cleanUp() {
if (testPassed) {
numSucceeded++;
numSucceededTests++;
} else {
numFailed++;
}
if (isPerformanceTest && testPassed) {
long endTime = System.nanoTime();
double runTime = ((double) (endTime - startTime)) / 1000000.0;
double refTime = getReferenceTime();
double standardRunTime = runTime / refTime * 30.0;
System.out.println("Your run time for test '" + getTestName(testInfo) + "' is " + runTime + " ms.");
System.out.println("Standard run time based on your run time is " + standardRunTime + " ms.");
try {
Socket socket = new Socket("vaelum.de", 12000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.print(userId + "\n");
out.flush();
out.print(getTestName(testInfo) + "\n");
out.flush();
out.print("" + standardRunTime + "\n");
out.flush();
System.out.println(in.readLine() + "\n");
in.close();
out.close();
socket.close();
} catch (IOException e2) {
System.out.println(e2 + "\n");
}
numFailedTests++;
}
if (showAllProgramOutput && !testPassed && !Terminal.OUT_TEST.isEmpty()) {
......@@ -218,11 +115,10 @@ public abstract class TestBase {
logData.clear();
}
/**
* Prints the current log
*/
protected void printLog() {
private void printLog() {
for (LogRecord logRecord : logData) {
System.out.print(logRecord.getMessage());
}
......@@ -237,7 +133,6 @@ public abstract class TestBase {
*/
protected void failAndLog(String message) {
printLog();
isPerformanceTest = false;
fail(message);
}
......@@ -248,12 +143,6 @@ public abstract class TestBase {
alwaysShowLog = true;
}
protected void enablePerformanceTest() {
isPerformanceTest = true;
checkUserId();
startTime = System.nanoTime();
}
/**
* Gets the next output from the output-queue and removes it
*
......@@ -461,6 +350,9 @@ public abstract class TestBase {
return new TestPair(input, output);
}
/**
* If enabled, this shows the output of a program, after a test fails
*/
protected void enableShowAllProgramOutput() {
this.showAllProgramOutput = true;
}
......
......@@ -72,7 +72,6 @@ class FancyProgramTest extends TestBase {
@Test
@DisplayName("performance test")
void perfTest() {
enablePerformanceTest();
testWithIOFile("src/test_examples/fancy_program/data/expected_test.io"
, () -> DummyMain.main(null));
testWithIOFile("src/test_examples/fancy_program/data/expected_test.io"
......
......@@ -27,7 +27,6 @@ public class DelTableTest extends TestBase {
@Test
@DisplayName("test 2")
void test2() {
enablePerformanceTest();
testUsingPairs(generatePairs(0, 50, 100000, readFile("src/test_examples/worksheet_5/task_b/data/solutionString2"))
, () -> DummyMain.main(null));
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment