-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathProgram.cs
59 lines (48 loc) · 1.58 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Data.Common;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
[MemoryDiagnoser]
public class ToArrayVsToListBenchmark
{
private BlogContext? _context;
private DbConnection? _connection;
[GlobalSetup(Targets = [nameof(ToArrayAsyncBenchmark), nameof(ToListAsyncBenchmark)])]
public void Setup()
{
_connection = CreateInMemoryConnection();
var options = new DbContextOptionsBuilder()
.UseSqlite(_connection)
.Options;
_context = new BlogContext(options);
_context.Database.EnsureDeleted();
_context.Database.EnsureCreated();
DataSeeder.Seed(_context, 10000);
}
[Params(100, 1000, 10000)]
public int NumberOfElements { get; set; }
[Benchmark]
public async Task<List<BlogPost>> ToListAsyncBenchmark()
{
return await _context!.BlogPosts.Take(NumberOfElements).ToListAsync();
}
[Benchmark]
public async Task<BlogPost[]> ToArrayAsyncBenchmark()
{
return await _context!.BlogPosts.Take(NumberOfElements).ToArrayAsync();
}
[GlobalCleanup]
public void Cleanup()
{
_context?.Dispose();
_connection?.Dispose();
}
public static void Main() => BenchmarkRunner.Run<ToArrayVsToListBenchmark>();
private static SqliteConnection CreateInMemoryConnection()
{
var connection = new SqliteConnection(string.Empty);
connection.Open();
return connection;
}
}