Skip to content

Commit

Permalink
Updates ConcurrencyTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed Aug 15, 2024
1 parent 92b514b commit aa098e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
33 changes: 23 additions & 10 deletions src/Examine.Test/Examine.Lucene/Search/ConcurrencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
using System.Threading.Tasks;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Store;
using Microsoft.Extensions.Logging;
using NUnit.Framework;

namespace Examine.Test.Examine.Lucene.Search
{
[TestFixture]
public class ConcurrencyTests : ExamineBaseTest
{
protected override ILoggerFactory CreateLoggerFactory()
=> Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));

[TestCase(1)]
[TestCase(10)]
[TestCase(25)]
[TestCase(100)]
public async Task BenchmarkConcurrentSearching(int threads)
{
var logger = LoggerFactory.CreateLogger<ConcurrencyTests>();
var tempBasePath = Path.Combine(Path.GetTempPath(), "ExamineTests");

try
Expand All @@ -34,14 +40,20 @@ public async Task BenchmarkConcurrentSearching(int threads)
luceneDir,
analyzer))
{
indexer.IndexItems(new[] {
ValueSet.FromObject(1.ToString(), "content",
new { nodeName = "location 1", bodyText = "Zanzibar is in Africa"}),
ValueSet.FromObject(2.ToString(), "content",
new { nodeName = "location 2", bodyText = "In Canada there is a town called Sydney in Nova Scotia"}),
ValueSet.FromObject(3.ToString(), "content",
new { nodeName = "location 3", bodyText = "Sydney is the capital of NSW in Australia"})
});
var random = new Random();
var valueSets = new List<ValueSet>();

for (var i = 0; i < 1000; i++)
{
valueSets.Add(ValueSet.FromObject(Guid.NewGuid().ToString(), "content",
new
{
nodeName = "location " + i,
bodyText = Enumerable.Range(0, random.Next(10, 100)).Select(x => Guid.NewGuid().ToString())
}));
}

indexer.IndexItems(valueSets);

var tasks = new List<Task>();

Expand All @@ -56,7 +68,8 @@ public async Task BenchmarkConcurrentSearching(int threads)
var results = query.Execute();
// enumerate (forces the result to execute)
Console.WriteLine("ThreadID: " + Thread.CurrentThread.ManagedThreadId + ", Results: " + string.Join(',', results.Select(x => $"{x.Id}-{x.Values.Count}-{x.Score}").ToArray()));
var logOutput = "ThreadID: " + Thread.CurrentThread.ManagedThreadId + ", Results: " + string.Join(',', results.Select(x => $"{x.Id}-{x.Values.Count}-{x.Score}").ToArray());
logger.LogDebug(logOutput);
}));
}

Expand All @@ -75,7 +88,7 @@ public async Task BenchmarkConcurrentSearching(int threads)
finally
{
stopwatch.Stop();
Console.WriteLine("Completed in ms: " + stopwatch.ElapsedMilliseconds);
logger.LogInformation("Completed in ms: " + stopwatch.ElapsedMilliseconds);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Examine.Test/ExamineBaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class ExamineBaseTest
[SetUp]
public virtual void Setup()
{
LoggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
LoggerFactory = CreateLoggerFactory();
LoggerFactory.CreateLogger(typeof(ExamineBaseTest)).LogDebug("Initializing test");
}

Expand All @@ -42,5 +42,8 @@ public TestIndex GetTestIndex(IndexWriter writer)
LoggerFactory,
Mock.Of<IOptionsMonitor<LuceneIndexOptions>>(x => x.Get(TestIndex.TestIndexName) == new LuceneIndexOptions()),
writer);

protected virtual ILoggerFactory CreateLoggerFactory()
=> Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Debug));
}
}

0 comments on commit aa098e1

Please sign in to comment.