diff --git a/Lucene.Net.sln b/Lucene.Net.sln index 389ea1774a..d466723b8d 100644 --- a/Lucene.Net.sln +++ b/Lucene.Net.sln @@ -200,6 +200,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lucene.Net.CodeAnalysis", " EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lucene.Net.Tests.CodeAnalysis", "src\dotnet\Lucene.Net.Tests.CodeAnalysis\Lucene.Net.Tests.CodeAnalysis.csproj", "{158F5D30-8B96-4C49-9009-0B8ACEDF8546}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lucene.Net.Tests.BenchmarkDotNet", "src\Lucene.Net.Tests.BenchmarkDotNet\Lucene.Net.Tests.BenchmarkDotNet.csproj", "{0C476146-411E-4C94-8A59-726A5F982A89}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -462,6 +464,10 @@ Global {158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Debug|Any CPU.Build.0 = Debug|Any CPU {158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Release|Any CPU.ActiveCfg = Release|Any CPU {158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Release|Any CPU.Build.0 = Release|Any CPU + {0C476146-411E-4C94-8A59-726A5F982A89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0C476146-411E-4C94-8A59-726A5F982A89}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0C476146-411E-4C94-8A59-726A5F982A89}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0C476146-411E-4C94-8A59-726A5F982A89}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/Lucene.Net.Tests.BenchmarkDotNet/HomePageScriptBenchmarks.cs b/src/Lucene.Net.Tests.BenchmarkDotNet/HomePageScriptBenchmarks.cs new file mode 100644 index 0000000000..37ac4b2096 --- /dev/null +++ b/src/Lucene.Net.Tests.BenchmarkDotNet/HomePageScriptBenchmarks.cs @@ -0,0 +1,112 @@ +using System; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using BenchmarkDotNet.Jobs; +using Lucene.Net.Util; +using Lucene.Net.Store; +using Lucene.Net.Analysis.Standard; +using Lucene.Net.Index; +using Lucene.Net.Documents; +using Lucene.Net.Search; +using System.Diagnostics; + +namespace Lucene.Net.Tests.BenchmarkDotNet +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + [MemoryDiagnoser] + [Config(typeof(Config))] + public class HomePageScriptBenchmarks + { + private class Config : ManualConfig + { + public Config() + { + var baseJob = Job.MediumRun; + + AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00009").WithId("4.8.0-beta00009")); + AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00008").WithId("4.8.0-beta00008")); + AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00007").WithId("4.8.0-beta00007")); + } + } + + private const int _directoryWriterIterations = 10; + private const int _indexSearchIterations = 25; + + [Benchmark] + public void HomePageScript() + { + // Ensures index backwards compatibility + var AppLuceneVersion = LuceneVersion.LUCENE_48; + + for (int d = 0; d < _directoryWriterIterations; d++) + { + using var dir = new RAMDirectory(); + + //create an analyzer to process the text + var analyzer = new StandardAnalyzer(AppLuceneVersion); + + //create an index writer + var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer); + using var writer = new IndexWriter(dir, indexConfig); + + for (int i = 0; i < _indexSearchIterations; i++) + { + var source = new + { + Name = $"Kermit{i} the Frog{i}", + FavoritePhrase = $"The quick{i} brown{i} fox{i} jumps{i} over{i} the lazy{i} dog{i} " + }; + Document doc = new Document + { + // StringField indexes but doesn't tokenize + new StringField("name", source.Name, Field.Store.YES), + new TextField("favoritePhrase", source.FavoritePhrase, Field.Store.YES) + }; + + writer.AddDocument(doc); + writer.Flush(triggerMerge: false, applyAllDeletes: false); + } + + for (int i = 0; i < _indexSearchIterations; i++) + { + // search with a phrase + var phrase = new MultiPhraseQuery + { + new Term("favoritePhrase", $"brown{i}"), + new Term("favoritePhrase", $"fox{i}") + }; + + // re-use the writer to get real-time updates + using var reader = writer.GetReader(applyAllDeletes: true); + var searcher = new IndexSearcher(reader); + var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs; + Debug.Assert(hits.Length > 0); + foreach (var hit in hits) + { + var foundDoc = searcher.Doc(hit.Doc); + var score = hit.Score; + var name = foundDoc.Get("name"); + var favoritePhrase = foundDoc.Get("favoritePhrase"); + } + } + } + } + + } +} diff --git a/src/Lucene.Net.Tests.BenchmarkDotNet/Lucene.Net.Tests.BenchmarkDotNet.csproj b/src/Lucene.Net.Tests.BenchmarkDotNet/Lucene.Net.Tests.BenchmarkDotNet.csproj new file mode 100644 index 0000000000..5d4af5aea7 --- /dev/null +++ b/src/Lucene.Net.Tests.BenchmarkDotNet/Lucene.Net.Tests.BenchmarkDotNet.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + + + diff --git a/src/Lucene.Net.Tests.BenchmarkDotNet/Program.cs b/src/Lucene.Net.Tests.BenchmarkDotNet/Program.cs new file mode 100644 index 0000000000..c65ec225c1 --- /dev/null +++ b/src/Lucene.Net.Tests.BenchmarkDotNet/Program.cs @@ -0,0 +1,26 @@ +using BenchmarkDotNet.Running; + +namespace Lucene.Net.Tests.BenchmarkDotNet +{ + /* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + class Program + { + private static void Main(string[] args) => new BenchmarkSwitcher(typeof(Program).Assembly).Run(args); + } +}