Permalink
Browse files

Update #283 - Switched over ado stuff to use much higher resolution t…

…imers
  • Loading branch information...
1 parent 38c1208 commit b6a0085a728a98ca8ab3c10e0158369c37d7428d @avanderhoorn avanderhoorn committed Mar 23, 2013
@@ -13,40 +13,49 @@
using Glimpse.Ado.Message;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;
+using Glimpse.Core.Message;
namespace Glimpse.Ado.AlternateType
{
public class GlimpseDbCommand : DbCommand
{
private IMessageBroker messageBroker;
+ private IExecutionTimer timerStrategy;
public GlimpseDbCommand(DbCommand innerCommand)
{
InnerCommand = innerCommand;
}
- public GlimpseDbCommand(DbCommand innerCommand, GlimpseDbConnection connection) :
- this(innerCommand)
+ public GlimpseDbCommand(DbCommand innerCommand, GlimpseDbConnection connection)
+ : this(innerCommand)
{
InnerConnection = connection;
}
-
- public GlimpseDbCommand(DbCommand innerCommand, GlimpseDbConnection connection, IMessageBroker messageBroker) :
- this(innerCommand, connection)
+
+ public GlimpseDbCommand(DbCommand innerCommand, GlimpseDbConnection connection, IMessageBroker messageBroker, IExecutionTimer timerStrategy)
+ : this(innerCommand, connection)
{
MessageBroker = messageBroker;
+ TimerStrategy = timerStrategy;
}
- private DbCommand InnerCommand { get; set; }
+ public DbCommand InnerCommand { get; set; }
- private GlimpseDbConnection InnerConnection { get; set; }
+ public GlimpseDbConnection InnerConnection { get; set; }
private IMessageBroker MessageBroker
{
get { return messageBroker ?? (messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker()); }
set { messageBroker = value; }
}
+ private IExecutionTimer TimerStrategy
+ {
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ set { timerStrategy = value; }
+ }
+
public override string CommandText
{
get { return InnerCommand.CommandText; }
@@ -166,21 +175,19 @@ protected override DbDataReader ExecuteDbDataReader(CommandBehavior behavior)
DbDataReader reader;
var commandId = Guid.NewGuid();
- LogCommandStart(commandId);
- var stopwatch = Stopwatch.StartNew();
+ var timer = TimerStrategy.Start();
+ LogCommandStart(commandId, timer);
try
{
reader = InnerCommand.ExecuteReader(behavior);
}
catch (Exception exception)
{
- stopwatch.Stop();
- LogCommandError(commandId, stopwatch.Elapsed, exception);
+ LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
throw;
}
- stopwatch.Stop();
- LogCommandEnd(commandId, stopwatch.Elapsed, reader.RecordsAffected);
+ LogCommandEnd(commandId, TimerStrategy.Stop(timer), reader.RecordsAffected);
return new GlimpseDbDataReader(reader, InnerCommand, InnerConnection.ConnectionId, commandId);
}
@@ -190,20 +197,18 @@ public override int ExecuteNonQuery()
int num;
var commandId = Guid.NewGuid();
- LogCommandStart(commandId);
- var stopwatch = Stopwatch.StartNew();
+ var timer = TimerStrategy.Start();
+ LogCommandStart(commandId, timer);
try
{
num = InnerCommand.ExecuteNonQuery();
}
catch (Exception exception)
- {
- stopwatch.Stop();
- LogCommandError(commandId, stopwatch.Elapsed, exception);
+ {
+ LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
throw;
- }
- stopwatch.Stop();
- LogCommandEnd(commandId, stopwatch.Elapsed, num);
+ }
+ LogCommandEnd(commandId, TimerStrategy.Stop(timer), num);
return num;
}
@@ -213,19 +218,18 @@ public override object ExecuteScalar()
object result;
var commandId = Guid.NewGuid();
- LogCommandStart(commandId);
- var stopwatch = Stopwatch.StartNew();
+ var timer = TimerStrategy.Start();
+ LogCommandStart(commandId, timer);
try
{
result = InnerCommand.ExecuteScalar();
}
catch (Exception exception)
- {
- stopwatch.Stop();
- LogCommandError(commandId, stopwatch.Elapsed, exception);
+ {
+ LogCommandError(commandId, TimerStrategy.Stop(timer), exception);
throw;
}
- LogCommandEnd(commandId, stopwatch.Elapsed, null);
+ LogCommandEnd(commandId, TimerStrategy.Stop(timer), null);
return result;
}
@@ -282,7 +286,7 @@ private static object GetParameterValue(IDataParameter parameter)
return parameter.Value;
}
- private void LogCommandStart(Guid commandId)
+ private void LogCommandStart(Guid commandId, TimeSpan timerTimeSpan)
{
if (MessageBroker != null)
{
@@ -302,23 +306,29 @@ private void LogCommandStart(Guid commandId)
}
}
- MessageBroker.Publish(new CommandExecutedMessage(InnerConnection.ConnectionId, commandId, InnerCommand.CommandText, parameters));
+ MessageBroker.Publish(
+ new CommandExecutedMessage(InnerConnection.ConnectionId, commandId, InnerCommand.CommandText, parameters)
+ .AsTimedMessage(timerTimeSpan));
}
}
- private void LogCommandEnd(Guid commandId, TimeSpan elapsed, int? recordsAffected)
+ private void LogCommandEnd(Guid commandId, TimerResult timerResult, int? recordsAffected)
{
if (MessageBroker != null)
{
- MessageBroker.Publish(new CommandDurationAndRowCountMessage(InnerConnection.ConnectionId, commandId, elapsed, recordsAffected));
+ MessageBroker.Publish(
+ new CommandDurationAndRowCountMessage(InnerConnection.ConnectionId, commandId, recordsAffected)
+ .AsTimedMessage(timerResult));
}
}
- private void LogCommandError(Guid commandId, TimeSpan elapsed, Exception exception)
+ private void LogCommandError(Guid commandId, TimerResult timerResult, Exception exception)
{
if (MessageBroker != null)
{
- MessageBroker.Publish(new CommandErrorMessage(InnerConnection.ConnectionId, commandId, elapsed, exception));
+ MessageBroker.Publish(
+ new CommandErrorMessage(InnerConnection.ConnectionId, commandId, exception)
+ .AsTimedMessage(timerResult));
}
}
#endregion
@@ -6,20 +6,19 @@
using Glimpse.Ado.Message;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;
+using Glimpse.Core.Message;
namespace Glimpse.Ado.AlternateType
{
public class GlimpseDbConnection : DbConnection
{
private IMessageBroker messageBroker;
+ private IExecutionTimer timerStrategy;
+ private TimeSpan timerTimeSpan;
public GlimpseDbConnection(DbConnection connection)
- {
- var providerFactory = connection.TryGetProfiledProviderFactory();
-
- InnerConnection = connection;
- InnerProviderFactory = providerFactory;
- ConnectionId = Guid.NewGuid();
+ : this(connection, connection.TryGetProfiledProviderFactory())
+ {
}
public GlimpseDbConnection(DbConnection connection, DbProviderFactory providerFactory)
@@ -37,30 +36,37 @@ public GlimpseDbConnection(DbConnection connection, DbProviderFactory providerFa
{
if (connection.State == ConnectionState.Open)
{
- MessageBroker.Publish(new ConnectionStartedMessage(ConnectionId));
+ OpenConnection();
}
connection.StateChange += StateChangeHaneler;
}
}
- public GlimpseDbConnection(DbConnection connection, DbProviderFactory providerFactory, Guid connectionId, IMessageBroker messageBroker)
+ public GlimpseDbConnection(DbConnection connection, DbProviderFactory providerFactory, Guid connectionId, IMessageBroker messageBroker, IExecutionTimer timerStrategy)
: this(connection, providerFactory, connectionId)
{
MessageBroker = messageBroker;
+ TimerStrategy = timerStrategy;
}
+ public DbProviderFactory InnerProviderFactory { get; set; }
+
+ public DbConnection InnerConnection { get; set; }
+
+ public Guid ConnectionId { get; set; }
+
private IMessageBroker MessageBroker
{
get { return messageBroker ?? (messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker()); }
set { messageBroker = value; }
}
- public DbProviderFactory InnerProviderFactory { get; set; }
-
- public DbConnection InnerConnection { get; set; }
-
- public Guid ConnectionId { get; set; }
+ private IExecutionTimer TimerStrategy
+ {
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ set { timerStrategy = value; }
+ }
public override string ConnectionString
{
@@ -179,7 +185,7 @@ protected override void Dispose(bool disposing)
InnerProviderFactory = null;
base.Dispose(disposing);
}
-
+
private void OnDtcTransactionCompleted(object sender, TransactionEventArgs args)
{
TransactionStatus aborted;
@@ -202,12 +208,28 @@ private void StateChangeHaneler(object sender, StateChangeEventArgs args)
{
if (args.CurrentState == ConnectionState.Open)
{
- MessageBroker.Publish(new ConnectionStartedMessage(ConnectionId));
+ OpenConnection();
}
else if (args.CurrentState == ConnectionState.Closed)
{
- MessageBroker.Publish(new ConnectionClosedMessage(ConnectionId));
- }
+ ClosedConnection();
+ }
+ }
+
+ private void OpenConnection()
+ {
+ timerTimeSpan = TimerStrategy.Start();
+
+ MessageBroker.Publish(
+ new ConnectionStartedMessage(ConnectionId)
+ .AsTimedMessage(timerTimeSpan));
+ }
+
+ private void ClosedConnection()
+ {
+ MessageBroker.Publish(
+ new ConnectionClosedMessage(ConnectionId)
+ .AsTimedMessage(TimerStrategy.Stop(timerTimeSpan)));
}
}
}
@@ -6,12 +6,14 @@
using Glimpse.Ado.Message;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Framework;
+using Glimpse.Core.Message;
namespace Glimpse.Ado.AlternateType
{
internal class GlimpseDbDataReader : DbDataReader
{
private IMessageBroker messageBroker;
+ private IExecutionTimer timerStrategy;
public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId)
{
@@ -21,10 +23,11 @@ public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid conn
CommandId = commandId;
}
- public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId, IMessageBroker messageBroker)
+ public GlimpseDbDataReader(DbDataReader dataReader, DbCommand command, Guid connectionId, Guid commandId, IMessageBroker messageBroker, IExecutionTimer timerStrategy)
: this(dataReader, command, connectionId, commandId)
{
MessageBroker = messageBroker;
+ TimerStrategy = timerStrategy;
}
private DbDataReader InnerDataReader { get; set; }
@@ -44,6 +47,12 @@ private IMessageBroker MessageBroker
get { return messageBroker ?? (messageBroker = GlimpseConfiguration.GetConfiguredMessageBroker()); }
set { messageBroker = value; }
}
+
+ private IExecutionTimer TimerStrategy
+ {
+ get { return timerStrategy ?? (timerStrategy = GlimpseConfiguration.GetConfiguredTimerStrategy()()); }
+ set { timerStrategy = value; }
+ }
public override int Depth
{
@@ -89,7 +98,9 @@ public override void Close()
{
if (MessageBroker != null)
{
- MessageBroker.Publish(new CommandRowCountMessage(ConnectionId, CommandId, RowCount));
+ MessageBroker.Publish(
+ new CommandRowCountMessage(ConnectionId, CommandId, RowCount)
+ .AsTimedMessage(TimeSpan.Zero));
}
var inner = InnerDataReader as SqlDataReader;
Oops, something went wrong.

0 comments on commit b6a0085

Please sign in to comment.