1+ using BenchmarkDotNet . Attributes ;
2+ using BenchmarkDotNet . Running ;
3+ using EF7Bulk ;
4+ using Microsoft . Data . Sqlite ;
5+ using Microsoft . EntityFrameworkCore ;
6+
7+ BenchmarkRunner . Run < UpdateBenchmark > ( ) ;
8+
9+ [ MemoryDiagnoser ]
10+ public class UpdateBenchmark
11+ {
12+ private PeopleContext _peopleContext ;
13+
14+ [ Params ( 100 , 1_000 ) ] public int RowsToUpdate { get ; set ; }
15+
16+ [ GlobalSetup ]
17+ public void Setup ( )
18+ {
19+ _peopleContext = CreateContext ( ) ;
20+ CreateContext ( ) ;
21+ FillWithData ( ) ;
22+ }
23+
24+ [ Benchmark ( Baseline = true ) ]
25+ public async Task OneByOneUpdate ( )
26+ {
27+ var entries = await _peopleContext . People . ToListAsync ( ) ;
28+ foreach ( var entry in entries )
29+ {
30+ entry . Age = 21 ;
31+ }
32+
33+ await _peopleContext . SaveChangesAsync ( ) ;
34+ }
35+
36+ [ Benchmark ]
37+ public async Task BulkUpdate ( )
38+ {
39+ await _peopleContext . People
40+ . ExecuteUpdateAsync ( s => s . SetProperty ( p => p . Age , p => 21 ) ) ;
41+ }
42+
43+ private void FillWithData ( )
44+ {
45+ using var context = CreateContext ( ) ;
46+ context . Database . EnsureCreated ( ) ;
47+ for ( var i = 1 ; i <= RowsToUpdate ; i ++ )
48+ context . People . Add ( new Person ( i , "Steven" , 31 ) ) ;
49+
50+ context . SaveChanges ( ) ;
51+ }
52+
53+ private static PeopleContext CreateContext ( )
54+ {
55+ var connection = new SqliteConnection ( "DataSource=myshareddb;mode=memory;cache=shared" ) ;
56+ connection . Open ( ) ;
57+ var options = new DbContextOptionsBuilder ( )
58+ . UseSqlite ( connection )
59+ . Options ;
60+ return new PeopleContext ( options ) ;
61+ }
62+ }
0 commit comments