Skip to content

Commit

Permalink
extracted ProfilerDelegate interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kcrimson committed Dec 23, 2023
1 parent fa45625 commit 2444919
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import java.time.Duration;
import java.time.Instant;

public final class Profiler {

public final class AsyncProfilerDelegate implements ProfilerDelegate {
private Config config;
private EventType eventType;
private String alloc;
Expand All @@ -26,7 +27,7 @@ public final class Profiler {

private final AsyncProfiler instance = PyroscopeAsyncProfiler.getAsyncProfiler();

Profiler(Config config) {
AsyncProfilerDelegate(Config config) {
setConfig(config);
}

Expand Down
15 changes: 15 additions & 0 deletions agent/src/main/java/io/pyroscope/javaagent/ProfilerDelegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.pyroscope.javaagent;

import io.pyroscope.javaagent.config.Config;

import java.time.Instant;

public interface ProfilerDelegate {
void start();

void stop();

Snapshot dumpProfile(Instant profilingStartTime, Instant now);

void setConfig(Config config);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static class Options {
final Config config;
final ProfilingScheduler scheduler;
final Logger logger;
final Profiler profiler;
final ProfilerDelegate profiler;

private Options(Builder b) {
this.config = b.config;
Expand All @@ -75,14 +75,14 @@ private Options(Builder b) {

public static class Builder {
final Config config;
final Profiler profiler;
final ProfilerDelegate profiler;
Exporter exporter;
ProfilingScheduler scheduler;
Logger logger;

public Builder(Config config) {
this.config = config;
this.profiler = new Profiler(config);
this.profiler = new AsyncProfilerDelegate(config);
}

public Builder setExporter(Exporter exporter) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.pyroscope.javaagent.api;

import io.pyroscope.javaagent.Profiler;
import io.pyroscope.javaagent.AsyncProfilerDelegate;
import io.pyroscope.javaagent.ProfilerDelegate;

import java.time.Instant;

Expand All @@ -9,13 +10,13 @@
*/
public interface ProfilingScheduler {
/**
* Use Profiler's to start, stop, dumpProfile
* {@link Profiler#start()}
* {@link Profiler#stop()}
* {@link Profiler#dumpProfile(Instant, Instant)}
* Use AsyncProfilerDelegate's to start, stop, dumpProfile
* {@link AsyncProfilerDelegate#start()}
* {@link AsyncProfilerDelegate#stop()}
* {@link AsyncProfilerDelegate#dumpProfile(Instant, Instant)}
* Here is an example of naive implementation
* <pre>
* public void start(Profiler profiler) {
* public void start(AsyncProfilerDelegate profiler) {
* new Thread(() -&#062; {
* while (true) {
* Instant startTime = Instant.now();
Expand All @@ -35,5 +36,5 @@ public interface ProfilingScheduler {
* Github issue #40</a> for more details.
*
**/
void start(Profiler profiler);
void start(ProfilerDelegate profiler);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.pyroscope.javaagent.impl;

import io.pyroscope.javaagent.Profiler;
import io.pyroscope.javaagent.AsyncProfilerDelegate;
import io.pyroscope.javaagent.ProfilerDelegate;
import io.pyroscope.javaagent.Snapshot;
import io.pyroscope.javaagent.api.Exporter;
import io.pyroscope.javaagent.api.Logger;
Expand Down Expand Up @@ -37,7 +38,7 @@ public ContinuousProfilingScheduler(Config config, Exporter exporter, Logger log
}

@Override
public void start(Profiler profiler) {
public void start(ProfilerDelegate profiler) {
Duration firstProfilingDuration;
try {
firstProfilingDuration = startFirst(profiler);
Expand Down Expand Up @@ -81,7 +82,7 @@ private void stop() {
*
* @return Duration of the first profiling interval
*/
private Duration startFirst(Profiler profiler) {
private Duration startFirst(ProfilerDelegate profiler) {
Instant now = Instant.now();

long uploadIntervalMillis = config.uploadInterval.toMillis();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.pyroscope.javaagent.impl;


import io.pyroscope.javaagent.AsyncProfilerDelegate;
import io.pyroscope.javaagent.EventType;
import io.pyroscope.javaagent.Profiler;
import io.pyroscope.javaagent.ProfilerDelegate;
import io.pyroscope.javaagent.Snapshot;
import io.pyroscope.javaagent.api.Exporter;
import io.pyroscope.javaagent.api.Logger;
Expand Down Expand Up @@ -45,7 +46,7 @@ public SamplingProfilingScheduler(Config config, Exporter exporter, Logger logge
}

@Override
public void start(Profiler profiler) {
public void start(ProfilerDelegate profiler) {
final long samplingDurationMillis = config.samplingDuration.toMillis();
final Duration uploadInterval = config.uploadInterval;

Expand All @@ -70,7 +71,7 @@ public void start(Profiler profiler) {
);
}

private void dumpProfile(final Profiler profiler, final long samplingDurationMillis, final Duration uploadInterval) {
private void dumpProfile(final ProfilerDelegate profiler, final long samplingDurationMillis, final Duration uploadInterval) {
Instant profilingStartTime = Instant.now();
try {
profiler.start();
Expand Down

0 comments on commit 2444919

Please sign in to comment.