in micronaut-app/src/main/java/io/containerapps/javaruntime/workshop/micronaut/MicronautResource.java [108:140]
public String memory(@QueryValue(value = "bites", defaultValue = "10") Integer bites,
@QueryValue(value = "db", defaultValue = "false") Boolean db,
@QueryValue(value = "desc", defaultValue = "") String desc) {
LOGGER.log(INFO, "Micronaut: memory: {0} {1} with desc {2}", bites, db, desc);
Instant start = Instant.now();
if (bites == null) {
bites = 1;
}
HashMap hunger = new HashMap<>();
for (int i = 0; i < bites * 1024 * 1024; i += 8192) {
byte[] bytes = new byte[8192];
hunger.put(i, bytes);
for (int j = 0; j < 8192; j++) {
bytes[j] = '0';
}
}
if (db) {
Statistics statistics = new Statistics();
statistics.type = Type.MEMORY;
statistics.parameter = bites.toString();
statistics.duration = Duration.between(start, Instant.now());
statistics.description = desc;
repository.save(statistics);
}
String msg = "Micronaut: Memory consumption is done with " + bites + " bites in " + Duration.between(start, Instant.now()).getNano() + " nano-seconds.";
if (db) {
msg += " The result is persisted in the database.";
}
return msg;
}