-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWriteRecordsBenchmarkBase.cs
More file actions
67 lines (56 loc) · 2.36 KB
/
WriteRecordsBenchmarkBase.cs
File metadata and controls
67 lines (56 loc) · 2.36 KB
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
60
61
62
63
64
65
66
67
using BenchmarkDotNet.Attributes;
using CmdScale.EntityFrameworkCore.TimescaleDB.Samples.Shared;
using Microsoft.EntityFrameworkCore;
using Testcontainers.PostgreSql;
namespace CmdScale.EntityFrameworkCore.TimescaleDB.Benchmarks
{
public abstract class WriteRecordsBenchmarkBase<T> where T : class
{
public int NumberOfRecords;
public int MaxBatchSize;
public int NumberOfWorkers;
private readonly PostgreSqlContainer _dbContainer = new PostgreSqlBuilder("timescale/timescaledb:latest-pg17")
.WithDatabase("benchmark_tests_db")
.WithUsername("test_user")
.WithPassword("test_password")
.Build();
protected string ConnectionString = "";
protected readonly List<T> Trades = [];
protected TimescaleContext? Context;
[GlobalSetup]
public async Task Setup()
{
await _dbContainer.StartAsync();
ConnectionString = _dbContainer.GetConnectionString();
DbContextOptionsBuilder<TimescaleContext> optionsBuilder = new();
optionsBuilder.UseNpgsql(ConnectionString).UseTimescaleDb();
Context = new TimescaleContext(optionsBuilder.Options);
await Context.Database.MigrateAsync();
Console.WriteLine("Migration applied successfully.");
}
[GlobalCleanup]
public async Task GlobalCleanup()
{
await _dbContainer.DisposeAsync();
}
[IterationSetup]
public void IterationSetup()
{
Trades.Clear();
Random random = new();
string[] tickers = ["AAPL", "GOOGL", "MSFT", "TSLA", "AMZN"];
DateTime baseTimestamp = DateTime.UtcNow.AddMinutes(-30);
for (int i = 0; i < NumberOfRecords; i++)
{
T trade = CreateTradeInstance(i, baseTimestamp, tickers[random.Next(tickers.Length)], random);
Trades.Add(trade);
}
// Truncate the table before each iteration for a clean slate
string tableName = GetTableName();
string sql = $"TRUNCATE TABLE \"{tableName}\"";
Context!.Database.ExecuteSqlRaw(sql);
}
protected abstract T CreateTradeInstance(int index, DateTime baseTimestamp, string ticker, Random random);
protected abstract string GetTableName();
}
}