Skip to content

Commit

Permalink
Create/apply aspect in order to measure execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
vieirandre committed Jul 20, 2020
1 parent cb3628f commit f235c9b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Aspects/ExecutionTimeMeasured.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using AspectInjector.Broker;
using NLog;
using System;
using System.Diagnostics;

namespace Mycenae.Aspects
{
[Aspect(Scope.PerInstance)]
[Injection(typeof(ExecutionTimeMeasured))]
public class ExecutionTimeMeasured : Attribute
{
private readonly ILogger _logger = LogManager.GetCurrentClassLogger();
private Stopwatch _stopwatch;

[Advice(Kind.Before)]
public void StartMeasurement()
{
_stopwatch = Stopwatch.StartNew();
}

[Advice(Kind.After)]
public void FinishMeasurement([Argument(Source.Name)] string name)
{
_stopwatch.Stop();

_logger.Info($"Time elapsed ({name}): {_stopwatch.Elapsed.Hours}h:{_stopwatch.Elapsed.Minutes}m:" +
$"{_stopwatch.Elapsed.Seconds}s:{_stopwatch.Elapsed.Milliseconds}ms");
}
}
}
1 change: 1 addition & 0 deletions Mycenae.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AspectInjector" Version="2.4.0" />
<PackageReference Include="CassandraCSharpDriver" Version="3.15.0" />
<PackageReference Include="CsvHelper" Version="15.0.5" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" />
Expand Down
2 changes: 2 additions & 0 deletions Tasks/EndToEnd.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cassandra;
using Mycenae.Aspects;
using Mycenae.Models;
using NLog;
using System;
Expand All @@ -11,6 +12,7 @@ internal class EndToEnd : MigrationTask
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

[ExecutionTimeMeasured]
internal override void Execute()
{
Logger.Info("Starting end-to-end migration...");
Expand Down
2 changes: 2 additions & 0 deletions Tasks/Extraction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cassandra;
using Mycenae.Aspects;
using Mycenae.Models;
using NLog;
using System;
Expand All @@ -13,6 +14,7 @@ internal class Extraction : MigrationTask
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

[ExecutionTimeMeasured]
internal override void Execute()
{
Logger.Info("Starting extraction phase...");
Expand Down
2 changes: 2 additions & 0 deletions Tasks/Insertion.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Cassandra;
using CsvHelper;
using Mycenae.Aspects;
using Mycenae.Converters;
using Mycenae.Models;
using NLog;
Expand All @@ -16,6 +17,7 @@ internal class Insertion : MigrationTask
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

[ExecutionTimeMeasured]
internal override void Execute()
{
Logger.Info("Starting insertion phase...");
Expand Down
2 changes: 2 additions & 0 deletions Tasks/MigrationTask.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Cassandra;
using Mycenae.Aspects;
using Mycenae.Models;
using Mycenae.Policies;
using NLog;
Expand Down Expand Up @@ -112,6 +113,7 @@ protected static IList<CColumn> GetColumnsInfo(string keyspace, string table)
return results.Columns.Select(column => new CColumn(column.Name, column.Type)).ToList();
}

[ExecutionTimeMeasured]
protected static async Task ExecuteInsertAsync(IList<BoundStatement> insertStatements)
{
Logger.Info($"Inserting {insertStatements.Count} records into table...");
Expand Down

0 comments on commit f235c9b

Please sign in to comment.