Skip to content

Commit 573f301

Browse files
committed
Adds simple benchmark between nuget versions
1 parent 081edee commit 573f301

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed

Lucene.Net.sln

+6
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lucene.Net.CodeAnalysis", "
200200
EndProject
201201
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}"
202202
EndProject
203+
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}"
204+
EndProject
203205
Global
204206
GlobalSection(SolutionConfigurationPlatforms) = preSolution
205207
Debug|Any CPU = Debug|Any CPU
@@ -462,6 +464,10 @@ Global
462464
{158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Debug|Any CPU.Build.0 = Debug|Any CPU
463465
{158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Release|Any CPU.ActiveCfg = Release|Any CPU
464466
{158F5D30-8B96-4C49-9009-0B8ACEDF8546}.Release|Any CPU.Build.0 = Release|Any CPU
467+
{0C476146-411E-4C94-8A59-726A5F982A89}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
468+
{0C476146-411E-4C94-8A59-726A5F982A89}.Debug|Any CPU.Build.0 = Debug|Any CPU
469+
{0C476146-411E-4C94-8A59-726A5F982A89}.Release|Any CPU.ActiveCfg = Release|Any CPU
470+
{0C476146-411E-4C94-8A59-726A5F982A89}.Release|Any CPU.Build.0 = Release|Any CPU
465471
EndGlobalSection
466472
GlobalSection(SolutionProperties) = preSolution
467473
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using BenchmarkDotNet.Attributes;
3+
using BenchmarkDotNet.Configs;
4+
using BenchmarkDotNet.Jobs;
5+
using Lucene.Net.Util;
6+
using Lucene.Net.Store;
7+
using Lucene.Net.Analysis.Standard;
8+
using Lucene.Net.Index;
9+
using Lucene.Net.Documents;
10+
using Lucene.Net.Search;
11+
using System.Diagnostics;
12+
13+
namespace Lucene.Net.Tests.BenchmarkDotNet
14+
{
15+
/*
16+
* Licensed to the Apache Software Foundation (ASF) under one or more
17+
* contributor license agreements. See the NOTICE file distributed with
18+
* this work for additional information regarding copyright ownership.
19+
* The ASF licenses this file to You under the Apache License, Version 2.0
20+
* (the "License"); you may not use this file except in compliance with
21+
* the License. You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
32+
[MemoryDiagnoser]
33+
[Config(typeof(Config))]
34+
public class HomePageScriptBenchmarks
35+
{
36+
private class Config : ManualConfig
37+
{
38+
public Config()
39+
{
40+
var baseJob = Job.MediumRun;
41+
42+
AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00009").WithId("4.8.0-beta00009"));
43+
AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00008").WithId("4.8.0-beta00008"));
44+
AddJob(baseJob.WithNuGet("Lucene.Net.Analysis.Common", "4.8.0-beta00007").WithId("4.8.0-beta00007"));
45+
}
46+
}
47+
48+
private const int _directoryWriterIterations = 10;
49+
private const int _indexSearchIterations = 25;
50+
51+
[Benchmark]
52+
public void HomePageScript()
53+
{
54+
// Ensures index backwards compatibility
55+
var AppLuceneVersion = LuceneVersion.LUCENE_48;
56+
57+
for (int d = 0; d < _directoryWriterIterations; d++)
58+
{
59+
using var dir = new RAMDirectory();
60+
61+
//create an analyzer to process the text
62+
var analyzer = new StandardAnalyzer(AppLuceneVersion);
63+
64+
//create an index writer
65+
var indexConfig = new IndexWriterConfig(AppLuceneVersion, analyzer);
66+
using var writer = new IndexWriter(dir, indexConfig);
67+
68+
for (int i = 0; i < _indexSearchIterations; i++)
69+
{
70+
var source = new
71+
{
72+
Name = $"Kermit{i} the Frog{i}",
73+
FavoritePhrase = $"The quick{i} brown{i} fox{i} jumps{i} over{i} the lazy{i} dog{i} "
74+
};
75+
Document doc = new Document
76+
{
77+
// StringField indexes but doesn't tokenize
78+
new StringField("name", source.Name, Field.Store.YES),
79+
new TextField("favoritePhrase", source.FavoritePhrase, Field.Store.YES)
80+
};
81+
82+
writer.AddDocument(doc);
83+
writer.Flush(triggerMerge: false, applyAllDeletes: false);
84+
}
85+
86+
for (int i = 0; i < _indexSearchIterations; i++)
87+
{
88+
// search with a phrase
89+
var phrase = new MultiPhraseQuery
90+
{
91+
new Term("favoritePhrase", $"brown{i}"),
92+
new Term("favoritePhrase", $"fox{i}")
93+
};
94+
95+
// re-use the writer to get real-time updates
96+
using var reader = writer.GetReader(applyAllDeletes: true);
97+
var searcher = new IndexSearcher(reader);
98+
var hits = searcher.Search(phrase, 20 /* top 20 */).ScoreDocs;
99+
Debug.Assert(hits.Length > 0);
100+
foreach (var hit in hits)
101+
{
102+
var foundDoc = searcher.Doc(hit.Doc);
103+
var score = hit.Score;
104+
var name = foundDoc.Get("name");
105+
var favoritePhrase = foundDoc.Get("favoritePhrase");
106+
}
107+
}
108+
}
109+
}
110+
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
10+
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00005" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using BenchmarkDotNet.Running;
2+
3+
namespace Lucene.Net.Tests.BenchmarkDotNet
4+
{
5+
/*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership.
9+
* The ASF licenses this file to You under the Apache License, Version 2.0
10+
* (the "License"); you may not use this file except in compliance with
11+
* the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
class Program
23+
{
24+
private static void Main(string[] args) => new BenchmarkSwitcher(typeof(Program).Assembly).Run(args);
25+
}
26+
}

0 commit comments

Comments
 (0)