Skip to content

Commit 50d1aba

Browse files
committed
add focused profiling
1 parent 35dbde1 commit 50d1aba

19 files changed

Lines changed: 375 additions & 46 deletions

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ else
1515
OS_TAG=linux
1616
endif
1717

18-
LIB_VERSION=1.0.1
18+
LIB_VERSION=1.0.2
1919
LIB_NAME=libstackimpact-$(LIB_VERSION)-$(OS_TAG)-x64.so
2020

2121

2222
.PHONY: test build clean
2323

2424
build: src/*.cpp src/*.h
2525
mkdir -p build
26+
rm -f build/libstackimpact-*-$(OS_TAG)-x64.so
2627
$(CPP) $(CPPFLAGS) $(INCLUDES) -fPIC -shared -o build/$(LIB_NAME) src/*.cpp -ldl -lpthread
2728

2829
test: build

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,26 @@ The agent can be configured by setting initialization options using the followin
8383
* `StackImpact.setAppVersion(String appVersion)` (Optional) Sets application version, which can be used to associate profiling information with the source code release.
8484
* `StackImpact.setAppEnvironment(String appEnvironment)` (Optional) Used to differentiate applications in different environments.
8585
* `StackImpact.setHostName(String hostName)` (Optional) By default, host name will be the OS hostname.
86+
* `StackImpact.setAutoProfilingMode(boolean isAutoProfilingMode)` (Optional) If set to `false`, disables automatic profiling and reporting. Focused profiling should be used instead. Useful for environments without support for timers or background tasks.
8687
* `StackImpact.setDebugMode(boolean isDebugMode)` (Optional) Enables debug logging.
8788
* `StackImpact.setCPUProfilerDisabled(boolean isDisabled)`, `setLockProfilerDisabled(boolean isDisabled)` (Optional) Disables respective profiler when `true`.
8889

8990

91+
#### Focused profiling
92+
93+
Use `StackImpact.profile()` to instruct the agent when to start and stop profiling. The agent decides if and which profiler is activated. Normally, this method should be used in repeating code, such as request or event handlers. Usage example:
94+
95+
```java
96+
ProfileSpan span = StackImpact.profile();
97+
98+
// your code here
99+
100+
span.stop();
101+
```
102+
103+
Import `ProfileSpan` object from `com.stackimpact.agent.ProfileSpan`.
104+
105+
90106
#### Shutting down the agent
91107
*Optional*
92108

dist/stackimpact.jar

-512 KB
Binary file not shown.

examples/aws-lambda/Hello.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package examples;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.io.OutputStream;
6+
import java.io.InputStreamReader;
7+
import java.io.OutputStreamWriter;
8+
import java.io.BufferedReader;
9+
import java.io.Writer;
10+
import java.util.Random;
11+
12+
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
13+
import com.amazonaws.services.lambda.runtime.Context;
14+
15+
16+
import org.json.simple.JSONObject;
17+
import org.json.simple.JSONArray;
18+
import org.json.simple.parser.ParseException;
19+
import org.json.simple.parser.JSONParser;
20+
21+
import com.stackimpact.agent.StackImpact;
22+
import com.stackimpact.agent.ProfileSpan;
23+
24+
25+
public class Hello implements RequestStreamHandler {
26+
JSONParser parser = new JSONParser();
27+
28+
29+
// initialize StackImpact agent
30+
static {
31+
StackImpact.setAutoProfilingMode(false);
32+
StackImpact.start("agent key here", "LambdaJavaApp");
33+
}
34+
35+
36+
public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
37+
ProfileSpan span = StackImpact.profile();
38+
39+
// simulate CPU work
40+
try {
41+
Random rand = new Random();
42+
for (int i = 0; i < 20 * 1000000; i++) {
43+
rand.nextInt(100000);
44+
}
45+
}
46+
catch(Exception ex) {
47+
}
48+
// end CPU work
49+
50+
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
51+
JSONObject responseJson = new JSONObject();
52+
String responseCode = "200";
53+
54+
try {
55+
JSONObject event = (JSONObject)parser.parse(reader);
56+
57+
JSONObject responseBody = new JSONObject();
58+
responseBody.put("input", event.toJSONString());
59+
responseBody.put("message", "Hello");
60+
61+
JSONObject headerJson = new JSONObject();
62+
headerJson.put("x-custom-header", "my custom header value");
63+
64+
responseJson.put("isBase64Encoded", false);
65+
responseJson.put("statusCode", responseCode);
66+
responseJson.put("headers", headerJson);
67+
responseJson.put("body", responseBody.toString());
68+
69+
} catch(ParseException pex) {
70+
responseJson.put("statusCode", "400");
71+
responseJson.put("exception", pex);
72+
}
73+
74+
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
75+
writer.write(responseJson.toJSONString());
76+
writer.close();
77+
78+
span.stop();
79+
}
80+
}
81+

examples/demo/DemoApp$1.class

0 Bytes
Binary file not shown.

examples/demo/DemoApp.class

8 Bytes
Binary file not shown.

examples/focused/BasicApp.class

621 Bytes
Binary file not shown.

examples/focused/SampleApp.class

1 KB
Binary file not shown.

examples/focused/SampleApp.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import java.util.Random;
3+
4+
import com.stackimpact.agent.StackImpact;
5+
import com.stackimpact.agent.ProfileSpan;
6+
7+
8+
public class SampleApp {
9+
public static void main(String args[]) throws Exception {
10+
StackImpact.start("agent key here", "SampleJavaApp");
11+
12+
while(true) {
13+
someRequestHandler();
14+
}
15+
}
16+
17+
18+
public static void someRequestHandler() {
19+
ProfileSpan span = StackImpact.profile();
20+
21+
try {
22+
Random rand = new Random();
23+
for (int i = 0; i < 20 * 1000000; i++) {
24+
rand.nextInt(100000);
25+
}
26+
Thread.sleep(rand.nextInt(200));
27+
}
28+
catch(Exception ex) {
29+
ex.printStackTrace();
30+
}
31+
32+
span.stop();
33+
}
34+
}

examples/focused/run.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
rm -f /tmp/libstackimpact-*
6+
$JAVA_HOME/bin/javac -cp ../../dist/stackimpact.jar SampleApp.java
7+
$JAVA_HOME/bin/java -cp ../../dist/stackimpact.jar:. SampleApp

0 commit comments

Comments
 (0)