Skip to content

Commit

Permalink
Merge pull request #6 from taboola/allow-specifying-interval-time-unit
Browse files Browse the repository at this point in the history
allow specifying sampling interval time unit
  • Loading branch information
sternr authored Feb 2, 2022
2 parents d9fc8da + ef0e89b commit c32a5a9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.taboola</groupId>
<artifactId>async-profiler-actuator-endpoint</artifactId>
<version>0.0.2</version>
<version>1.0.0</version>

<name>${project.groupId}:${project.artifactId}</name>
<description>Async Profiler Actuator Endpoint</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private Integer getInterval(ProfileRequest profileRequest) {
if (profileRequest.getEventType().equals(Events.ALLOC)) {
interval = profileRequest.getSamplingIntervalBytes();
} else {
if (profileRequest.getSamplingIntervalMs() != null) {
interval = Math.toIntExact(TimeUnit.MILLISECONDS.toNanos(profileRequest.getSamplingIntervalMs())); //interval should be in nanos
if (profileRequest.getSamplingInterval() != null) {
interval = Math.toIntExact(profileRequest.getSamplingIntervalTimeUnit().toNanos(profileRequest.getSamplingInterval())); //interval should be in nanos
}
}

Expand All @@ -105,7 +105,7 @@ private void validate(ProfileRequest profileRequest, String filePath) {
throw new IllegalArgumentException("Profiling duration must be greater than 0");
}

if (profileRequest.getSamplingIntervalMs() != null && profileRequest.getSamplingIntervalMs() <= 0) {
if (profileRequest.getSamplingInterval() != null && profileRequest.getSamplingInterval() <= 0) {
throw new IllegalArgumentException("Sampling interval must be greater than 0");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

import lombok.Data;

import java.util.concurrent.TimeUnit;

@Data
public class ProfileRequest {

private int durationSeconds = 60;//profiling duration
private int frameBufferSize = 5_000_000;
private Integer samplingIntervalMs = 1;//1ms default, relevant only for non alloc events.
private Integer samplingInterval = 1;
private TimeUnit samplingIntervalTimeUnit = TimeUnit.MILLISECONDS;
private Integer samplingIntervalBytes = 10_000_000;//relevant only for alloc event.
private boolean separateThreads = false;
private String eventType = Events.CPU;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;

Expand All @@ -20,20 +22,34 @@ public void testCreateStartCommand() {
ProfileRequest profileRequest = new ProfileRequest();
profileRequest.setEventType("cpu");
profileRequest.setFormat("flamegraph");
profileRequest.setSamplingIntervalMs(1);
profileRequest.setSamplingInterval(1);
profileRequest.setFrameBufferSize(10);
String command = commandFactory.createStartCommand(profileRequest, file);

assertEquals("start,event=cpu,file=f,flamegraph,interval=1000000,framebuf=10", command);
}

@Test
public void testCreateStartCommandWithTimeUnit() {
String file = "f";
ProfileRequest profileRequest = new ProfileRequest();
profileRequest.setEventType("cpu");
profileRequest.setFormat("flamegraph");
profileRequest.setSamplingInterval(1);
profileRequest.setSamplingIntervalTimeUnit(TimeUnit.NANOSECONDS);
profileRequest.setFrameBufferSize(10);
String command = commandFactory.createStartCommand(profileRequest, file);

assertEquals("start,event=cpu,file=f,flamegraph,interval=1,framebuf=10", command);
}

@Test
public void testCreateStartCommand_whenHasIncludedThreads_shouldAddFilterFlag() {
String file = "f";
ProfileRequest profileRequest = new ProfileRequest();
profileRequest.setEventType("cpu");
profileRequest.setFormat("flamegraph");
profileRequest.setSamplingIntervalMs(1);
profileRequest.setSamplingInterval(1);
profileRequest.setFrameBufferSize(10);
profileRequest.setIncludedThreads("a");
String command = commandFactory.createStartCommand(profileRequest, file);
Expand All @@ -47,7 +63,7 @@ public void testCreateStartCommand_whenEventIsAlloc_intervalShouldBeTakenFromInt
ProfileRequest profileRequest = new ProfileRequest();
profileRequest.setEventType("alloc");
profileRequest.setFormat("flamegraph");
profileRequest.setSamplingIntervalMs(1);
profileRequest.setSamplingInterval(1);
profileRequest.setSamplingIntervalBytes(2);
profileRequest.setFrameBufferSize(10);
String command = commandFactory.createStartCommand(profileRequest, file);
Expand All @@ -73,7 +89,7 @@ public void testCreateStartCommand_whenValidationFails_shouldThrow() {
assertThrows("Profiling duration must be greater than 0", IllegalArgumentException.class, () -> commandFactory.createStartCommand(finalProfileRequest2, "a"));

profileRequest = new ProfileRequest();
profileRequest.setSamplingIntervalMs(-1);
profileRequest.setSamplingInterval(-1);
ProfileRequest finalProfileRequest3 = profileRequest;
assertThrows("Sampling interval must be greater than 0", IllegalArgumentException.class, () -> commandFactory.createStartCommand(finalProfileRequest3, "a"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void testDefaultValues() {

assertEquals(60, profileRequest.getDurationSeconds());
assertEquals(5_000_000, profileRequest.getFrameBufferSize());
assertEquals(1, profileRequest.getSamplingIntervalMs().intValue());
assertEquals(1, profileRequest.getSamplingInterval().intValue());
assertEquals(10_000_000, profileRequest.getSamplingIntervalBytes().intValue());
assertEquals("cpu", profileRequest.getEventType());
assertEquals("svg", profileRequest.getFormat());
Expand Down

0 comments on commit c32a5a9

Please sign in to comment.