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