Skip to content

Commit

Permalink
Merge pull request #173 from grafana/patch/multiple-library-deployments
Browse files Browse the repository at this point in the history
Prevent multiple async-profiler library deployments
  • Loading branch information
aleks-p authored Dec 4, 2024
2 parents e102ce2 + 367ecc0 commit ee7458a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ public static AsyncProfiler getAsyncProfiler() {
private static String deployLibrary() throws IOException {
final String fileName = libraryFileName();
final String userName = System.getProperty("user.name");
final String tmpDir = System.getProperty("java.io.tmpdir");
final File targetDir = new File(tmpDir, userName + "-pyroscope/");
targetDir.mkdirs();
final Path targetDir = Files.createTempDirectory( userName + "-pyroscope");

try (final InputStream is = loadResource(fileName)) {
final Path target = targetDir.toPath().resolve(targetLibraryFileName(fileName)).toAbsolutePath();
final Path target = targetDir.resolve(targetLibraryFileName(fileName)).toAbsolutePath();
Files.copy(is, target, StandardCopyOption.REPLACE_EXISTING);
return target.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.pyroscope;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ConcurrentUsageTest {

@Test
public void runConcurrently() throws IOException {
int iterations = 10;
List<Process> processes = new ArrayList<>();
for (int i = 0; i < iterations; i++) {
Process process = new ProcessBuilder(
"java", "-cp", System.getProperty("java.class.path"), TestApplication.class.getName()
).inheritIO().start();
processes.add(process);
}

processes.parallelStream().forEach(p -> {
int exitCode = 0;
try {
exitCode = p.waitFor();
} catch (InterruptedException e) {
Assertions.fail("could not get process status", e);
}
if (exitCode != 0) {
Assertions.fail("process failed with exit code: " + exitCode);
}
});
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.pyroscope;

import io.pyroscope.labels.io.pyroscope.PyroscopeAsyncProfiler;
import one.profiler.AsyncProfiler;
import one.profiler.Counter;

import java.util.concurrent.TimeUnit;

public class TestApplication {

public static void main(String[] args) {
AsyncProfiler asyncProfiler = PyroscopeAsyncProfiler.getAsyncProfiler();
asyncProfiler.start("cpu", TimeUnit.SECONDS.toNanos(1));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.exit(1);
}
asyncProfiler.stop();

System.out.println(
asyncProfiler + "-" +
asyncProfiler.dumpCollapsed(Counter.SAMPLES).split(";").length
);
}
}

0 comments on commit ee7458a

Please sign in to comment.