Skip to content

Commit

Permalink
Add max duration
Browse files Browse the repository at this point in the history
  • Loading branch information
Žiga Leber committed Aug 9, 2023
1 parent 3c67234 commit 7becbbc
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void fitness(SunflowKnobs request, StreamObserver<Fitness> responseObserv
knobs.bucketSize(),
knobs.aoSamples()));

final var runner = factory.new Runner(knobs);
final var runner = request.hasMaxDuration() ? factory.new Runner(knobs, request.getMaxDuration()) : factory.new Runner(knobs);
final var measurements = meter.measure(runner);
final var distance = runner.imageDifference().mse();
final var reply = Fitness.newBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,59 @@ void testQuick() throws IOException {
assertTrue(response.getTime() > 0);
assertTrue(response.getDistance() > 0.0);
}

@Tag("PostDeploy")
@org.junit.jupiter.api.Test
void testSlow() throws IOException {
var request = SunflowKnobs.newBuilder()
.setThreads(1)
.setResolution(640)
.setAaMin(3)
.setAaMax(3)
.setBucketSize(32)
.setAoSamples(64)
.setFilter(Filter.BLACKMAN_HARRIS)
.build();

var channel = ManagedChannelBuilder.forTarget("localhost:50051")
.usePlaintext()
.build();

var stub = SunflowServiceGrpc.newBlockingStub(channel);
var response = stub.fitness(request);
System.out.println(response.getEnergy());
System.out.println(response.getTime());
System.out.println(response.getDistance());
assertTrue(response.getEnergy() > 0.0);
assertTrue(response.getTime() > 0);
assertTrue(response.getDistance() > 0.0);
}

@Tag("PostDeploy")
@org.junit.jupiter.api.Test
void testCustomMaxDuration() throws IOException {
var request = SunflowKnobs.newBuilder()
.setThreads(1)
.setResolution(640)
.setAaMin(3)
.setAaMax(3)
.setBucketSize(32)
.setAoSamples(64)
.setMaxDuration(500)
.setFilter(Filter.BLACKMAN_HARRIS)
.build();

var channel = ManagedChannelBuilder.forTarget("localhost:50051")
.usePlaintext()
.build();

var stub = SunflowServiceGrpc.newBlockingStub(channel);
var response = stub.fitness(request);
System.out.println(response.getEnergy());
System.out.println(response.getTime());
System.out.println(response.getDistance());
assertTrue(response.getEnergy() > 0.0);
assertTrue(response.getTime() > 0);
assertTrue(response.getDistance() > 0.0);
}
}
2 changes: 1 addition & 1 deletion proto/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group 'si.um.feri.lpm.green'
version '0.4.0'
version '0.5.0'

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion proto/src/main/proto
Submodule proto updated 1 files
+1 −0 sunflow.proto
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ include 'sunflowload'
include 'proto'
include 'green'
include 'kaze'
include 'client'

Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,23 @@ public class Runner implements Runnable {
BufferedImage image;
BufferedImage resizedImage;
SunflowKnobs knobs;
double tolerance = 0.5;
double tolerance;
boolean killed = false;
long maxDuration;

public Runner(SunflowKnobs knobs) {

public Runner(SunflowKnobs knobs, long maxDuration) {
this.knobs = knobs;
this.maxDuration = maxDuration;
}

public Runner(SunflowKnobs knobs) {
this(knobs, 0.5);
}

public Runner(SunflowKnobs knobs, double tolerance) {
this.knobs = knobs;
this.tolerance = tolerance;
this.maxDuration = referenceDuration + (long)(tolerance * referenceDuration); // Tolerance is added to account for the variability of the runtimes
}

@Override
Expand All @@ -112,7 +119,7 @@ public void run() {
UI.taskCancel();
killed = true;
}
}, referenceDuration + (long)(tolerance * referenceDuration)); // Tolerance is added to account for the variability of the runtimes
}, maxDuration);
this.image = render(this.knobs);
timer.cancel();
this.resizedImage = resize(this.image, referenceImage.getWidth(), referenceImage.getHeight());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package si.um.feri.lpm.green.sunflowload;

import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.Instant;

import static org.junit.jupiter.api.Assertions.*;

class SunflowRunnerTest {
Expand Down Expand Up @@ -49,4 +53,15 @@ void timeoutSlow2() {
runner.run();
assertTrue(runner.killed());
}

@Test
void timeoutCustom() {
final long duration = 5000;
final var runner = factory.new Runner(new SunflowKnobs(7, 640, 3, 3, 32, 64, Filter.BLACKMAN_HARRIS), duration);
Instant start = Instant.now();
runner.run();
final var actualDuration = Duration.between(start, Instant.now()).toMillis();
assertTrue(runner.killed());
System.out.println(actualDuration);
}
}

0 comments on commit 7becbbc

Please sign in to comment.