in micronaut-app/src/main/java/io/containerapps/javaruntime/workshop/micronaut/MicronautResource.java [57:93]
public String cpu(@QueryValue(value = "iterations", defaultValue = "10") Long iterations,
@QueryValue(value = "db", defaultValue = "false") Boolean db,
@QueryValue(value = "desc", defaultValue = "") String desc) {
LOGGER.log(INFO, "Micronaut: cpu: {0} {1} with desc {2}", iterations, db, desc);
Long iterationsDone = iterations;
Instant start = Instant.now();
if (iterations == null) {
iterations = 20000L;
} else {
iterations *= 20000;
}
while (iterations > 0) {
if (iterations % 20000 == 0) {
try {
Thread.sleep(20);
} catch (InterruptedException ie) {
}
}
iterations--;
}
if (db) {
Statistics statistics = new Statistics();
statistics.type = Type.CPU;
statistics.parameter = iterations.toString();
statistics.duration = Duration.between(start, Instant.now());
statistics.description = desc;
repository.save(statistics);
}
String msg = "Micronaut: CPU consumption is done with " + iterationsDone + " iterations in " + Duration.between(start, Instant.now()).getNano() + " nano-seconds.";
if (db) {
msg += " The result is persisted in the database.";
}
return msg;
}