private void TestInner(IndexType indexType, double[] expectedVector3, int[] expectedIdList, double[] expectedDistanceList) { Func <double, double> roundTo = (double v) => Math.Round(v, PRECISION); string? path = $"index.{indexType.ToString().ToLowerInvariant()}.{TEST_INDEX_DIM}d.ann"; IAnnoyIndex?index = AnnoyIndex.Load(path, TEST_INDEX_DIM, indexType); index.Should().NotBeNull(); index.Dimension.Should().Be(TEST_INDEX_DIM); index.Size.Should().Be(TEST_NODE_COUNT); { AnnoyIndexSearchResult?nearest = index.GetNearestToItem(0, 5, -1, true); nearest.IdList.ToArray().Should().BeEquivalentTo(expectedIdList); nearest.DistanceList.ToArray().Select(RoundTo).Should().BeEquivalentTo(expectedDistanceList.Select(RoundTo)); } IReadOnlyList <float>?vector3 = index.GetItemVector(3); vector3.Select(RoundTo).Should().BeEquivalentTo(expectedVector3.Select(RoundTo)); IReadOnlyList <float>?v0 = index.GetItemVector(0); { AnnoyIndexSearchResult?nearest = index.GetNearest(v0, 5, -1, true); nearest.IdList.ToArray().Should().BeEquivalentTo(expectedIdList); nearest.DistanceList.ToArray().Select(RoundTo).Should().BeEquivalentTo(expectedDistanceList.Select(RoundTo)); } { AnnoyIndexSearchResult?neareast = index.GetNearest(v0, 5, -1, false); neareast.IdList.ToArray().Should().BeEquivalentTo(expectedIdList); neareast.DistanceList.Length.Should().Be(0); } }
static void Main(int dim, ulong size, uint nResult, ulong nLoop) { foreach (var metric in new[] { "angular", "euclidean", "manhattan", "dot" }) { var path = $"index.{metric}.{dim}d.ann"; var index = AnnoyIndex.Load(path, dim, Enum.Parse <IndexType>(metric, ignoreCase: true)); var sw = Stopwatch.StartNew(); for (ulong i = 0; i < nLoop; i++) { var id = i % size; var vector = index.GetItemVector(id); index.GetNearest(vector, nResult, -1, true); } sw.Stop(); Console.WriteLine($"[Dotnet] RuAnnoy"); Console.WriteLine($"[{metric}] Total time elapsed: {sw.Elapsed.TotalSeconds}s"); Console.WriteLine($"[{metric}] Avg time elapsed: {sw.ElapsedMilliseconds/(float)nLoop}ms"); Console.WriteLine(); } }
public void TestInvalidIndex() { IAnnoyIndex?index = AnnoyIndex.Load("invalid.ann", 5, IndexType.Euclidean); index.Should().BeNull(); }