Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit 6f8909c

Browse files
Add some code examples using @simonswine repo.
Co-authored-by: Christian Simon <[email protected]>
1 parent 6830eb1 commit 6f8909c

File tree

14 files changed

+1396
-3
lines changed

14 files changed

+1396
-3
lines changed

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
We've compiled a set of examples using [docker-compose](./tools/docker-compose/) to help you get started with your language of choice.

tools/docker-compose/docker-compose.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,39 @@ services:
1515
- phlare
1616

1717
grafana:
18-
image: aocenas/grafana:profiling-ds
18+
image: aocenas/grafana:profiling-ds-2@sha256:34e6cf8c315c8efea3368cdadbc448d671f791df02a8dbb75f7376e323cb3dab
1919
ports:
2020
- "3000:3000"
2121
environment:
2222
- GF_FEATURE_TOGGLES_ENABLE=flameGraph
2323
- GF_AUTH_ANONYMOUS_ENABLED=true
2424
- GF_AUTH_ANONYMOUS_ORG_ROLE=Admin
25+
- GF_DIAGNOSTICS_PROFILING_ENABLED=true
26+
- GF_DIAGNOSTICS_PROFILING_ADDR=0.0.0.0
27+
- GF_DIAGNOSTICS_PROFILING_PORT=6060
2528
volumes:
2629
- ./datasource.yaml:/etc/grafana/provisioning/datasources/datasources.yml
2730
networks:
2831
- phlare
2932

33+
python:
34+
build:
35+
context: python
36+
dockerfile: Dockerfile
37+
ports:
38+
- 8081:8081
39+
networks:
40+
- phlare
41+
rust:
42+
build:
43+
context: rust
44+
dockerfile: Dockerfile
45+
ports:
46+
- 8080:3000
47+
networks:
48+
- phlare
49+
3050
volumes:
3151
data:
3252

33-
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json
53+
# yaml-language-server: $schema=https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json

tools/docker-compose/phlare.yaml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
scrape_configs:
2-
- job_name: "default"
2+
- job_name: "phlare"
33
scrape_interval: "15s"
44
static_configs:
55
- targets: ["phlare:4100"]
6+
- job_name: "grafana"
7+
scrape_interval: "15s"
8+
static_configs:
9+
- targets: ["grafana:6060"]
10+
- job_name: "cp-rust"
11+
scrape_interval: "15s"
12+
static_configs:
13+
- targets: ["rust:3000"]
14+
profiling_config:
15+
pprof_config:
16+
block: { enabled: false }
17+
goroutine: { enabled: false }
18+
memory: { enabled: false }
19+
mutex: { enabled: false }
20+
- job_name: "cp-python"
21+
scrape_interval: "15s"
22+
static_configs:
23+
- targets: ["python:8081"]
24+
profiling_config:
25+
pprof_config:
26+
block: { enabled: false }
27+
goroutine: { enabled: false }
28+
mutex: { enabled: false }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM python:3.10 as builder
2+
3+
# Install build-base to allow for compilation of the profiling agent.
4+
RUN apt-get update && apt-get install -y build-essential git
5+
6+
# Compile the profiling agent, generating wheels for it.
7+
COPY requirements.txt .
8+
RUN pip3 wheel --wheel-dir=/tmp/wheels -r requirements.txt
9+
10+
FROM python:3.10
11+
12+
# Copy over the directory containing wheels for the profiling agent.
13+
COPY --from=builder /tmp/wheels /tmp/wheels
14+
RUN ls -al
15+
16+
RUN apt-get update && apt-get install -y git
17+
18+
# Install the profiling agent.
19+
COPY requirements.txt .
20+
COPY main.py .
21+
22+
RUN pip3 install --no-index --find-links=/tmp/wheels -r requirements.txt
23+
24+
25+
# Run the application when the docker image is run, using either CMD (as is done
26+
# here) or ENTRYPOINT.
27+
CMD python3 -u main.py
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Continuous Profiling for Python
2+
3+
The use of [zprofile] requires to build the wheel for the particular libraries shipped, locally refer to the [Dockerfile] how to achieve this.
4+
5+
## Run backend
6+
7+
```shell
8+
$ docker build -t cp-python .
9+
$ docker run -p 8081:8081 cp-python
10+
```
11+
12+
## Collect profiles
13+
14+
```shell
15+
$ pprof -http :6060 "http://localhost:8081/debug/pprof/profile?seconds=1"
16+
```
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import time
2+
3+
# import profiling modules
4+
from pypprof.net_http import start_pprof_server
5+
import mprofile
6+
7+
def prime_number_from_1_to(to=100):
8+
prime_numbers = []
9+
for i in range(1, to):
10+
if i > 1:
11+
for j in range(2, i):
12+
if divisible(i, j):
13+
break
14+
else:
15+
prime_numbers.append(i)
16+
return prime_numbers
17+
18+
def divisible(i, j):
19+
return i % j == 0
20+
21+
22+
def main():
23+
# start memory profiling
24+
mprofile.start(sample_rate=128 * 1024)
25+
26+
# enable pprof http server
27+
start_pprof_server(host='0.0.0.0', port=8081)
28+
29+
to = 500
30+
while True:
31+
result = prime_number_from_1_to(to)
32+
print("there are %d prime numbers from 1 to %d" % (len(result), to))
33+
time.sleep(0.5)
34+
35+
if __name__ == "__main__":
36+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mprofile==0.0.14
2+
protobuf==3.20.3
3+
git+https://github.com/simonswine/pypprof.git@a5b539b839f9eb91be9277ae22c1698eac9e5ce8#egg=pypprof
4+
six==1.16.0
5+
git+https://github.com/simonswine/zprofile.git@100934da015661b3baf7c7c45f68293546ba77f5#egg=zprofile
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
/.git/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

0 commit comments

Comments
 (0)