Skip to content

Commit 340b62f

Browse files
committed
Add some query_namespaces performance tweaks
1 parent b487bb3 commit 340b62f

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

poetry.lock

Lines changed: 32 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ pytest-asyncio = "0.15.1"
8282
pytest-cov = "2.10.1"
8383
pytest-mock = "3.6.1"
8484
pytest-timeout = "2.2.0"
85+
pytest-benchmark = [
86+
{ version = '5.0.0', python = ">=3.9,<4.0" }
87+
]
8588
urllib3_mock = "0.3.3"
8689
responses = ">=0.8.1"
8790
ddtrace = "^2.14.4"
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import time
2+
import random
3+
import pytest
4+
from pinecone import Pinecone
5+
from pinecone.grpc import PineconeGRPC
6+
7+
latencies = []
8+
9+
10+
def call_n_threads(index):
11+
query_vec = [random.random() for i in range(1024)]
12+
start = time.time()
13+
combined_results = index.query_namespaces(
14+
vector=query_vec,
15+
namespaces=["ns1", "ns2", "ns3", "ns4"],
16+
include_values=False,
17+
include_metadata=True,
18+
filter={"publication_date": {"$eq": "Last3Months"}},
19+
top_k=1000,
20+
)
21+
finish = time.time()
22+
# print(f"Query took {finish-start} seconds")
23+
latencies.append(finish - start)
24+
25+
return combined_results
26+
27+
28+
class TestQueryNamespacesRest:
29+
@pytest.mark.parametrize("n_threads", [4])
30+
def test_query_namespaces_grpc(self, benchmark, n_threads):
31+
pc = PineconeGRPC()
32+
index = pc.Index(
33+
host="jen1024-dojoi3u.svc.apw5-4e34-81fa.pinecone.io", pool_threads=n_threads
34+
)
35+
benchmark.pedantic(call_n_threads, (index,), rounds=10, warmup_rounds=1, iterations=5)
36+
37+
@pytest.mark.parametrize("n_threads", [4])
38+
def test_query_namespaces_rest(self, benchmark, n_threads):
39+
pc = Pinecone()
40+
index = pc.Index(
41+
host="jen1024-dojoi3u.svc.apw5-4e34-81fa.pinecone.io",
42+
pool_threads=n_threads,
43+
connection_pool_maxsize=20,
44+
)
45+
benchmark.pedantic(call_n_threads, (index,), rounds=10, warmup_rounds=1, iterations=5)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random
2+
from pinecone.data.query_results_aggregator import QueryResultsAggregator
3+
4+
5+
def fake_results(i):
6+
matches = [
7+
{"id": f"id{i}", "score": random.random(), "values": [random.random() for _ in range(768)]}
8+
for _ in range(1000)
9+
]
10+
matches.sort(key=lambda x: x["score"], reverse=True)
11+
return {"namespace": f"ns{i}", "matches": matches}
12+
13+
14+
def aggregate_results(responses):
15+
ag = QueryResultsAggregator(1000)
16+
for response in responses:
17+
ag.add_results(response)
18+
return ag.get_results()
19+
20+
21+
class TestQueryResultsAggregatorPerf:
22+
def test_my_stuff(self, benchmark):
23+
responses = [fake_results(i) for i in range(10)]
24+
benchmark(aggregate_results, responses)

0 commit comments

Comments
 (0)