Run matching accuracy tests and measure query latencies
(test_vectors, collection, brute_force_results)
| 699 | return matches_test_vectors, rps_test_vectors |
| 700 | |
| 701 | def run_matching_tests(test_vectors, collection, brute_force_results): |
| 702 | """Run matching accuracy tests and measure query latencies""" |
| 703 | print(f"\nStarting similarity search tests with {len(test_vectors)} queries...") |
| 704 | |
| 705 | total_recall = 0 |
| 706 | total_queries = 0 |
| 707 | latencies = [] |
| 708 | |
| 709 | for i, query_vec in enumerate(test_vectors): |
| 710 | try: |
| 711 | test_vec = next( |
| 712 | test_vec |
| 713 | for test_vec in brute_force_results |
| 714 | if str(query_vec["id"]) == str(test_vec["query_id"]) # Compare as strings |
| 715 | ) |
| 716 | |
| 717 | # Measure query latency |
| 718 | query_start_time = time.time() |
| 719 | results = collection.search.dense( |
| 720 | query_vector=query_vec["dense_values"], |
| 721 | top_k=5, |
| 722 | return_raw_text=True |
| 723 | ) |
| 724 | query_end_time = time.time() |
| 725 | query_latency = (query_end_time - query_start_time) * 1000 # Convert to ms |
| 726 | latencies.append(query_latency) |
| 727 | |
| 728 | if "results" in results: |
| 729 | query_id = test_vec["query_id"] |
| 730 | print(f"\nQuery {i + 1} (Vector ID: {query_id}):") |
| 731 | print(f"Query latency: {query_latency:.2f} ms") |
| 732 | |
| 733 | server_top5 = [str(match["id"]) for match in results["results"][:5]] # Convert to string |
| 734 | print("Server top 5:", server_top5) |
| 735 | |
| 736 | brute_force_top5 = [str(test_vec[f"top{j}_id"]) for j in range(1, 6)] # Convert to string |
| 737 | print("Brute force top 5:", brute_force_top5) |
| 738 | |
| 739 | matches = sum(1 for id in server_top5 if id in brute_force_top5) |
| 740 | recall = (matches / 5) * 100 |
| 741 | total_recall += recall |
| 742 | total_queries += 1 |
| 743 | |
| 744 | print(f"Recall@5 for this query: {recall}% ({matches}/5 matches)") |
| 745 | |
| 746 | time.sleep(0.1) |
| 747 | |
| 748 | except Exception as e: |
| 749 | print(f"Error in query {i + 1}: {e}") |
| 750 | |
| 751 | # Calculate and display latency statistics |
| 752 | if latencies: |
| 753 | latencies.sort() |
| 754 | avg_latency = sum(latencies) / len(latencies) |
| 755 | p50_latency = latencies[int(len(latencies) * 0.5)] |
| 756 | p90_latency = latencies[int(len(latencies) * 0.9)] |
| 757 | p95_latency = latencies[int(len(latencies) * 0.95)] |
| 758 | min_latency = min(latencies) |
no test coverage detected