Skip to content

Commit

Permalink
add memory track
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushaga14 committed Dec 24, 2024
1 parent 9b091c1 commit 3c1eb16
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions apps/testing/src/main/java/com/akto/testing/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

Expand Down Expand Up @@ -154,7 +156,7 @@ private static TestingRun findPendingTestingRun(int userDeltaTime) {

Bson update = Updates.combine(
Updates.set(TestingRun.PICKED_UP_TIMESTAMP, Context.now()),
Updates.set(TestingRun.STATE, TestingRun.State.RUNNING)xr
Updates.set(TestingRun.STATE, TestingRun.State.RUNNING)
);

// returns the previous state of testing run before the update
Expand Down Expand Up @@ -210,7 +212,7 @@ private static void setTestingRunConfig(TestingRun testingRun, TestingRunResultS
} else {
loggerMaker.infoAndAddToDb("in loop Found testing run base config with id :" + baseConfig.getId(), LogDb.TESTING);
}

try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -239,6 +241,40 @@ private static void setTestingRunConfig(TestingRun testingRun, TestingRunResultS
}
}


// Runnable task for monitoring memory
static class MemoryMonitorTask implements Runnable {
@Override
public void run() {
Runtime runtime = Runtime.getRuntime();

// Loop to print memory usage every 1 second
while (true) {
// Calculate memory statistics
long totalMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
long usedMemory = totalMemory - freeMemory;

// Print memory statistics
System.out.print("Used Memory: " + (usedMemory / 1024 / 1024) + " MB ");
System.out.print("Free Memory: " + (freeMemory / 1024 / 1024) + " MB ");
System.out.print("Total Memory: " + (totalMemory / 1024 / 1024) + " MB ");
System.out.print("Available Memory: " + ((runtime.maxMemory() - usedMemory) / 1024 / 1024) + " MB ");
System.out.println("-------------------------");

// Pause for 1 second
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.err.println("Memory monitor thread interrupted: " + e.getMessage());
break; // Exit the loop if thread is interrupted
}
}
}
}

private static final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

public static void main(String[] args) throws InterruptedException {
String mongoURI = System.getenv("AKTO_MONGO_CONN");
ReadPreference readPreference = ReadPreference.secondary();
Expand All @@ -256,6 +292,8 @@ public static void main(String[] args) throws InterruptedException {
} while (!connectedToMongo);

setupRateLimitWatcher();

executorService.scheduleAtFixedRate(new Main.MemoryMonitorTask(), 0, 1, TimeUnit.SECONDS);

if (!SKIP_SSRF_CHECK) {
Setup setup = SetupDao.instance.findOne(new BasicDBObject());
Expand Down

0 comments on commit 3c1eb16

Please sign in to comment.