Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored storage layer #7

Merged
merged 7 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benchmarks/TimescaleBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TimescaleDBReportingExample
{
private const string CleanDbSql = $"""
DROP TABLE IF EXISTS {SqlQueries.StepStatsTable};
DROP TABLE IF EXISTS {SqlQueries.ClusterStatsTableName};
DROP TABLE IF EXISTS {SqlQueries.SessionsTable};
""";

public void Run()
Expand All @@ -20,12 +20,12 @@ public void Run()
.UsePostgreSql();

const string connectionString = "Host=localhost;Port=5432;Database=metricsdb;Username=timescaledb;Password=timescaledb;Pooling=true;Maximum Pool Size=300;";

using var connection = new NpgsqlConnection(connectionString);

connection.ExecuteNonQuery(CleanDbSql);

connection.ExecuteNonQuery(SqlQueries.CreateStepStatsTable + SqlQueries.CreateClusterStatsTable);
connection.ExecuteNonQuery(SqlQueries.CreateStepStatsTable + SqlQueries.CreateSessionsTable);

var writeScenario = new WriteScenario().Create(connectionString);
var readScenario = new ReadScenario().Create(connectionString, DateTime.UtcNow, sessionId: "0");
Expand Down
2 changes: 0 additions & 2 deletions benchmarks/TimescaleBenchmark/ReadScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ public ScenarioProps Create(string connectionString, DateTime startTime, string
SimulationValue = fakePoint.SimulationValue,
SessionId = sessionId,
CurrentOperation = fakePoint.CurrentOperation,
TestSuite = fakePoint.TestSuite,
TestName = fakePoint.TestName,

OkStatusCodes = JsonSerializer.Serialize(fakePoint.OkStatusCodes),
OkLatencyCount = JsonSerializer.Serialize(fakePoint.OkLatencyCount),
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/TimescaleBenchmark/WriteScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public ScenarioProps Create(string connectionString)

fakePoint.Time = curTime;
fakePoint.SessionId = ctx.ScenarioInfo.InstanceNumber.ToString();

fakePoint.CurrentOperation = NBomber.Contracts.Stats.OperationType.Bombing;

await connection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, Enumerable.Repeat(fakePoint, 5));

return Response.Ok();
Expand Down
2 changes: 1 addition & 1 deletion examples/Demo/infra-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"TimescaleDbSink":{
"TimescaleDbSink": {
"ConnectionString": "Host=localhost;Port=5432;Database=metricsdb;Username=timescaledb;Password=timescaledb;Pooling=true;Maximum Pool Size=300;"
}
}
6 changes: 4 additions & 2 deletions src/NBomber.Sinks.Timescale/Contracts/PointDbRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class PointDbRecord
[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.Scenario)] public string Scenario { get; set; }
[Column(ColumnNames.Step)] public string Step { get; set; }

Expand Down Expand Up @@ -68,5 +67,8 @@ 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.NodeInfo)] public string NodeInfo { get; set; }
}
21 changes: 10 additions & 11 deletions src/NBomber.Sinks.Timescale/SqlQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace NBomber.Sinks.Timescale;

public static class SqlQueries
{
public const string StepStatsTable = "step_stats";
public const string ClusterStatsTableName = "cluster_stats";
public const string StepStatsTable = "nb_step_stats";
public const string SessionsTable = "nb_sessions";

public static string CreateStepStatsTable => $@"
CREATE TABLE IF NOT EXISTS ""{StepStatsTable}""
Expand All @@ -16,8 +16,6 @@ CREATE TABLE IF NOT EXISTS ""{StepStatsTable}""
""{ColumnNames.SessionId}"" TEXT NOT NULL,
""{ColumnNames.CurrentOperation}"" TEXT,

""{ColumnNames.TestSuite}"" TEXT,
""{ColumnNames.TestName}"" TEXT,
""{ColumnNames.Scenario}"" TEXT,
""{ColumnNames.Step}"" TEXT,

Expand Down Expand Up @@ -72,14 +70,15 @@ CREATE TABLE IF NOT EXISTS ""{StepStatsTable}""
CREATE INDEX IF NOT EXISTS {ColumnNames.SessionId}_index ON {StepStatsTable} ({ColumnNames.SessionId});
";

public static string CreateClusterStatsTable => $@"
CREATE TABLE IF NOT EXISTS ""{ClusterStatsTableName}""
public static string CreateSessionsTable => $@"
CREATE TABLE IF NOT EXISTS ""{SessionsTable}""
(
""{ColumnNames.Time}"" TIMESTAMPTZ NOT NULL,
""{ColumnNames.SessionId}"" TEXT NOT NULL,
""{ColumnNames.SessionId}"" TEXT PRIMARY KEY,
""{ColumnNames.CurrentOperation}"" TEXT,
""{ColumnNames.TestSuite}"" TEXT,
""{ColumnNames.TestName}"" TEXT,
""{ColumnNames.NodeInfo}"" JSONB
);
SELECT create_hypertable('{ClusterStatsTableName}', by_range('{ColumnNames.Time}', INTERVAL '1 day'), if_not_exists => TRUE);
CREATE INDEX IF NOT EXISTS {ColumnNames.SessionId}_index ON {ClusterStatsTableName} ({ColumnNames.SessionId});
);
";
}
}
45 changes: 36 additions & 9 deletions src/NBomber.Sinks.Timescale/TimescaleDbSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NBomber.Contracts;
using NBomber.Contracts.Stats;
using NBomber.Sinks.Timescale.Contracts;
using System.Data;

namespace NBomber.Sinks.Timescale;

Expand Down Expand Up @@ -64,7 +65,7 @@ public async Task Init(IBaseContext context, IConfiguration infraConfig)

await _mainConnection.ExecuteNonQueryAsync(
SqlQueries.CreateStepStatsTable
+ SqlQueries.CreateClusterStatsTable
+ SqlQueries.CreateSessionsTable
);
}

Expand All @@ -73,21 +74,25 @@ public async Task Start()
if (_mainConnection != null)
{
var nodeInfo = _context.GetNodeInfo();

var testInfo = _context.TestInfo;

var record = new NodeInfoDbRecord
{
Time = DateTime.UtcNow,
SessionId = _context.TestInfo.SessionId,
SessionId = testInfo.SessionId,
CurrentOperation = OperationType.Bombing,
TestSuite = testInfo.TestSuite,
TestName = testInfo.TestName,
NodeInfo = Json.serialize(nodeInfo)
};

await _mainConnection.BinaryBulkInsertAsync(SqlQueries.ClusterStatsTableName, new [] { record });
await _mainConnection.BinaryBulkInsertAsync(SqlQueries.SessionsTable, new [] { record });
}
}

public Task SaveRealtimeStats(ScenarioStats[] stats) => SaveScenarioStats(stats);

public Task SaveFinalStats(NodeStats stats) => SaveScenarioStats(stats.ScenarioStats);
public Task SaveFinalStats(NodeStats stats) => SaveScenarioStats(stats.ScenarioStats, isFinal: true);

public Task Stop() => Task.CompletedTask;

Expand All @@ -97,7 +102,7 @@ public void Dispose()
_mainConnection.Dispose();
}

private async Task SaveScenarioStats(ScenarioStats[] stats)
private async Task SaveScenarioStats(ScenarioStats[] stats, bool isFinal = false)
{
if (_mainConnection != null)
{
Expand All @@ -107,7 +112,31 @@ private async Task SaveScenarioStats(ScenarioStats[] stats)
.SelectMany(step => MapToPoint(step, currentTime))
.ToArray();

await _mainConnection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, points);
if (!isFinal)
{
await _mainConnection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, points);
}
else
{
using (var transaction = _mainConnection.EnsureOpen().BeginTransaction())
{
await _mainConnection.BinaryBulkInsertAsync(SqlQueries.StepStatsTable, points, transaction: (NpgsqlTransaction) transaction);

var testInfo = _context.TestInfo;

var queryEntity = new NodeInfoDbRecord
{
SessionId = testInfo.SessionId,
CurrentOperation = OperationType.Complete,
};

var fields = Field.Parse<NodeInfoDbRecord>(e => e.CurrentOperation);

await _mainConnection.UpdateAsync(SqlQueries.SessionsTable, queryEntity, fields: fields, transaction: transaction);

transaction.Commit();
}
}
}
}

Expand Down Expand Up @@ -143,8 +172,6 @@ private PointDbRecord[] MapToPoint(ScenarioStats scnStats, DateTime currentTime)
Time = currentTime,
SessionId = testInfo.SessionId,
CurrentOperation = nodeInfo.CurrentOperation,
TestSuite = testInfo.TestSuite,
TestName = testInfo.TestName,
Scenario = scnStats.ScenarioName,
Step = step.StepName,

Expand Down
Loading