Skip to content

Commit a22146e

Browse files
committed
Add BatchedInsertData.
1 parent a452592 commit a22146e

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using BenchmarkDotNet.Attributes;
6+
using EFCore.Cassandra.Benchmarks.Models;
7+
using Microsoft.EntityFrameworkCore;
8+
9+
namespace EFCore.Cassandra.Benchmarks
10+
{
11+
[Config(typeof(CassandraBenchmarkConfig))]
12+
public class BatchedInsertData : IDisposable
13+
{
14+
private readonly FakeDbContext _dbContext;
15+
private Applicant[][] _applicants;
16+
17+
public BatchedInsertData()
18+
{
19+
_dbContext = new FakeDbContext();
20+
}
21+
22+
[ParamsSource(nameof(IterationsSource))]
23+
public int Iterations { get; set; } = 1000;
24+
25+
public IEnumerable<int> IterationsSource => new[] {1, 10, 100};
26+
27+
[ParamsSource(nameof(BatchSizeSource))]
28+
public int BatchSize { get; set; } = 10;
29+
30+
public IEnumerable<int> BatchSizeSource => new[] {10, 100};
31+
32+
public void Dispose()
33+
{
34+
_dbContext.Dispose();
35+
}
36+
37+
[GlobalSetup]
38+
public void SetupFixture()
39+
{
40+
_applicants = Enumerable.Range(0, Iterations).Select(_ => BuildApplicants()).ToArray();
41+
}
42+
43+
44+
[Benchmark]
45+
public async Task BatchedInsertApplicantsAsync()
46+
{
47+
var a = Enumerable.Range(0, Iterations)
48+
.Select(i => _dbContext.BulkInsertAsync(_applicants[i].ToList()));
49+
50+
await Task.WhenAll(a);
51+
}
52+
53+
54+
private Applicant[] BuildApplicants()
55+
{
56+
return Enumerable.Range(0, BatchSize).Select(_ => BuildApplicant()).ToArray();
57+
}
58+
59+
private static Applicant BuildApplicant() =>
60+
new Applicant
61+
{
62+
Id = Guid.NewGuid(),
63+
Order = 0,
64+
Dic = new Dictionary<string, string>(),
65+
Lst = new List<string>(),
66+
LstInt = new List<int>()
67+
};
68+
}
69+
}

src/EFCore.Cassandra.Benchmarks/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ internal class Program
1010
private static async Task Main(string[] args)
1111
{
1212
BenchmarkRunner.Run<InsertData>();
13+
BenchmarkRunner.Run<BatchedInsertData>();
1314
}
1415
}
1516
}

0 commit comments

Comments
 (0)