Search in sources :

Example 1 with Profiler

use of org.neo4j.resources.Profiler in project neo4j by neo4j.

the class ProfilerExtension method afterEach.

@Override
public void afterEach(ExtensionContext context) {
    Profiler profiler = getStoredValue(context);
    try {
        profiler.finish();
        if (context.getExecutionException().isPresent()) {
            String displayName = "Profile: " + context.getTestClass().map(Class::getSimpleName).orElse("class") + "." + context.getDisplayName();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            profiler.printProfile(new PrintStream(buffer, false, StandardCharsets.UTF_8), displayName);
            buffer.writeTo(System.err);
            ExtensionContext.Store testDirStore = getStore(context, TestDirectorySupportExtension.TEST_DIRECTORY_NAMESPACE);
            TestDirectory testDir = testDirStore.get(TestDirectorySupportExtension.TEST_DIRECTORY, TestDirectory.class);
            if (testDir != null && testDir.isInitialised()) {
                Path profileOutputFile = testDir.createFile("profiler-output.txt");
                FileSystemAbstraction fs = testDir.getFileSystem();
                try (OutputStream out = fs.openAsOutputStream(profileOutputFile, false)) {
                    buffer.writeTo(out);
                }
            }
        }
    } catch (Exception e) {
        throw new JUnitException("Failed to finish profiling and/or produce profiling output.", e);
    }
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) FileSystemAbstraction(org.neo4j.io.fs.FileSystemAbstraction) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JUnitException(org.junit.platform.commons.JUnitException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JUnitException(org.junit.platform.commons.JUnitException) TestDirectory(org.neo4j.test.rule.TestDirectory) Profiler(org.neo4j.resources.Profiler) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext)

Example 2 with Profiler

use of org.neo4j.resources.Profiler in project neo4j by neo4j.

the class CentralJobSchedulerTest method shouldProfileGroup.

@Timeout(value = 20, unit = SECONDS)
@Test
void shouldProfileGroup() throws InterruptedException {
    life.start();
    BinaryLatch checkpointLatch = new BinaryLatch();
    scheduler.schedule(Group.CHECKPOINT, NOT_MONITORED, checkpointLatch::await);
    Profiler profiler = Profiler.profiler();
    scheduler.profileGroup(Group.CHECKPOINT, profiler);
    String printedProfile;
    do {
        ByteArrayOutputStream bufferOut = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(bufferOut);
        profiler.printProfile(out, "Test Title");
        out.flush();
        printedProfile = bufferOut.toString();
    } while (!printedProfile.contains("BinaryLatch.await"));
    checkpointLatch.release();
    profiler.finish();
}
Also used : PrintStream(java.io.PrintStream) Profiler(org.neo4j.resources.Profiler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 Profiler (org.neo4j.resources.Profiler)2 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 Test (org.junit.jupiter.api.Test)1 Timeout (org.junit.jupiter.api.Timeout)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 JUnitException (org.junit.platform.commons.JUnitException)1 FileSystemAbstraction (org.neo4j.io.fs.FileSystemAbstraction)1 TestDirectory (org.neo4j.test.rule.TestDirectory)1 BinaryLatch (org.neo4j.util.concurrent.BinaryLatch)1