|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Nest; |
| 4 | +using Tests.Domain; |
| 5 | +using Tests.Profiling.Framework; |
| 6 | +using Tests.Profiling.Framework.Performance; |
| 7 | + |
| 8 | +namespace Tests.Profiling |
| 9 | +{ |
| 10 | + public class BulkProfileTests |
| 11 | + { |
| 12 | + private static readonly string IndexName = "bulk-profile"; |
| 13 | + private readonly IElasticClient _client; |
| 14 | + |
| 15 | + public BulkProfileTests(ProfilingCluster cluster) |
| 16 | + { |
| 17 | + _client = cluster.Client; |
| 18 | + |
| 19 | + if (_client.IndexExists(IndexName).Exists) |
| 20 | + _client.DeleteIndex(IndexName); |
| 21 | + |
| 22 | + var createIndexResponse = _client.CreateIndex(IndexName); |
| 23 | + |
| 24 | + if (!createIndexResponse.IsValid) |
| 25 | + Console.WriteLine($"invalid response creating index: {createIndexResponse.ServerError?.Error?.Reason}"); |
| 26 | + } |
| 27 | + |
| 28 | + [Performance(Iterations = 10)] |
| 29 | + public void Sync() |
| 30 | + { |
| 31 | + var bulkResponse = _client.Bulk(b => b |
| 32 | + .IndexMany(Developer.Generator.Generate(1000), (bd, d) => bd |
| 33 | + .Index(IndexName) |
| 34 | + .Document(d) |
| 35 | + )); |
| 36 | + |
| 37 | + if (!bulkResponse.IsValid) |
| 38 | + { |
| 39 | + if (bulkResponse.Errors) |
| 40 | + foreach (var error in bulkResponse.ItemsWithErrors) |
| 41 | + Console.WriteLine($"error with id {error.Id}. message: {error.Error.Reason}"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + [Performance(Iterations = 10)] |
| 46 | + public async Task Async() |
| 47 | + { |
| 48 | + var bulkResponse = await _client.BulkAsync(b => b |
| 49 | + .IndexMany(Developer.Generator.Generate(1000), (bd, d) => bd |
| 50 | + .Index(IndexName) |
| 51 | + .Document(d) |
| 52 | + )) |
| 53 | + .ConfigureAwait(false); |
| 54 | + |
| 55 | + if (!bulkResponse.IsValid) |
| 56 | + { |
| 57 | + if (bulkResponse.Errors) |
| 58 | + foreach (var error in bulkResponse.ItemsWithErrors) |
| 59 | + Console.WriteLine($"error with id {error.Id}. message: {error.Error.Reason}"); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments