Skip to content

Commit

Permalink
Add Stopwatch to log execution time of remote operations
Browse files Browse the repository at this point in the history
Introduce Stopwatch to AxoDataExchange class to measure execution time.
Add private Stopwatch field `sw` to the class.
Modify RemoteCreate, RemoteRead, RemoteUpdate, RemoteDelete,
RemoteEntityExist, and RemoteCreateOrUpdate methods to start and stop
the stopwatch. Log the elapsed time with record identifier, data
exchange symbol, and time in milliseconds using AxoApplication.Current.Logger.Information.
  • Loading branch information
PTKu committed Dec 10, 2024
1 parent b62e689 commit a218475
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/data/src/AXOpen.Data/DataExchange/AxoDataExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Third party licenses: https://github.com/ix-ax/axsharp/blob/dev/notices.md

using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Compression;
using System.Linq;
using System.Linq.Expressions;
Expand Down Expand Up @@ -181,16 +182,18 @@ public IEnumerable<IBrowsableDataObject> GetRecords(string identifier)
return DataRepository.GetRecords(identifier).Cast<IBrowsableDataObject>();
}

private Stopwatch sw = new Stopwatch();

/// <inheritdoc />
public async Task<bool> RemoteCreate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();
Repository.Create(identifier, cloned);

sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' created in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

Expand All @@ -199,9 +202,12 @@ public async Task<bool> RemoteRead(string identifier)
{
try
{
sw.Restart();
await Operation.ReadAsync();
var record = Repository.Read(identifier);
await ((ITwinObject)DataEntity).PlainToOnline(record);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' read from '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}
catch (Exception exception)
Expand All @@ -213,36 +219,47 @@ public async Task<bool> RemoteRead(string identifier)
/// <inheritdoc />
public async Task<bool> RemoteUpdate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

var cloned = await ((ITwinObject)DataEntity).OnlineToPlain<TPlain>();

cloned.Hash = HashHelper.CreateHash(cloned);
Repository.Update(identifier, cloned);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' updated in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

/// <inheritdoc />
public async Task<bool> RemoteDelete(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);
Repository.Delete(identifier);
sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' deleted in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return true;
}

/// <inheritdoc />
public async Task<bool> RemoteEntityExist(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);
return Repository.Exists(identifier);
var retVal = Repository.Exists(identifier);
sw.Stop();
AxoApplication.Current.Logger.Information($"Information about record '{identifier}' existence in '{this.Symbol}' retrieved in '{sw.ElapsedMilliseconds} ms'", this, AxoApplication.Current.ControllerIdentity);
return retVal;
}

/// <inheritdoc />
public async Task<bool> RemoteCreateOrUpdate(string identifier)
{
sw.Restart();
await Operation.ReadAsync();
await DataEntity.DataEntityId.SetAsync(identifier);

Expand All @@ -258,6 +275,10 @@ public async Task<bool> RemoteCreateOrUpdate(string identifier)
{
Repository.Create(identifier, cloned);
}

sw.Stop();
AxoApplication.Current.Logger.Information($"Record '{identifier}' created in '{this.Symbol}' in '{sw.ElapsedMilliseconds} ms' using `Create or update` function.", this, AxoApplication.Current.ControllerIdentity);

return true;
}

Expand Down

0 comments on commit a218475

Please sign in to comment.