Commit 0a3f7cd5 authored by Sören Henning's avatar Sören Henning

removed debuggings and handle termination

parent d1b9147e
package watchme; package watchme;
import java.time.Duration;
import java.util.function.LongUnaryOperator; import java.util.function.LongUnaryOperator;
/** /**
...@@ -11,14 +12,15 @@ public class CallConfiguration { ...@@ -11,14 +12,15 @@ public class CallConfiguration {
private Method method; private Method method;
private LongUnaryOperator valueFunction; private LongUnaryOperator valueFunction;
private LongUnaryOperator timeFunction; private LongUnaryOperator timeFunction;
private Duration executionDuration;
private boolean multiThreaded; private boolean multiThreaded;
public CallConfiguration(Method method, LongUnaryOperator valueFunction, LongUnaryOperator timeFunction, boolean multiThreaded) { public CallConfiguration(Method method, LongUnaryOperator valueFunction, LongUnaryOperator timeFunction, Duration executionDuration, boolean multiThreaded) {
super(); super();
this.method = method; this.method = method;
this.valueFunction = valueFunction; this.valueFunction = valueFunction;
this.timeFunction = timeFunction; this.timeFunction = timeFunction;
this.executionDuration = executionDuration;
this.multiThreaded = multiThreaded; this.multiThreaded = multiThreaded;
} }
...@@ -46,6 +48,14 @@ public class CallConfiguration { ...@@ -46,6 +48,14 @@ public class CallConfiguration {
this.timeFunction = timeFunction; this.timeFunction = timeFunction;
} }
public Duration getExecutionDuration() {
return executionDuration;
}
public void setExecutionDuration(Duration executionDuration) {
this.executionDuration = executionDuration;
}
public boolean isMultiThreaded() { public boolean isMultiThreaded() {
return multiThreaded; return multiThreaded;
} }
......
package watchme; package watchme;
import java.util.concurrent.Executor; import java.time.Instant;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
/** /**
...@@ -10,8 +11,10 @@ import java.util.concurrent.Executors; ...@@ -10,8 +11,10 @@ import java.util.concurrent.Executors;
public class Caller implements Runnable { public class Caller implements Runnable {
private final Method method; private final Method method;
private final Executor executor; private final ExecutorService executor;
private final SleepService sleepService; private final SleepService sleepService;
private final Instant endTime;
public Caller(final CallConfiguration configuration) { public Caller(final CallConfiguration configuration) {
this.sleepService = new SleepService(configuration.getTimeFunction()); this.sleepService = new SleepService(configuration.getTimeFunction());
...@@ -22,13 +25,15 @@ public class Caller implements Runnable { ...@@ -22,13 +25,15 @@ public class Caller implements Runnable {
} }
this.method = configuration.getMethod(); this.method = configuration.getMethod();
this.method.setSleepService(new SleepService(configuration.getValueFunction())); this.method.setSleepService(new SleepService(configuration.getValueFunction()));
this.endTime = Instant.now().plus(configuration.getExecutionDuration());
} }
public void run() { public void run() {
while(true) { while(Instant.now().isBefore(this.endTime)) {
this.executor.execute(this.method); this.executor.execute(this.method);
this.sleepService.sleep(); this.sleepService.sleep();
} }
this.executor.shutdown();
} }
} }
...@@ -13,8 +13,6 @@ public class FooMethod extends Method { ...@@ -13,8 +13,6 @@ public class FooMethod extends Method {
public void foo() { public void foo() {
this.sleepService.sleep(); this.sleepService.sleep();
System.out.println("Hello from foo()!");
//System.out.println(Instant.now() + ": " + "Hello from foo()!");
} }
} }
...@@ -4,35 +4,44 @@ import watchme.function.FunctionSum; ...@@ -4,35 +4,44 @@ import watchme.function.FunctionSum;
import watchme.function.Noise; import watchme.function.Noise;
import watchme.function.StaticAnomaly; import watchme.function.StaticAnomaly;
import java.time.Duration;
public enum Scenario { public enum Scenario {
CONSTANT_WITH_ANOMALY(new CallConfiguration(new FooMethod(), CONSTANT_WITH_ANOMALY(new CallConfiguration(new FooMethod(),
new FunctionSum(x -> 100, new Noise(10, 12161608), new StaticAnomaly(100, 3_000)), new FunctionSum(x -> 100, new Noise(10, 12161608), new StaticAnomaly(100, 3_000)),
new FunctionSum(x -> 100, new Noise(50, 12101608)), new FunctionSum(x -> 100),
Duration.ofSeconds(5),
true)), true)),
LINEAR_WITH_ANOMALY(new CallConfiguration(new FooMethod(), LINEAR_WITH_ANOMALY(new CallConfiguration(new FooMethod(),
new FunctionSum(x -> (long) ((500.0/4_000) * x), new Noise(10, 908170816), new StaticAnomaly(100, 3_000)), new FunctionSum(x -> (long) ((500.0/4_000) * x), new Noise(10, 908170816), new StaticAnomaly(100, 3_000)),
new FunctionSum(x -> 100, new Noise(50, 9211708)), new FunctionSum(x -> 100, new Noise(50, 9211708)),
Duration.ofSeconds(5),
true)), true)),
SINUS_WITH_ANOMALY(new CallConfiguration(new FooMethod(), SINUS_WITH_ANOMALY(new CallConfiguration(new FooMethod(),
new FunctionSum(x -> (long) ((100 * Math.sin(((2*Math.PI) / 10_000) * x)) + 200), new Noise(10, 9261708), new StaticAnomaly(-100, 3_000)), new FunctionSum(x -> (long) ((100 * Math.sin(((2*Math.PI) / 10_000) * x)) + 200), new Noise(10, 9261708), new StaticAnomaly(-100, 3_000)),
new FunctionSum(x -> 100, new Noise(50, 9301708)), new FunctionSum(x -> 100, new Noise(50, 9301708)),
Duration.ofSeconds(50),
true)), true)),
EXPONENTIAL(new CallConfiguration(new FooMethod(), EXPONENTIAL(new CallConfiguration(new FooMethod(),
new FunctionSum(x -> (long) ((Math.exp(x / (4_000/8)) - 1) / (3_000 / 400)) + 100, new Noise(10, 9311708)), new FunctionSum(x -> (long) ((Math.exp(x / (4_000/8)) - 1) / (3_000 / 400)) + 100, new Noise(10, 9311708)),
new FunctionSum(x -> 100, new Noise(50, 9321708)), new FunctionSum(x -> 100, new Noise(50, 9321708)),
Duration.ofSeconds(5),
true)), true)),
CONSTANT_SLOW(new CallConfiguration(new FooMethod(), CONSTANT_SLOW(new CallConfiguration(new FooMethod(),
x -> 100, x -> 100,
x -> 2000, x -> 2000,
Duration.ofSeconds(60),
true)), true)),
UNAMED(new CallConfiguration(new FooMethod(), UNAMED(new CallConfiguration(new FooMethod(),
x -> (long) (100 * Math.sin((double) x / (10*1000)) + 200), x -> (long) (100 * Math.sin((double) x / (10*1000)) + 200),
x -> x, x -> x,
Duration.ofSeconds(5),
true)), true)),
INCREASING_SPEED(new CallConfiguration(new FooMethod(), INCREASING_SPEED(new CallConfiguration(new FooMethod(),
x -> 10, x -> 10,
x -> (long) (x * ((double) -1_000 / 10_000)) + 1_000, x -> (long) (x * ((double) -1_000 / 10_000)) + 1_000,
Duration.ofSeconds(60),
true)); true));
private CallConfiguration configuration; private CallConfiguration configuration;
......
...@@ -19,11 +19,8 @@ public class SleepService { ...@@ -19,11 +19,8 @@ public class SleepService {
} }
public void sleep() { public void sleep() {
// Time in milliseconds since instantiation
long timestamp = Duration.between(instantiationTime, Instant.now()).toMillis(); long timestamp = Duration.between(instantiationTime, Instant.now()).toMillis();
// Time to sleep in milliseconds
long sleepTime = Math.max(this.function.applyAsLong(timestamp), 0); long sleepTime = Math.max(this.function.applyAsLong(timestamp), 0);
//System.out.println(" -> sleep:" + sleepTime);
try { try {
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
} catch (InterruptedException e) { } catch (InterruptedException e) {
......
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