Skip to content

Commit

Permalink
docs: add performance docs
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Aug 26, 2024
1 parent f5bec5c commit b056525
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ php_server [<matcher>] {
split_path <delim...> # Sets the substrings for splitting the URI into two parts. The first matching substring will be used to split the "path info" from the path. The first piece is suffixed with the matching substring and will be assumed as the actual resource (CGI script) name. The second piece will be set to PATH_INFO for the script to use. Default: `.php`
resolve_root_symlink false # Disables resolving the `root` directory to its actual value by evaluating a symbolic link, if one exists (enabled by default).
env <key> <value> # Sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
file_server off # Disables the built-in file_server directive.
}
```

Expand Down
68 changes: 68 additions & 0 deletions docs/performance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Performance

By default, FrankenPHP tries to offer a good compromise between performance and ease of use.
However, it is possible to slightly improve performance using the appropriate configuration.

## Number of Threads and of Workers

By default, FrankenPHP starts 2 times more threads and workers (in worker mode) than the available numbers of CPU.

The appropriate values depend heavily on how your application is written, what it does and your hardware.
We strongly recommend to change these values.

To find the right values, it's best to try out different values and run load tests simulating real traffic.
[k6](https://k6.io) and [Gatling](https://gatling.io) are good tools for this.

## Worker Mode

Enabling [the worker mode](worker.md) will dramatically improve the performance,
but your app must be adapted to be comptible with this mode:
you need to create a worker script and to be sure that the app is not leaking memory.

## Go Runtime Configuration

FrankenPHP is written and Go.

In general, the Go runtime doesn't require any special configuration, but in certain circumstances it can be helped to perform better.

You likely want to set the `GODEBUG` environement variable to `cgocheck=0` (the default in the FrankenPHP Docker images).

If you run FrankenPHP in containers (Docker, Kubernetes, LXC...) and limit the memory available for the containers,
set the `GOMEMLIMIT` environement variable to the available amount of memory.

For more details, [the Go documentation page dedicated to this subject](https://pkg.go.dev/runtime#hdr-Environment_Variables) is a must-read to get the most out of the runtime.

## `file_server`

By default, the `php_server` directive automatically sets up a file server to
serve static files (assets) stored in the root directory.

This feature is convenient, but comes with a cost.
To disable it, use the following config:

```caddyfile
php_server {
file_server off
}
```

## Placeholders

You can use [placeholders](https://caddyserver.com/docs/conventions#placeholders) in the `root` and `env` directives.
However, this prevent caching these values, and comes with a significant performance cost.

If possible, avoid placeholders in these directives.

## `resolve_root_symlink`

By default, if the document root is a symbolic link, it is automatically resolved by FrankenPHP (this is needed by PHP).
If the document root is not a symlink, you can disable this feature.

```caddyfile
php_server {
resolve_root_symlink false
}
```

This will improve performance if the `root` directive contains [placeholders](https://caddyserver.com/docs/conventions#placeholders). The gain will be negilible in other cases.

5 changes: 4 additions & 1 deletion testdata/benchmark.Caddyfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ http:// {
root * .

encode zstd br gzip
php_server
php_server {
file_server off
resolve_root_symlink false
}
}
}

0 comments on commit b056525

Please sign in to comment.