This repository has been archived on 2024-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
AJP/DiSLProject2022/src-profiler/ex11/Profiler.java

43 lines
1.4 KiB
Java
Raw Normal View History

package ex11;
2023-01-28 16:57:59 +00:00
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
public final class Profiler {
private static final Map<CallInfo, long[]> count = new HashMap<>();
2023-01-28 16:57:59 +00:00
private static final Path outFile = Path.of("ex11.csv");
private static boolean stop = false;
static {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
stop = true;
2023-01-28 16:57:59 +00:00
try (final OutputStream path = Files.newOutputStream(outFile, StandardOpenOption.WRITE)) {
final CallInfoCSVWriter w = new CallInfoCSVWriter(path);
for (final var entry : count.entrySet()) {
System.out.println(entry.getKey().toString() + "\n#invokes " + entry.getValue()[0] + "\n");
w.write(entry.getKey(), entry.getValue()[0]);
}
System.out.println("Invocations have been written to: " + outFile.toAbsolutePath());
} catch (final IOException e) {
throw new RuntimeException("Writing CSV file with call info failed", e);
}
}));
}
private Profiler() {
}
public static void registerCall(final CallInfo info) {
if (stop) return;
count.computeIfAbsent(info, ignored -> new long[1])[0]++;
}
}