Skip to content

Commit

Permalink
docs: Add new file for Pprof tutorial.
Browse files Browse the repository at this point in the history
  • Loading branch information
TAM360 committed Sep 13, 2024
1 parent d26664f commit 9b7d813
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions docs/book/src/cronjob-tutorial/pprof-tutorial.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Using Pprof for Controller Profiling

Pprof is a Go based library designed for profiling the binary in order to gather CPU, and Memory related data. Pprof is helpful in finding any sort of bottlenecks that are appearing either at Memory, CPU, or both levels.

Pprof itself runs as part of the the Controller. You can extract the statistics, and visualize them on an interactive website. Furthermore, **you don't need to install this package seperately**. Pprof comes packaged as part of Kubebuilder `controller-runtime` [dependency](https://github.com/kubernetes-sigs/controller-runtime/pull/1943) within the scafolded project.

For further information about the Pprof usage, check out the official [docs](https://github.com/google/pprof).

# How to use Pprof in your Codebase

1. In your `cmd/main.go` file, specify the `PprofBindAddress` URL field as part of Controller Manager's configurations:

```golang
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
....
Scheme: scheme,
LeaderElection: enableLeaderElection,
// PprofBindAddress is the TCP address that the controller should bind to
// for serving pprof. Specify the manager address and the port that should be bind.
PprofBindAddress: "127.0.0.1:8082",
...
})
```

2. Reinstall the Controller binary.

```bash
make install
```

3. Start your controller on your localhost:

```bash
export ENABLE_WEBHOOKS=false # If you're not using a Webhook. Else, set it to true.
make run
```

4. Deploy your custom resource on Kind, or any other Kubernetes cluster. Wait for the pods to be ready.

```bash
kubectl apply -f batch_v1_cronjob.yaml
```

5. Use `curl` to export the profiling statistics into a file. Note that the **URL hit should be same as the one specified in the `PprofBindAddress` field of your Controller's Manager**.
```bash
curl -s "http://127.0.0.1:8082/debug/pprof/profile" > ./cpu-profile.out
```
6. Visualize the file on an interactive dashboard.
```bash
# Go tool will open a session on port 8080.
# You can change this as per your own need.
go tool pprof -http=:8080 ./cpu-profile.out
```
Visualizaion resutl will vary depending on the deployed workload, and the Controller's behavior.
However, you'll see a dashboard on your browser similar to this one:
![pprof-result-visualization](./images/pprof-result-visualization.png)

0 comments on commit 9b7d813

Please sign in to comment.