Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Oct 13, 2024
1 parent e39f5af commit e046809
Show file tree
Hide file tree
Showing 21 changed files with 253 additions and 213 deletions.
2 changes: 1 addition & 1 deletion NBomber.Sinks.Timescale.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "examples\Demo\Demo.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NBomber.Sinks.Timescale", "src\NBomber.Sinks.Timescale\NBomber.Sinks.Timescale.csproj", "{9225A528-D8A6-4EDB-9C71-228F4F0B58E2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nbomber.Sinks.Timescale.Tests", "tests\Nbomber.Sinks.Timescale.Tests\Nbomber.Sinks.Timescale.Tests.csproj", "{2034EFD1-9463-4292-9DE2-C5DA9610B1BC}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBomber.Sinks.Timescale.Tests", "tests\NBomber.Sinks.Timescale.Tests\NBomber.Sinks.Timescale.Tests.csproj", "{2034EFD1-9463-4292-9DE2-C5DA9610B1BC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/TimescaleBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
using Npgsql;
using RepoDb;
using NBomber.CSharp;
using NBomber.Sinks.Timescale;
using NBomber.Sinks.Timescale.DAL;

new TimescaleDBReportingExample().Run();

public class TimescaleDBReportingExample
{
private const string CleanDbSql = $"""
DROP TABLE IF EXISTS {SqlQueries.StepStatsTable};
DROP TABLE IF EXISTS {SqlQueries.SessionsTable};
DROP TABLE IF EXISTS {TableNames.StepStatsTable};
DROP TABLE IF EXISTS {TableNames.SessionsTable};
""";

public void Run()
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/TimescaleBenchmark/ReadScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using RepoDb;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Sinks.Timescale;
using NBomber.Sinks.Timescale.Contracts;
using NBomber.Sinks.Timescale.DAL;

namespace TimescaleBenchmark;

Expand All @@ -25,7 +25,7 @@ public ScenarioProps Create(string connectionString, DateTime startTime, string

var st = endTime - TimeSpan.FromMinutes(10);

var dataStepStats = connection.QueryAsync<PointDbRecord>(SqlQueries.StepStatsTable,
var dataStepStats = connection.QueryAsync<PointDbRecord>(TableNames.StepStatsTable,
p => p.SessionId == sessionId
&& p.Time <= endTime
&& p.Time >= st);
Expand Down Expand Up @@ -119,7 +119,7 @@ public ScenarioProps Create(string connectionString, DateTime startTime, string

await using var connection = new NpgsqlConnection(connectionString);

var insert = connection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, fakePoints);
var insert = connection.BinaryBulkInsertAsync(TableNames.StepStatsTable, fakePoints);

await insert;
});
Expand Down
12 changes: 7 additions & 5 deletions benchmarks/TimescaleBenchmark/WriteScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using RepoDb;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Sinks.Timescale;
using NBomber.Sinks.Timescale.Contracts;
using NBomber.Sinks.Timescale.DAL;

namespace TimescaleBenchmark;

Expand All @@ -30,11 +30,13 @@ public ScenarioProps Create(string connectionString)
fakePoint.CurrentOperation = NBomber.Contracts.Stats.OperationType.Bombing;
try
{
await connection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, Enumerable.Repeat(fakePoint, 5));
await connection.BinaryBulkInsertAsync(TableNames.StepStatsTable, Enumerable.Repeat(fakePoint, 5));
}
catch (Exception ex)
{ }

catch
{
// ignored
}

return Response.Ok();
});

Expand Down
15 changes: 11 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
services:

timescaledb:
image: timescale/timescaledb:2.14.2-pg16
restart: always
image: timescale/timescaledb:2.14.2-pg16
command: postgres -c 'max_connections=500'
restart: always
environment:
POSTGRES_DB: metricsdb
POSTGRES_USER: timescaledb
Expand All @@ -12,7 +12,12 @@
- 5432:5432
volumes:
- metrics-data:/var/lib/postgresql/data

healthcheck:
test: [ "CMD-SHELL", "pg_isready -d 'user=timescaledb dbname=metricsdb'" ]
interval: 5s
timeout: 10s
retries: 5
start_period: 5s

pgadmin:
image: dpage/pgadmin4
Expand All @@ -24,10 +29,12 @@
- 5051:80
volumes:
- pgadmin-data:/var/lib/pgadmin
depends_on:
- timescaledb

volumes:
metrics-data:
driver: local

pgadmin-data:
driver: local
driver: local
16 changes: 16 additions & 0 deletions src/NBomber.Sinks.Timescale/Contracts/NodeInfoDbRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.ComponentModel.DataAnnotations.Schema;
using NBomber.Contracts.Stats;
using NBomber.Sinks.Timescale.DAL;

namespace NBomber.Sinks.Timescale.Contracts;

internal class NodeInfoDbRecord
{
[Column(ColumnNames.Time)] public DateTime Time { get; set; }
[Column(ColumnNames.SessionId)] public string SessionId { get; set; }
[Column(ColumnNames.CurrentOperation)] public OperationType CurrentOperation { get; set; }
[Column(ColumnNames.TestSuite)] public string TestSuite { get; set; }
[Column(ColumnNames.TestName)] public string TestName { get; set; }
[Column(ColumnNames.Metadata)] public string Metadata{ get; set; }
[Column(ColumnNames.NodeInfo)] public string NodeInfo { get; set; }
}
21 changes: 4 additions & 17 deletions src/NBomber.Sinks.Timescale/Contracts/PointDbRecord.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#pragma warning disable CS1591

namespace NBomber.Sinks.Timescale.Contracts;

using System.ComponentModel.DataAnnotations.Schema;
using NBomber.Contracts.Stats;
using NBomber.Sinks.Timescale.DAL;

public class PointDbRecord
namespace NBomber.Sinks.Timescale.Contracts;

internal class PointDbRecord
{
[Column(ColumnNames.Time)] public DateTime Time { get; set; }
[Column(ColumnNames.ScenarioTimestamp)] public TimeSpan ScenarioTimestamp { get; set; }
[Column(ColumnNames.SessionId)] public string SessionId { get; set; }
[Column(ColumnNames.CurrentOperation)] public OperationType CurrentOperation { get; set; }


[Column(ColumnNames.Scenario)] public string Scenario { get; set; }
[Column(ColumnNames.Step)] public string Step { get; set; }
Expand Down Expand Up @@ -63,15 +61,4 @@ public class PointDbRecord
[Column(ColumnNames.FailLatencyCount)] public string FailLatencyCount { get; set; }

[Column(ColumnNames.SimulationValue)] public int SimulationValue { get; set; }
}

public class NodeInfoDbRecord
{
[Column(ColumnNames.Time)] public DateTime Time { get; set; }
[Column(ColumnNames.SessionId)] public string SessionId { get; set; }
[Column(ColumnNames.CurrentOperation)] public OperationType CurrentOperation { get; set; }
[Column(ColumnNames.TestSuite)] public string TestSuite { get; set; }
[Column(ColumnNames.TestName)] public string TestName { get; set; }
[Column(ColumnNames.Metadata)] public string Metadata{ get; set; }
[Column(ColumnNames.NodeInfo)] public string NodeInfo { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
namespace NBomber.Sinks.Timescale.Contracts;
namespace NBomber.Sinks.Timescale.DAL;

public static class ColumnNames
internal static class TableNames
{
public const string StepStatsTable = "nb_step_stats";
public const string SessionsTable = "nb_sessions";
public const string SchemaVersionTable = "nb_sink_schema_version";
}

internal static class ColumnNames
{
public const string Time = "time";
public const string ScenarioTimestamp = "scenario_timestamp";
Expand Down Expand Up @@ -64,5 +71,5 @@ public static class ColumnNames

public const string SimulationValue = "simulation_value";

public const string Version = "Version";
public const string Version = "version";
}
84 changes: 84 additions & 0 deletions src/NBomber.Sinks.Timescale/DAL/DbMigrations .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Npgsql;
using RepoDb;
using ILogger = Serilog.ILogger;

namespace NBomber.Sinks.Timescale.DAL;

internal class DbMigrations(string connectionString, ILogger logger)
{
public const int SinkSchemaVersion = 0;

public async Task Run()
{
var currentDbVersion = await GetCurrendDbVersion();

if (currentDbVersion > SinkSchemaVersion)
{
var errMessage = $@"Your NBomber.Sinks.Timescale schema version: '{SinkSchemaVersion}' is not compatible with DB schema version: '{currentDbVersion}'";
logger.Error(errMessage);
throw new PlatformNotSupportedException(errMessage);
}
else if (currentDbVersion < SinkSchemaVersion)
{
for (var v = currentDbVersion + 1; v <= SinkSchemaVersion; v++)
{
await ApplyMigration(v);
}
}
}

private async Task<int> GetCurrendDbVersion()
{
try
{
using var connection = new NpgsqlConnection(connectionString);
var result = await connection.ExecuteQueryAsync<int>($@"SELECT ""{ColumnNames.Version}"" FROM {TableNames.SchemaVersionTable};");
var currentDbVersion = result.FirstOrDefault();
return currentDbVersion;
}
catch (Exception ex)
{
logger.Error(ex, ex.Message);
return -1;
}
}

private async Task ApplyMigration(int version)
{
await using var connection = new NpgsqlConnection(connectionString);

switch (version)
{
case 0:
await connection.ExecuteNonQueryAsync(
SqlQueries.CreateStepStatsTable
+ SqlQueries.CreateSessionsTable
+ SqlQueries.CreateDbSchemaVersion);

await connection.ExecuteNonQueryAsync($@"
INSERT INTO {TableNames.SchemaVersionTable} (""{ColumnNames.Version}"")
VALUES ({version})
;");

logger.Debug("Created initial tables");
break;

//case 1:
// await connection.ExecuteNonQueryAsync($@"
// ALTER TABLE {SqlQueries.StepStatsTable}
// ADD COLUMN IF NOT EXISTS {ColumnNames.TestCulomn} TEXT
// ;

// WITH updated AS (
// UPDATE {SqlQueries.DbSchemaVersion}
// SET ""{ColumnNames.Version}"" = {version}
// RETURNING *
// )
// INSERT INTO {SqlQueries.DbSchemaVersion} (""{ColumnNames.Version}"")
// SELECT 1
// WHERE NOT EXISTS (SELECT * FROM updated);");
// break;
}
}
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
#pragma warning disable CS1591
namespace NBomber.Sinks.Timescale.DAL;

using NBomber.Sinks.Timescale.Contracts;

namespace NBomber.Sinks.Timescale;

public static class SqlQueries
internal static class SqlQueries
{
public const string StepStatsTable = "nb_step_stats";
public const string SessionsTable = "nb_sessions";
public const string DbSchemaVersion = "nb_sink_schema_version";
public static string CreateStepStatsTable => $@"
CREATE TABLE IF NOT EXISTS ""{StepStatsTable}""
CREATE TABLE IF NOT EXISTS ""{TableNames.StepStatsTable}""
(
""{ColumnNames.Time}"" TIMESTAMPTZ NOT NULL,
""{ColumnNames.ScenarioTimestamp}"" TIME WITHOUT TIME ZONE NOT NULL,
Expand Down Expand Up @@ -68,12 +61,12 @@ CREATE TABLE IF NOT EXISTS ""{StepStatsTable}""
""{ColumnNames.SimulationValue}"" INT
);
SELECT create_hypertable('{StepStatsTable}', by_range('{ColumnNames.Time}', INTERVAL '1 day'), if_not_exists => TRUE);
CREATE INDEX IF NOT EXISTS {ColumnNames.SessionId}_index ON {StepStatsTable} ({ColumnNames.SessionId});
SELECT create_hypertable('{TableNames.StepStatsTable}', by_range('{ColumnNames.Time}', INTERVAL '1 day'), if_not_exists => TRUE);
CREATE INDEX IF NOT EXISTS {ColumnNames.SessionId}_index ON {TableNames.StepStatsTable} ({ColumnNames.SessionId});
";

public static string CreateSessionsTable => $@"
CREATE TABLE IF NOT EXISTS ""{SessionsTable}""
CREATE TABLE IF NOT EXISTS ""{TableNames.SessionsTable}""
(
""{ColumnNames.Time}"" TIMESTAMPTZ NOT NULL,
""{ColumnNames.SessionId}"" TEXT PRIMARY KEY,
Expand All @@ -86,9 +79,9 @@ CREATE TABLE IF NOT EXISTS ""{SessionsTable}""
";

public static string CreateDbSchemaVersion => $@"
CREATE TABLE IF NOT EXISTS ""{DbSchemaVersion}""
CREATE TABLE IF NOT EXISTS ""{TableNames.SchemaVersionTable}""
(
""{ColumnNames.Version}"" INT PRIMARY KEY
);
);
";
}
Loading

0 comments on commit e046809

Please sign in to comment.