Skip to content

Commit a39e96b

Browse files
committed
server : add support for external server for tests
This commit adds support for using an externally started llama-server instance for the server tests. This can be enabled by setting the DEBUG_EXTERNAL environment variable. The motivation for this is to allow debugging of the server itself when investigating a test failure. Instructions for how to do this are added to the README.md file in the tests directory.
1 parent e7a5130 commit a39e96b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

tools/server/tests/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,33 @@ cmake --build build -j --target llama-server && ./tools/server/tests/tests.sh
6464
```
6565

6666
To see all available arguments, please refer to [pytest documentation](https://docs.pytest.org/en/stable/how-to/usage.html)
67+
68+
### Debugging external llama-server
69+
It can sometimes be useful to run the server in a debugger when invesigating test
70+
failures. To do this, the environment variable `DEBUG_EXTERNAL=1` can be set
71+
which will cause the test to skip starting a llama-server itself. Instead, the
72+
server can be started in a debugger.
73+
74+
Example using `gdb`:
75+
```console
76+
$ gdb --args ../../../build/bin/llama-server \
77+
--host 127.0.0.1 --port 8080 \
78+
--temp 0.8 --seed 42 \
79+
--hf-repo ggml-org/models --hf-file tinyllamas/stories260K.gguf \
80+
--batch-size 32 --no-slots --alias tinyllama-2 --ctx-size 512 \
81+
--parallel 2 --n-predict 64
82+
```
83+
And a break point can be set in before running:
84+
```console
85+
(gdb) br server.cpp:4604
86+
(gdb) r
87+
main: server is listening on http://127.0.0.1:8080 - starting the main loop
88+
srv update_slots: all slots are idle
89+
```
90+
91+
And then the test in question can be run in another terminal:
92+
```console
93+
(venv) $ env DEBUG_EXTERNAL=1 ./tests.sh unit/test_chat_completion.py -v -x
94+
```
95+
And this should trigger the breakpoint and allow inspection of the server state
96+
in the debugger terminal.

tools/server/tests/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,12 @@ def __init__(self):
9999
self.debug = True
100100
if "PORT" in os.environ:
101101
self.server_port = int(os.environ["PORT"])
102+
self.external_server = "DEBUG_EXTERNAL" in os.environ
102103

103104
def start(self, timeout_seconds: int | None = DEFAULT_HTTP_TIMEOUT) -> None:
105+
if self.external_server:
106+
print(f"[external_server]: Assuming external server running on {self.server_host}:{self.server_port}")
107+
return
104108
if self.server_path is not None:
105109
server_path = self.server_path
106110
elif "LLAMA_SERVER_BIN_PATH" in os.environ:
@@ -244,6 +248,9 @@ def start(self, timeout_seconds: int | None = DEFAULT_HTTP_TIMEOUT) -> None:
244248
raise TimeoutError(f"Server did not start within {timeout_seconds} seconds")
245249

246250
def stop(self) -> None:
251+
if self.external_server:
252+
print("[external_server]: Not stopping external server")
253+
return
247254
if self in server_instances:
248255
server_instances.remove(self)
249256
if self.process:

0 commit comments

Comments
 (0)