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+ }
0 commit comments