Skip to content

Commit e238242

Browse files
committed
refactored indexing profiling project so that Testers are easier to implement
1 parent abf05f2 commit e238242

File tree

12 files changed

+244
-235
lines changed

12 files changed

+244
-235
lines changed

src/Nest/ElasticClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ElasticClient(
4242
INestSerializer serializer = null)
4343
{
4444
this._connectionSettings = settings ?? new ConnectionSettings();
45-
this.Connection = connection ?? new HttpConnection(settings);
45+
this.Connection = connection ?? new HttpConnection(this._connectionSettings);
4646

4747
this.Serializer = serializer ?? new NestSerializer(this._connectionSettings);
4848
var stringifier = new NestStringifier(this._connectionSettings);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Elasticsearch.Net.Connection.HttpClient;
5+
using Nest;
6+
7+
namespace Profiling.Indexing
8+
{
9+
public class HttpClientTester : Tester
10+
{
11+
public override IElasticClient CreateClient(string indexName)
12+
{
13+
var settings = this.CreateSettings(indexName, 9200);
14+
var client = new ElasticClient(settings, new ElasticsearchHttpClient(settings));
15+
return client;
16+
}
17+
18+
19+
}
20+
}

src/Profiling/Profiling.Indexing/HttpSSTextTester .cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Profiling/Profiling.Indexing/HttpTester.cs

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,15 @@
55

66
namespace Profiling.Indexing
77
{
8-
internal class HttpTester : Tester, ITester
8+
public class HttpTester : Tester
99
{
10-
public void Run(string indexName, int port, int numMessages, int bufferSize)
10+
public override IElasticClient CreateClient(string indexName)
1111
{
12-
var settings = this.CreateSettings(indexName, port);
12+
var settings = this.CreateSettings(indexName, 9200);
1313
var client = new ElasticClient(settings);
14-
15-
Connect(client, settings);
16-
17-
GenerateAndIndex(client, indexName, numMessages, bufferSize);
18-
}
19-
public void SearchUsingSingleClient(string indexName, int port, int numberOfSearches)
20-
{
21-
var settings = this.CreateSettings(indexName, port);
22-
var client = new ElasticClient(settings);
23-
24-
var tasks = new List<Task>();
25-
for (var p = 0; p < numberOfSearches; p++)
26-
{
27-
var t = client.SearchAsync<Message>(s => s.MatchAll())
28-
.ContinueWith(ta =>
29-
{
30-
if (!ta.Result.IsValid)
31-
throw new ApplicationException(ta.Result.ConnectionStatus.ToString());
32-
});
33-
tasks.Add(t);
34-
}
35-
Task.WaitAll(tasks.ToArray());
36-
}
37-
public void SearchUsingMultipleClients(string indexName, int port, int numberOfSearches)
38-
{
39-
var settings = this.CreateSettings(indexName, port);
40-
var tasks = new List<Task>();
41-
for (var p = 0; p < numberOfSearches; p++)
42-
{
43-
var client = new ElasticClient(settings);
44-
var t = client.SearchAsync<Message>(s => s.MatchAll())
45-
.ContinueWith(ta =>
46-
{
47-
if (!ta.Result.IsValid)
48-
throw new ApplicationException(ta.Result.ConnectionStatus.ToString());
49-
});
50-
tasks.Add(t);
51-
}
52-
53-
Task.WaitAll(tasks.ToArray());
54-
14+
return client;
5515
}
16+
5617

5718
}
5819
}

src/Profiling/Profiling.Indexing/ITester.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/Profiling/Profiling.Indexing/PartitionExtension.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,30 @@ public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> sourc
3333
yield return new ReadOnlyCollection<T>(array);
3434
}
3535
}
36+
37+
public static decimal GetMedian(this IEnumerable<int> source)
38+
{
39+
// Create a copy of the input, and sort the copy
40+
int[] temp = source.ToArray();
41+
Array.Sort(temp);
42+
43+
int count = temp.Length;
44+
if (count == 0)
45+
{
46+
throw new InvalidOperationException("Empty collection");
47+
}
48+
else if (count % 2 == 0)
49+
{
50+
// count is even, average two middle elements
51+
int a = temp[count / 2 - 1];
52+
int b = temp[count / 2];
53+
return (a + b) / 2m;
54+
}
55+
else
56+
{
57+
// count is odd, return the middle element
58+
return temp[count / 2];
59+
}
60+
}
3661
}
3762
}

src/Profiling/Profiling.Indexing/Profiling.Indexing.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
</Reference>
7777
<Reference Include="System" />
7878
<Reference Include="System.Core" />
79+
<Reference Include="System.Net.Http" />
7980
<Reference Include="System.Runtime.Serialization" />
8081
<Reference Include="System.Xml.Linq" />
8182
<Reference Include="System.Data.DataSetExtensions" />
@@ -84,9 +85,8 @@
8485
<Reference Include="System.Xml" />
8586
</ItemGroup>
8687
<ItemGroup>
87-
<Compile Include="HttpSSTextTester .cs" />
88+
<Compile Include="HttpClientTester.cs" />
8889
<Compile Include="HttpTester.cs" />
89-
<Compile Include="ITester.cs" />
9090
<Compile Include="MessageGenerator.cs" />
9191
<Compile Include="PartitionExtension.cs" />
9292
<Compile Include="Program.cs" />
@@ -119,6 +119,10 @@
119119
</BootstrapperPackage>
120120
</ItemGroup>
121121
<ItemGroup>
122+
<ProjectReference Include="..\..\Connections\Elasticsearch.Net.Connection.HttpClient\Elasticsearch.Net.Connection.HttpClient.csproj">
123+
<Project>{a69322fd-b874-44ef-abe0-63f4a7b5593e}</Project>
124+
<Name>Elasticsearch.Net.Connection.HttpClient</Name>
125+
</ProjectReference>
122126
<ProjectReference Include="..\..\Elasticsearch.Net\Elasticsearch.Net.csproj">
123127
<Project>{e97ccf40-0ba6-43fe-9f2d-58d454134088}</Project>
124128
<Name>Elasticsearch.Net</Name>
Lines changed: 33 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Linq;
45
using Nest;
@@ -7,96 +8,52 @@ namespace Profiling.Indexing
78
{
89
class Program
910
{
10-
const string INDEX_PREFIX = "proto-load-test-";
11-
const int HTTP_PORT = 9200;
12-
const int THRIFT_PORT = 9500;
13-
14-
// Total number of messages to send to elasticsearch
15-
const int NUM_MESSAGES = 250000;
16-
17-
// Number of messages to buffer before sending via bulk API
18-
const int BUFFER_SIZE = 1000;
11+
static IEnumerable<Tester> Testers()
12+
{
13+
yield return new HttpTester();
14+
yield return new HttpClientTester();
15+
yield return new ThriftTester();
16+
}
1917

2018
static void Main(string[] args)
2119
{
20+
var warmup = new HttpTester().RunTests(10);
2221

23-
var process = Process.GetCurrentProcess();
24-
var baseThreadCount = process.Threads.Count;
25-
var baseMemorySize = process.VirtualMemorySize64;
26-
var host = "localhost";
27-
if (Process.GetProcessesByName("fiddler").Any())
28-
host = "ipv4.fiddler";
29-
var client = new ElasticClient(new ConnectionSettings(new Uri("http://"+host+":9200"), "nest-default-index"));
30-
//warmer
31-
RunTest<HttpTester>(HTTP_PORT, 10);
32-
33-
client.Refresh();
34-
35-
Console.WriteLine("Warmed up caches press any key to index {0} messages", NUM_MESSAGES);
22+
Console.WriteLine("Warmed up caches to start testing, press any key to start tests");
3623
Console.ReadLine();
24+
3725
ConsoleKeyInfo key;
3826
do
3927
{
40-
key = RunIndex(baseThreadCount, baseMemorySize);
28+
var results = Testers().Select(t => t.RunTests()).ToList();
29+
Console.WriteLine();
30+
foreach(var r in results)
31+
PrintRunResults(r);
32+
33+
Console.WriteLine("\nPress r to index again or any other key to delete indices created by this tool.\n");
34+
key = Console.ReadKey();
4135
} while (key.KeyChar == 'r');
4236

43-
RunIndex(baseThreadCount, baseMemorySize);
44-
45-
client.DeleteIndex(d => d.Index(INDEX_PREFIX + "*"));
46-
37+
var client = new ElasticClient();
38+
client.DeleteIndex(d => d.Index(Tester.INDEX_PREFIX + "*"));
4739
}
4840

49-
private static ConsoleKeyInfo RunIndex(int baseThreadCount, long baseMemorySize)
41+
private static void PrintRunResults(RunResults runResult)
5042
{
51-
var process = Process.GetCurrentProcess();
52-
double httpRate = RunTest<HttpTester>(HTTP_PORT);
53-
var threadCountHttp = process.Threads.Count;
54-
var memorySizeHttp = process.VirtualMemorySize64;
55-
56-
Console.WriteLine();
57-
Console.WriteLine("HTTP (IndexManyAsync): {0:0,0}/s\r (Before:After) {1}:{2} Threads {3}:{4} Virtual memory"
58-
, httpRate, baseThreadCount, threadCountHttp, baseMemorySize, memorySizeHttp);
59-
60-
Console.WriteLine("Press r to index again or any other key to delete indices created by this tool.");
61-
return Console.ReadKey();
62-
}
63-
64-
private static double RunTest<T>(int port, int? messages = null) where T : ITester
65-
{
66-
string type = typeof(T).Name.ToLowerInvariant();
67-
Console.WriteLine("Starting {0} test", type);
68-
69-
// Recreate index up-front, so this process doesn't interfere with perf figures
70-
Stopwatch sw = new Stopwatch();
71-
sw.Start();
72-
73-
var tester = Activator.CreateInstance<T>();
74-
75-
tester.Run(INDEX_PREFIX + type + "-" + Guid.NewGuid().ToString(), port, messages ?? NUM_MESSAGES, BUFFER_SIZE);
76-
77-
sw.Stop();
78-
double rate = NUM_MESSAGES / ((double)sw.ElapsedMilliseconds / 1000);
79-
80-
Console.WriteLine("{0} index test completed in {1}ms ({2:0,0}/s)", type, sw.ElapsedMilliseconds, rate);
81-
82-
//var numberOfSearches = 10000;
83-
84-
//sw.Restart();
85-
//tester.SearchUsingSingleClient(INDEX_PREFIX + type, port, numberOfSearches);
86-
//double singleClientSearchRate = numberOfSearches / ((double)sw.ElapsedMilliseconds / 1000);
87-
//Console.WriteLine("{0} search single client test completed in {1}ms ({2:0,0}/s)", type, sw.ElapsedMilliseconds, singleClientSearchRate);
88-
89-
//sw.Restart();
90-
//tester.SearchUsingMultipleClients(INDEX_PREFIX + type, port, numberOfSearches);
91-
//double multiClientSearchRate = numberOfSearches / ((double)sw.ElapsedMilliseconds / 1000);
92-
//Console.WriteLine("{0} search multi client test completed in {1}ms ({2:0,0}/s)", type, sw.ElapsedMilliseconds, multiClientSearchRate);
93-
94-
//// Close the index so we don't interfere with the next test
95-
//CloseIndex(type);
96-
97-
return rate;
43+
Console.WriteLine("---{0}\t---------------", runResult.TesterName);
44+
Console.WriteLine(" {0:0,0} msg/s {1} ms {2} docs",
45+
runResult.RatePerSecond,
46+
runResult.ElapsedMillisecond,
47+
runResult.IndexedDocuments
48+
);
49+
var maxEsTime = runResult.EsTimings.Max();
50+
var meanEsTime = runResult.EsTimings.GetMedian();
51+
Console.WriteLine(" max es-time:{0} mean es-time:{1}", maxEsTime, meanEsTime);
52+
Console.WriteLine(" memory before:{0} thread count before:{1}",
53+
runResult.Before.MemorySize, runResult.Before.ThreadCount);
54+
Console.WriteLine(" memory after:{0} thread count after:{1}",
55+
runResult.After.MemorySize, runResult.After.ThreadCount);
9856
}
9957

100-
10158
}
10259
}

0 commit comments

Comments
 (0)