Skip to content

Commit

Permalink
Adds simple benchmark between nuget versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Jul 15, 2020
1 parent 081edee commit 573f301
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Lucene.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
112 changes: 112 additions & 0 deletions src/Lucene.Net.Tests.BenchmarkDotNet/HomePageScriptBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Lucene.Net.Analysis.Common" Version="4.8.0-beta00005" />
</ItemGroup>

</Project>
26 changes: 26 additions & 0 deletions src/Lucene.Net.Tests.BenchmarkDotNet/Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 573f301

Please sign in to comment.