2023-01-11 14:17:18 +00:00
|
|
|
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;
|
2023-01-11 14:17:18 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2023-01-28 17:07:24 +00:00
|
|
|
import java.util.concurrent.atomic.LongAdder;
|
2023-01-11 14:17:18 +00:00
|
|
|
|
|
|
|
public final class Profiler {
|
2023-01-28 17:07:24 +00:00
|
|
|
private static final Map<CallInfo, LongAdder> count = new HashMap<>();
|
2023-01-11 14:17:18 +00:00
|
|
|
|
2023-01-28 16:57:59 +00:00
|
|
|
private static final Path outFile = Path.of("ex11.csv");
|
|
|
|
|
2023-01-11 14:17:18 +00:00
|
|
|
private static boolean stop = false;
|
|
|
|
|
|
|
|
static {
|
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
stop = true;
|
2023-01-28 17:07:24 +00:00
|
|
|
try (final OutputStream path = Files.newOutputStream(outFile)) {
|
2023-01-28 16:57:59 +00:00
|
|
|
final CallInfoCSVWriter w = new CallInfoCSVWriter(path);
|
|
|
|
for (final var entry : count.entrySet()) {
|
2023-01-28 17:07:24 +00:00
|
|
|
System.out.println(entry.getKey().toString() + "\n#invokes " + entry.getValue().longValue() + "\n");
|
|
|
|
w.write(entry.getKey(), entry.getValue().longValue());
|
2023-01-28 16:57:59 +00:00
|
|
|
}
|
|
|
|
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);
|
2023-01-11 14:17:18 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
private Profiler() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void registerCall(final CallInfo info) {
|
|
|
|
if (stop) return;
|
2023-01-28 17:07:24 +00:00
|
|
|
count.computeIfAbsent(info, ignored -> new LongAdder()).increment();
|
2023-01-11 14:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|