|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.Linq; |
| 5 | +using System.Net.Sockets; |
| 6 | +using System.Runtime.Remoting.Channels; |
| 7 | +using System.Security.Cryptography.X509Certificates; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Nest.Tests.MockData; |
| 10 | +using Nest.Tests.MockData.Domain; |
| 11 | +using NUnit.Framework; |
| 12 | +using System.Diagnostics; |
| 13 | +using FluentAssertions; |
| 14 | +using Elasticsearch.Net; |
| 15 | + |
| 16 | +namespace Nest.Tests.Integration.Reproduce |
| 17 | +{ |
| 18 | + [TestFixture] |
| 19 | + public class ReproduceConnectionStallsTests : IntegrationTests |
| 20 | + { |
| 21 | + private readonly string _index; |
| 22 | + |
| 23 | + public ReproduceConnectionStallsTests() |
| 24 | + { |
| 25 | + this._index = ElasticsearchConfiguration.NewUniqueIndexName(); |
| 26 | + var people = NestTestData.Session.List<Person>(10000).Get().ToList(); |
| 27 | + var lotsOfPeople = Partition(people, 1000).ToList(); |
| 28 | + var createIndexResult = this._client.CreateIndex(_index, c => c |
| 29 | + .NumberOfReplicas(0) |
| 30 | + .NumberOfShards(1) |
| 31 | + .Settings(s=>s.Add("index.refresh_interval", -1)) |
| 32 | + .AddMapping<Person>(m => m.MapFromAttributes()) |
| 33 | + ); |
| 34 | + Parallel.ForEach(lotsOfPeople, (listOfPeople) => |
| 35 | + { |
| 36 | + _client.IndexMany(listOfPeople, _index); |
| 37 | + }); |
| 38 | + _client.UpdateSettings(u=>u.Index(_index).RefreshInterval("1s")); |
| 39 | + _client.Refresh(r=>r.Index(_index)); |
| 40 | + |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | + [Test] |
| 45 | + public void SearchShouldNotHaveBlips() |
| 46 | + { |
| 47 | + //Search once so that caches are sure to exist. |
| 48 | + var timing = this.Search(); |
| 49 | + |
| 50 | + var timings = new List<Timings>(); |
| 51 | + for (var i = 0; i < 1000; i++) |
| 52 | + { |
| 53 | + timing = this.Search(); |
| 54 | + timings.Add(timing); |
| 55 | + } |
| 56 | + |
| 57 | + timings.Should().OnlyContain(t => t.IsValid); |
| 58 | + |
| 59 | + var minElasticsearch = timings.Min(t => t.ElasticsearchTook); |
| 60 | + var minNest = timings.Min(t => t.NestTook); |
| 61 | + |
| 62 | + //elasticsearch is fast! min should absolutely be below 2ms |
| 63 | + minElasticsearch.Should().BeLessThan(10); |
| 64 | + |
| 65 | + //nest has some overhead, it should be wthin reason though |
| 66 | + minNest.Should().BeLessThan(20); |
| 67 | + |
| 68 | + |
| 69 | + var maxElasticsearch = timings.Max(t => t.ElasticsearchTook); |
| 70 | + var maxNest = timings.Max(t => t.NestTook); |
| 71 | + |
| 72 | + maxElasticsearch.Should().BeLessThan(20); |
| 73 | + maxNest.Should().BeLessThan(30); |
| 74 | + |
| 75 | + var plotElasticsearch = timings.Select(p => p.ElasticsearchTook).ToArray(); |
| 76 | + var plotNest = timings.Select(p => p.NestTook).ToArray(); |
| 77 | + |
| 78 | + plotElasticsearch.Should().NotBeEmpty(); |
| 79 | + plotNest.Should().NotBeEmpty(); |
| 80 | + |
| 81 | + var csv = string.Join("\n", timings.Select((t,i) => "{0}\t{1}\t{2}\t{3}".F(i, t.ElasticsearchTook, t.NestTook, t.IsValid))); |
| 82 | + Assert.Pass(csv); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + private Timings Search() |
| 87 | + { |
| 88 | + int take = 100; |
| 89 | + var stopwatch = Stopwatch.StartNew(); |
| 90 | + var search = this._client.Search<Person>(s => s.MatchAll().Size(take).Index(_index)); |
| 91 | + stopwatch.Stop(); |
| 92 | + return new Timings() |
| 93 | + { |
| 94 | + ElasticsearchTook = search.ElapsedMilliseconds, |
| 95 | + NestTook = stopwatch.ElapsedMilliseconds, |
| 96 | + IsValid = search.IsValid && search.Documents.Count() == take |
| 97 | + |
| 98 | + }; |
| 99 | + } |
| 100 | + |
| 101 | + public class Timings |
| 102 | + { |
| 103 | + public long ElasticsearchTook { get; set; } |
| 104 | + public long NestTook { get; set; } |
| 105 | + public bool IsValid { get; set; } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + public IEnumerable<IEnumerable<T>> Partition<T>(IEnumerable<T> source, int size) |
| 110 | + { |
| 111 | + T[] array = null; |
| 112 | + int count = 0; |
| 113 | + foreach (T item in source) |
| 114 | + { |
| 115 | + if (array == null) |
| 116 | + { |
| 117 | + array = new T[size]; |
| 118 | + } |
| 119 | + array[count] = item; |
| 120 | + count++; |
| 121 | + if (count == size) |
| 122 | + { |
| 123 | + yield return new ReadOnlyCollection<T>(array); |
| 124 | + array = null; |
| 125 | + count = 0; |
| 126 | + } |
| 127 | + } |
| 128 | + if (array != null) |
| 129 | + { |
| 130 | + Array.Resize(ref array, count); |
| 131 | + yield return new ReadOnlyCollection<T>(array); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | +} |
0 commit comments