Skip to content

Commit d123e76

Browse files
committed
Code Cleanup: Tests.Benchmarking, Tests.ClusterLauncher
1 parent 5fe6167 commit d123e76

File tree

8 files changed

+107
-200
lines changed

8 files changed

+107
-200
lines changed

src/Tests/Tests.Benchmarking/BenchmarkProgram.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ public static int Main(string[] arguments)
1515
if (arguments.Count() >= 1 && arguments[0].Equals("non-interactive", StringComparison.OrdinalIgnoreCase))
1616
{
1717
Console.WriteLine("Running in Non-Interactive mode.");
18-
foreach (var benchmarkType in GetBenchmarkTypes())
19-
{
20-
BenchmarkRunner.Run(benchmarkType);
21-
}
18+
foreach (var benchmarkType in GetBenchmarkTypes()) BenchmarkRunner.Run(benchmarkType);
2219

2320
return 0;
2421
}

src/Tests/Tests.Benchmarking/BulkDeserializationBenchmarkTests.cs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
using Newtonsoft.Json;
99
using Tests.Benchmarking.Framework;
1010
using Tests.Core.Client;
11-
1211
#if !DOTNETCORE
1312
using System.Buffers;
13+
1414
#endif
1515

1616
namespace Tests.Benchmarking
@@ -19,11 +19,11 @@ namespace Tests.Benchmarking
1919
public class BulkDeserializationBenchmarkTests
2020
{
2121
private static readonly IElasticClient Client = TestClient.DefaultInMemoryClient;
22-
private byte[] _tinyResponse;
23-
private byte[] _mediumResponse;
24-
private byte[] _largeResponse;
2522
private byte[] _hugeResponse;
2623
private JsonSerializer _jsonSerializer;
24+
private byte[] _largeResponse;
25+
private byte[] _mediumResponse;
26+
private byte[] _tinyResponse;
2727

2828
[GlobalSetup]
2929
public void Setup()
@@ -97,9 +97,7 @@ public BulkResponse Baseline()
9797
{
9898
using (var reader = new JsonTextReader(new StreamReader(new MemoryStream(_hugeResponse))))
9999
{
100-
while (reader.Read())
101-
{
102-
}
100+
while (reader.Read()) { }
103101

104102
return new BulkResponse();
105103
}
@@ -124,34 +122,23 @@ public BulkResponse Baseline()
124122
}
125123
};
126124

127-
private static object ReturnBulkResponse(int numberOfItems)
125+
private static object ReturnBulkResponse(int numberOfItems) => new
128126
{
129-
return new
130-
{
131-
took = 276,
132-
errors = false,
133-
items = Enumerable.Range(0, numberOfItems)
134-
.Select(i => BulkItemResponse())
135-
.ToArray()
136-
};
137-
}
127+
took = 276,
128+
errors = false,
129+
items = Enumerable.Range(0, numberOfItems)
130+
.Select(i => BulkItemResponse())
131+
.ToArray()
132+
};
138133

139134
#if !DOTNETCORE
140135
public class JsonArrayPool : IArrayPool<char>
141136
{
142137
public static readonly JsonArrayPool Instance = new JsonArrayPool();
143138

144-
public char[] Rent(int minimumLength)
145-
{
146-
// get char array from System.Buffers shared pool
147-
return ArrayPool<char>.Shared.Rent(minimumLength);
148-
}
139+
public char[] Rent(int minimumLength) => ArrayPool<char>.Shared.Rent(minimumLength);
149140

150-
public void Return(char[] array)
151-
{
152-
// return char array to System.Buffers shared pool
153-
ArrayPool<char>.Shared.Return(array);
154-
}
141+
public void Return(char[] array) => ArrayPool<char>.Shared.Return(array);
155142
}
156143
#endif
157144
}

src/Tests/Tests.Benchmarking/FieldResolverBenchmarkTests.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace Tests.Benchmarking
88
[BenchmarkConfig(100)]
99
public class FieldResolverBenchmarkTests
1010
{
11+
private static readonly Field InferredField = Infer.Field<Project>(p => p.Name);
12+
private static readonly Field PropertyField = typeof(Project).GetProperty(nameof(Project.Name));
13+
private static readonly Field StringField = "Name";
1114
private FieldResolver _expressionResolver;
12-
private FieldResolver _propertyResolver;
13-
private FieldResolver _stringResolver;
1415
private NoncachingFieldResolver _nonCachingExpressionResolver;
1516
private NoncachingFieldResolver _nonCachingPropertyResolver;
1617
private NoncachingFieldResolver _nonCachingStringResolver;
17-
private static readonly Field StringField = "Name";
18-
private static readonly Field PropertyField = typeof(Project).GetProperty(nameof(Project.Name));
19-
private static readonly Field InferredField = Infer.Field<Project>(p => p.Name);
18+
private FieldResolver _propertyResolver;
19+
private FieldResolver _stringResolver;
2020

2121
[GlobalSetup]
2222
public void Setup()
@@ -30,39 +30,21 @@ public void Setup()
3030
}
3131

3232
[Benchmark]
33-
public string NonCachedFieldUsingExpression()
34-
{
35-
return _nonCachingExpressionResolver.Resolve(InferredField);
36-
}
33+
public string NonCachedFieldUsingExpression() => _nonCachingExpressionResolver.Resolve(InferredField);
3734

3835
[Benchmark]
39-
public string CachedFieldUsingExpression()
40-
{
41-
return _expressionResolver.Resolve(InferredField);
42-
}
36+
public string CachedFieldUsingExpression() => _expressionResolver.Resolve(InferredField);
4337

4438
[Benchmark]
45-
public string NonCachedFieldUsingPropertyInfo()
46-
{
47-
return _nonCachingPropertyResolver.Resolve(PropertyField);
48-
}
39+
public string NonCachedFieldUsingPropertyInfo() => _nonCachingPropertyResolver.Resolve(PropertyField);
4940

5041
[Benchmark]
51-
public string CachedFieldUsingPropertyInfo()
52-
{
53-
return _propertyResolver.Resolve(PropertyField);
54-
}
42+
public string CachedFieldUsingPropertyInfo() => _propertyResolver.Resolve(PropertyField);
5543

5644
[Benchmark(Baseline = true)]
57-
public string NonCachedFieldUsingString()
58-
{
59-
return _nonCachingStringResolver.Resolve(StringField);
60-
}
45+
public string NonCachedFieldUsingString() => _nonCachingStringResolver.Resolve(StringField);
6146

6247
[Benchmark]
63-
public string CachedFieldUsingString()
64-
{
65-
return _stringResolver.Resolve(StringField);
66-
}
48+
public string CachedFieldUsingString() => _stringResolver.Resolve(StringField);
6749
}
6850
}

src/Tests/Tests.Benchmarking/Framework/BenchmarkConfig.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
using BenchmarkDotNet.Configs;
55
using BenchmarkDotNet.Diagnosers;
66
using BenchmarkDotNet.Environments;
7+
using BenchmarkDotNet.Exporters;
78
using BenchmarkDotNet.Jobs;
89
using BenchmarkDotNet.Loggers;
910
using BenchmarkDotNet.Reports;
1011
using Newtonsoft.Json;
1112

1213
namespace Tests.Benchmarking.Framework
1314
{
14-
public class CustomJsonExporter : BenchmarkDotNet.Exporters.ExporterBase
15+
public class CustomJsonExporter : ExporterBase
1516
{
1617
protected override string FileExtension => "json";
1718

@@ -41,13 +42,13 @@ public override void ExportToLog(Summary summary, ILogger logger)
4142
{
4243
var data = new Dictionary<string, object>
4344
{
44-
{ "DisplayInfo", r.BenchmarkCase.DisplayInfo },
45+
{ "DisplayInfo", r.BenchmarkCase.DisplayInfo },
4546
{ "Namespace", r.BenchmarkCase.Descriptor.Type.Namespace },
4647
{ "Type", r.BenchmarkCase.Descriptor.Type.Name },
4748
{ "Method", r.BenchmarkCase.Descriptor.WorkloadMethod.Name },
4849
{ "MethodTitle", r.BenchmarkCase.Descriptor.WorkloadMethod.Name },
4950
{ "Parameters", r.BenchmarkCase.Parameters.PrintInfo },
50-
{ "Statistics", r.ResultStatistics },
51+
{ "Statistics", r.ResultStatistics },
5152
{ "Memory", r.GcStats }
5253
};
5354

@@ -63,21 +64,23 @@ public override void ExportToLog(Summary summary, ILogger logger)
6364
}));
6465
}
6566
}
67+
6668
public class BenchmarkConfigAttribute : Attribute, IConfigSource
6769
{
68-
public IConfig Config { get; }
69-
7070
public BenchmarkConfigAttribute(int runCount = 1)
7171
{
72-
var jobs = new[] {
72+
var jobs = new[]
73+
{
7374
Job.Dry.With(Runtime.Core).With(Jit.RyuJit).WithIterationCount(runCount),
7475
Job.Dry.With(Runtime.Clr).With(Jit.RyuJit).WithIterationCount(runCount),
7576
Job.Dry.With(Runtime.Clr).With(Jit.LegacyJit).WithIterationCount(runCount)
7677
};
77-
this.Config = DefaultConfig.Instance
78+
Config = DefaultConfig.Instance
7879
.With(jobs)
7980
.With(new CustomJsonExporter())
8081
.With(MemoryDiagnoser.Default);
8182
}
83+
84+
public IConfig Config { get; }
8285
}
8386
}

src/Tests/Tests.Benchmarking/NoncachingFieldResolver.cs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Reflection;
99
using System.Runtime.CompilerServices;
1010
using System.Text;
11-
using Elasticsearch.Net;
1211
using Nest;
1312

1413
namespace Tests.Benchmarking
@@ -17,27 +16,28 @@ public class NoncachingFieldResolver
1716
{
1817
private readonly IConnectionSettingsValues _settings;
1918

20-
public NoncachingFieldResolver(IConnectionSettingsValues settings) => this._settings = settings;
19+
public NoncachingFieldResolver(IConnectionSettingsValues settings) => _settings = settings;
2120

2221
public string Resolve(Field field)
2322
{
24-
var name = this.ResolveFieldName(field);
23+
var name = ResolveFieldName(field);
2524
if (field.Boost.HasValue) name += $"^{field.Boost.Value.ToString(CultureInfo.InvariantCulture)}";
2625
return name;
2726
}
2827

29-
internal static bool IsConditionless(Field field) => field == null || (string.IsNullOrEmpty(field.Name) && field.Expression == null && field.Property == null);
28+
internal static bool IsConditionless(Field field) =>
29+
field == null || string.IsNullOrEmpty(field.Name) && field.Expression == null && field.Property == null;
3030

3131
internal static bool IsConditionless(PropertyName property) =>
32-
property == null || (string.IsNullOrEmpty(property.Name) && property.Expression == null && property.Property == null);
32+
property == null || string.IsNullOrEmpty(property.Name) && property.Expression == null && property.Property == null;
3333

3434
private string ResolveFieldName(Field field)
3535
{
3636
if (IsConditionless(field)) return null;
3737
if (!string.IsNullOrEmpty(field.Name)) return field.Name;
38-
if (field.Expression != null && !field.CachableExpression) return this.Resolve(field.Expression, field.Property);
38+
if (field.Expression != null && !field.CachableExpression) return Resolve(field.Expression, field.Property);
3939

40-
var fieldName = this.Resolve(field.Expression, field.Property);
40+
var fieldName = Resolve(field.Expression, field.Property);
4141
return fieldName;
4242
}
4343

@@ -46,12 +46,9 @@ public string Resolve(PropertyName property)
4646
if (IsConditionless(property)) return null;
4747
if (!string.IsNullOrEmpty(property.Name)) return property.Name;
4848

49-
if (property.Expression != null && !property.CacheableExpression)
50-
{
51-
return this.Resolve(property.Expression, property.Property);
52-
}
49+
if (property.Expression != null && !property.CacheableExpression) return Resolve(property.Expression, property.Property);
5350

54-
var propertyName = this.Resolve(property.Expression, property.Property, true);
51+
var propertyName = Resolve(property.Expression, property.Property, true);
5552
return propertyName;
5653
}
5754

@@ -72,16 +69,16 @@ private string Resolve(Expression expression, MemberInfo member, bool toLastToke
7269

7370
internal class FieldExpressionVisitor : ExpressionVisitor
7471
{
75-
private readonly Stack<string> _stack = new Stack<string>();
76-
7772
private readonly IConnectionSettingsValues _settings;
73+
private readonly Stack<string> _stack = new Stack<string>();
7874

7975
public FieldExpressionVisitor(IConnectionSettingsValues settings) => _settings = settings;
8076

8177
public string Resolve(Expression expression, bool toLastToken = false)
8278
{
83-
this.Visit(expression);
79+
Visit(expression);
8480
if (toLastToken) return Enumerable.Last<string>(_stack);
81+
8582
return Enumerable.Aggregate<string, StringBuilder>(_stack, new StringBuilder(),
8683
(sb, name) =>
8784
(sb.Length > 0 ? sb.Append(".") : sb).Append(name))
@@ -95,7 +92,7 @@ public string Resolve(MemberInfo info)
9592

9693
var name = info.Name;
9794

98-
if (this._settings.PropertyMappings.TryGetValue(info, out var propertyMapping))
95+
if (_settings.PropertyMappings.TryGetValue(info, out var propertyMapping))
9996
return propertyMapping.Name;
10097

10198
var att = ElasticsearchPropertyAttributeBase.From(info);
@@ -108,7 +105,8 @@ public string Resolve(MemberInfo info)
108105
protected override Expression VisitMember(MemberExpression expression)
109106
{
110107
if (_stack == null) return base.VisitMember(expression);
111-
var name = this.Resolve(expression.Member);
108+
109+
var name = Resolve(expression.Member);
112110
_stack.Push(name);
113111
return base.VisitMember(expression);
114112
}
@@ -121,7 +119,7 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCall)
121119
var callingMember = new ReadOnlyCollection<Expression>(
122120
new List<Expression> { { methodCall.Arguments.First() } }
123121
);
124-
base.Visit(callingMember);
122+
Visit(callingMember);
125123
return methodCall;
126124
}
127125
else if (methodCall.Method.Name == "get_Item" && methodCall.Arguments.Any())
@@ -130,23 +128,18 @@ protected override Expression VisitMethodCall(MethodCallExpression methodCall)
130128
var isDict =
131129
typeof(IDictionary).IsAssignableFrom(t)
132130
|| typeof(IDictionary<,>).IsAssignableFrom(t)
133-
|| (t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>));
131+
|| t.GetTypeInfo().IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>);
132+
133+
if (!isDict) return base.VisitMethodCall(methodCall);
134134

135-
if (!isDict)
136-
{
137-
return base.VisitMethodCall(methodCall);
138-
}
139135
VisitConstantOrVariable(methodCall, _stack);
140-
this.Visit(methodCall.Object);
136+
Visit(methodCall.Object);
141137
return methodCall;
142138
}
143139
else if (IsLinqOperator(methodCall.Method))
144140
{
145-
for (int i = 1; i < methodCall.Arguments.Count; i++)
146-
{
147-
this.Visit(methodCall.Arguments[i]);
148-
}
149-
this.Visit(methodCall.Arguments[0]);
141+
for (var i = 1; i < methodCall.Arguments.Count; i++) Visit(methodCall.Arguments[i]);
142+
Visit(methodCall.Arguments[0]);
150143
return methodCall;
151144
}
152145
return base.VisitMethodCall(methodCall);

0 commit comments

Comments
 (0)