Skip to content

Commit 7ca8233

Browse files
authored
feat(testing): remove SQLite dependency from inference recorder (#3254)
Recording files use a predictable naming format, making the SQLite index redundant. The binary SQLite file was causing frequent git conflicts. Simplify by calculating file paths directly from request hashes. Signed-off-by: Derek Higgins <[email protected]>
1 parent 1eb1ac0 commit 7ca8233

File tree

3 files changed

+2
-57
lines changed

3 files changed

+2
-57
lines changed

llama_stack/testing/inference_recorder.py

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import hashlib
1010
import json
1111
import os
12-
import sqlite3
1312
from collections.abc import Generator
1413
from contextlib import contextmanager
1514
from enum import StrEnum
@@ -125,28 +124,13 @@ class ResponseStorage:
125124
def __init__(self, test_dir: Path):
126125
self.test_dir = test_dir
127126
self.responses_dir = self.test_dir / "responses"
128-
self.db_path = self.test_dir / "index.sqlite"
129127

130128
self._ensure_directories()
131-
self._init_database()
132129

133130
def _ensure_directories(self):
134131
self.test_dir.mkdir(parents=True, exist_ok=True)
135132
self.responses_dir.mkdir(exist_ok=True)
136133

137-
def _init_database(self):
138-
with sqlite3.connect(self.db_path) as conn:
139-
conn.execute("""
140-
CREATE TABLE IF NOT EXISTS recordings (
141-
request_hash TEXT PRIMARY KEY,
142-
response_file TEXT,
143-
endpoint TEXT,
144-
model TEXT,
145-
timestamp TEXT,
146-
is_streaming BOOLEAN
147-
)
148-
""")
149-
150134
def store_recording(self, request_hash: str, request: dict[str, Any], response: dict[str, Any]):
151135
"""Store a request/response pair."""
152136
# Generate unique response filename
@@ -169,34 +153,9 @@ def store_recording(self, request_hash: str, request: dict[str, Any], response:
169153
f.write("\n")
170154
f.flush()
171155

172-
# Update SQLite index
173-
with sqlite3.connect(self.db_path) as conn:
174-
conn.execute(
175-
"""
176-
INSERT OR REPLACE INTO recordings
177-
(request_hash, response_file, endpoint, model, timestamp, is_streaming)
178-
VALUES (?, ?, ?, ?, datetime('now'), ?)
179-
""",
180-
(
181-
request_hash,
182-
response_file,
183-
request.get("endpoint", ""),
184-
request.get("model", ""),
185-
response.get("is_streaming", False),
186-
),
187-
)
188-
189156
def find_recording(self, request_hash: str) -> dict[str, Any] | None:
190157
"""Find a recorded response by request hash."""
191-
with sqlite3.connect(self.db_path) as conn:
192-
result = conn.execute(
193-
"SELECT response_file FROM recordings WHERE request_hash = ?", (request_hash,)
194-
).fetchone()
195-
196-
if not result:
197-
return None
198-
199-
response_file = result[0]
158+
response_file = f"{request_hash[:12]}.json"
200159
response_path = self.responses_dir / response_file
201160

202161
if not response_path.exists():
-56 KB
Binary file not shown.

tests/unit/distribution/test_inference_recordings.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
# This source code is licensed under the terms described in the LICENSE file in
55
# the root directory of this source tree.
66

7-
import sqlite3
87
import tempfile
98
from pathlib import Path
109
from unittest.mock import patch
@@ -133,7 +132,6 @@ def test_response_storage(self, temp_storage_dir):
133132
# Test directory creation
134133
assert storage.test_dir.exists()
135134
assert storage.responses_dir.exists()
136-
assert storage.db_path.exists()
137135

138136
# Test storing and retrieving a recording
139137
request_hash = "test_hash_123"
@@ -147,15 +145,6 @@ def test_response_storage(self, temp_storage_dir):
147145

148146
storage.store_recording(request_hash, request_data, response_data)
149147

150-
# Verify SQLite record
151-
with sqlite3.connect(storage.db_path) as conn:
152-
result = conn.execute("SELECT * FROM recordings WHERE request_hash = ?", (request_hash,)).fetchone()
153-
154-
assert result is not None
155-
assert result[0] == request_hash # request_hash
156-
assert result[2] == "/v1/chat/completions" # endpoint
157-
assert result[3] == "llama3.2:3b" # model
158-
159148
# Verify file storage and retrieval
160149
retrieved = storage.find_recording(request_hash)
161150
assert retrieved is not None
@@ -185,10 +174,7 @@ async def mock_create(*args, **kwargs):
185174

186175
# Verify recording was stored
187176
storage = ResponseStorage(temp_storage_dir)
188-
with sqlite3.connect(storage.db_path) as conn:
189-
recordings = conn.execute("SELECT COUNT(*) FROM recordings").fetchone()[0]
190-
191-
assert recordings == 1
177+
assert storage.responses_dir.exists()
192178

193179
async def test_replay_mode(self, temp_storage_dir, real_openai_chat_response):
194180
"""Test that replay mode returns stored responses without making real calls."""

0 commit comments

Comments
 (0)