|
| 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 | +} |
0 commit comments