Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions src/main/java/com/volta/engine/LoadEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.volta.http.HttpSender;
import java.net.http.HttpResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -12,20 +15,21 @@ public class LoadEngine {
private final int targetRps;
private final int durationSeconds;
private volatile boolean running = false;
private static final int MAX_CONCURRENT_REQUESTS = 1000;

public LoadEngine(String URL, int targetRPS, int durationSeconds) {
if (URL == null || URL.isBlank()) {
public LoadEngine(String url, int targetRps, int durationSeconds) {
if (url == null || url.isBlank()) {
throw new IllegalArgumentException("URL must not be empty");
}
if (targetRPS <= 0) {
if (targetRps <= 0) {
throw new IllegalArgumentException("RPS must be positive");
}
if (durationSeconds <= 0) {
throw new IllegalArgumentException("Duration must be positive");
}

this.url = URL;
this.targetRps = targetRPS;
this.url = url;
this.targetRps = targetRps;
this.durationSeconds = durationSeconds;
}

Expand All @@ -35,25 +39,55 @@ public void start() {
long endTime = System.nanoTime() + (long) durationSeconds * 1_000_000_000L;
long sendNextTime = System.nanoTime();

try (HttpSender sender = new HttpSender()) {
Semaphore semaphore = new Semaphore(MAX_CONCURRENT_REQUESTS);

try (HttpSender sender = new HttpSender();
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number of threads can grow without limit, and eventually no amount of memory will be enough


while (running && System.nanoTime() < endTime) {

while (System.nanoTime() < sendNextTime) {
// busy-wait
long waitMillis = (sendNextTime - System.nanoTime()) / 1_000_000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this calculation, waitMillis may become 0, which will cause the sleep to be skipped and the requests to be sent immediately


if (waitMillis > 0) {
try {
Thread.sleep(waitMillis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
}

try {
HttpResponse<String> response = sender.send(url);
log.info("Status: {}, Body: {}", response.statusCode(), response.body());
} catch (Exception e) {
log.error("Request failed", e);
semaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}

executor.submit(
() -> {
try {
HttpResponse<String> response = sender.send(url);
log.info("Status: {}, Body: {}", response.statusCode(), response.body());
} catch (Exception e) {
log.error("Request failed", e);
} finally {
semaphore.release();
}
});

if (System.nanoTime() - sendNextTime > 1_000_000_000L) {
sendNextTime = System.nanoTime();
}

sendNextTime += intervalNanos;
}
} catch (Exception e) {
log.error("Sender closed or failed to initialize", e);
}

// try-with-resources handles executor.close() and sender.close()
running = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s not obvious how the entire process terminates

log.info("Test finished");
}

Expand Down
35 changes: 0 additions & 35 deletions src/test/java/com/volta/HttpSenderTest.java

This file was deleted.

38 changes: 36 additions & 2 deletions src/test/java/com/volta/engine/LoadEngineTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,47 @@ void shouldHandleServerErrors() {
wireMock.stubFor(get("/error").willReturn(serverError()));
LoadEngine engine = new LoadEngine(baseUrl + "/error", 10, 2);

assertDoesNotThrow(() -> engine.start());
assertDoesNotThrow(engine::start);
}

@Test
void shouldHandleInvalidUrl() {
LoadEngine engine = new LoadEngine("http://invalid-host-that-does-not-exist:9999/test", 5, 2);

assertDoesNotThrow(() -> engine.start());
assertDoesNotThrow(engine::start);
}

@Test
void shouldRejectNullUrl() {
assertThrows(IllegalArgumentException.class, () -> new LoadEngine(null, 10, 5));
}

@Test
void shouldRejectEmptyUrl() {
assertThrows(IllegalArgumentException.class, () -> new LoadEngine("", 10, 5));
}

@Test
void shouldRejectZeroRps() {
assertThrows(
IllegalArgumentException.class, () -> new LoadEngine("http://localhost/test", 0, 5));
}

@Test
void shouldRejectNegativeRps() {
assertThrows(
IllegalArgumentException.class, () -> new LoadEngine("http://localhost/test", -1, 5));
}

@Test
void shouldRejectZeroDuration() {
assertThrows(
IllegalArgumentException.class, () -> new LoadEngine("http://localhost/test", 10, 0));
}

@Test
void shouldRejectNegativeDuration() {
assertThrows(
IllegalArgumentException.class, () -> new LoadEngine("http://localhost/test", 10, -1));
}
}
Loading