Пример #1
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return the <see cref="IDataReader"/> instance if it is an ExecuteReader operation.
        ///     If it is not ExecuteReader, it should return null.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual void ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func <IDataReader> execute, TagCollection tags)
        {
            if (execute == null)
            {
                return;
            }

            if (command == null)
            {
                execute();
                return;
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            var dataReader = execute();

            if (dataReader == null)
            {
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return;
            }

            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                         new ProfiledDbDataReader(dataReader, this);

            _inProgressDataReaders[reader] = dbTiming;
        }
Пример #2
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/> asynchronously.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return a scalar value.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public async Task <object> ExecuteDbCommandAsync(DbExecuteType executeType, DbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (executeType == DbExecuteType.Reader)
            {
                throw new NotSupportedException("ExecuteDbCommandAsync doesn't support executing data reader.");
            }

            if (command == null)
            {
                return(await execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            try
            {
                return(await execute());
            }
            finally
            {
                dbTiming.Stop();
            }
        }
Пример #3
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual async Task <object> ExecuteCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (command == null)
            {
                return(execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };
            var result = await execute();

            var dataReader = result as DbDataReader;

            if (dataReader == null)
            {
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return(result);
            }

            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                         new ProfiledDbDataReader(dataReader, this);

            _inProgressDataReaders[reader] = dbTiming;

            return(reader);
        }
Пример #4
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return the <see cref="DbDataReader"/> instance if it is an ExecuteReader operation.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual Task <DbDataReader> ExecuteDbDataReaderCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <DbDataReader> > execute, TagCollection tags)
        {
            if (command == null)
            {
                return(execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            return(execute().ContinueWith <DbDataReader>(r =>
            {
                var dataReader = r.Result;
                if (dataReader == null)
                {
                    // if not executing reader, stop the sql timing right after execute()
                    dbTiming.Stop();
                    return null;
                }

                dbTiming.FirstFetch();
                var reader = dataReader as ProfiledDbDataReader ??
                             new ProfiledDbDataReader(dataReader, this);
                _inProgressDataReaders[reader] = dbTiming;

                return reader;
            }));
        }
Пример #5
0
        /// <summary>
        /// Initializes a <see cref="DbTiming"/>.
        /// </summary>
        /// <param name="profiler">
        ///     The <see cref="IProfiler"/> where
        ///     to add the timing to when stops.
        /// </param>
        /// <param name="executeType">The <see cref="DbExecuteType"/> of the <see cref="DbCommand"/> being executed &amp; profiled.</param>
        /// <param name="command">The <see cref="DbCommand"/> being executed &amp; profiled.</param>
        public DbTiming(
            IProfiler profiler, DbExecuteType executeType, DbCommand command)
            : base(profiler, "db", ProfilingSession.ProfilingSessionContainer.CurrentSessionStepId, command == null ? null : command.CommandText, null)
        {
            if (profiler == null)
            {
                throw new ArgumentNullException("profiler");
            }
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            _profiler         = profiler;
            StartMilliseconds = (long)_profiler.Elapsed.TotalMilliseconds;
            Sort = profiler.Elapsed.Ticks;
            Data = new Dictionary <string, string>();

            Data["executeType"] = executeType.ToString().ToLowerInvariant();

            if (command.Parameters == null || command.Parameters.Count == 0)
            {
                return;
            }

            Data["parameters"] = SerializeParameters(command.Parameters);
        }
Пример #6
0
 /// <summary>
 /// Initializes a <see cref="DbTiming"/>.
 /// </summary>
 /// <param name="profiler">
 ///     The <see cref="IProfiler"/> where
 ///     to add the <see cref="CustomTiming"/> to when stops.
 /// </param>
 /// <param name="executeType">The <see cref="DbExecuteType"/> of the <see cref="IDbCommand"/> being executed &amp; profiled.</param>
 /// <param name="command">The <see cref="IDbCommand"/> being executed &amp; profiled.</param>
 public DbTiming(
     IProfiler profiler, DbExecuteType executeType, IDbCommand command)
     : base(profiler, "db", command == null ? null : command.CommandText)
 {
     _profiler = profiler;
     DbExecuteType = executeType;
     ExecuteType = executeType.ToString().ToLowerInvariant();
     if (command != null)
     {
         Parameters = new DbTimingParameterCollection(command.Parameters);
         InputSize = Parameters.Size;
         InputData = ToXml(Parameters);
     }
 }
Пример #7
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/> asynchronously.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return a scalar value.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public async Task <object> ExecuteDbCommandAsync(DbExecuteType executeType, DbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (command == null)
            {
                return(await execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            if (executeType == DbExecuteType.Reader)
            {
                // for ExecuteReader
                var dataReader = (await execute()) as DbDataReader;
                if (dataReader == null)
                {
                    // if not executing reader, stop the sql timing right after execute()
                    dbTiming.Stop();
                    return(null);
                }

                dbTiming.FirstFetch();
                var reader = dataReader as ProfiledDbDataReader ??
                             new ProfiledDbDataReader(dataReader, this);
                _inProgressDataReaders[reader] = dbTiming;

                return(reader);
            }

            // for ExecuteNonQuery and ExecuteScalar
            try
            {
                return(await execute());
            }
            finally
            {
                dbTiming.Stop();
            }
        }
Пример #8
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return the number of affected rows if it is an ExecuteNonQuery operation.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual Task <int> ExecuteNonQueryCommandAsync(DbExecuteType executeType, IDbCommand command, Func <Task <int> > execute, TagCollection tags)
        {
            if (command == null)
            {
                return(execute());
            }

            var dbTiming = new DbTiming(_profiler, executeType, command)
            {
                Tags = tags
            };

            return(execute().ContinueWith(r =>
            {
                dbTiming.Stop();
                return r.Result;
            }));
        }
Пример #9
0
 void IDbProfiler.ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func<IDataReader> execute, IEnumerable<string> tags)
 {
     ExecuteDbCommand(executeType, command, execute, tags);
 }
Пример #10
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler, 
        ///     which should return the <see cref="IDataReader"/> instance if it is an ExecuteReader operation.
        ///     If it is not ExecuteReader, it should return null.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual void ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func<IDataReader> execute, IEnumerable<string> tags)
        {
            if (execute == null)
            {
                return;
            }

            if (command == null)
            {
                execute();
                return;
            }

            var dbTiming = new DbTiming(_profiler, executeType, command);
            if (tags != null && tags.Any())
            {
                dbTiming.Tags = new TagCollection(tags);
            }

            var dataReader = execute();
            if (dataReader == null)
            {
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return;
            }

            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                new ProfiledDbDataReader(dataReader, this);
            _inProgressDataReaders[reader] = dbTiming;
        }
Пример #11
0
 void IDbProfiler.ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func <IDataReader> execute, TagCollection tags)
 {
     ExecuteDbCommand(executeType, command, execute, tags);
 }
Пример #12
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="IDbCommand"/>.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="IDbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return the <see cref="IDataReader"/> instance if it is an ExecuteReader operation.
        ///     If it is not ExecuteReader, it should return null.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public virtual IDataReader ExecuteDbCommand(DbExecuteType executeType, DbCommand command, Func <object> execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (command == null)
            {
                execute();
                return(null);
            }
            object   result   = "";
            DbTiming dbTiming = null;

            var data = execute();

            var dataReader = data as IDataReader;

            if (dataReader == null)
            {
                if (data != null)
                {
                    if (executeType == DbExecuteType.NonQuery)
                    {
                        result = Convert.ToInt32(data);
                    }
                    else if (executeType == DbExecuteType.Scalar)
                    {
                        result = (data);
                    }
                }


                dbTiming = new DbTiming(_profiler, executeType, command, result)
                {
                    Tags = tags
                };
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return(null);
            }

            //int readerCount = 0;
            //while (dataReader.Read())
            //{
            //    readerCount++;
            //}

            //result = dataReader;

            dbTiming = new DbTiming(_profiler, executeType, command, result)
            {
                Tags = tags
            };
            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                         new ProfiledDbDataReader(dataReader, this);

            //var reader = new ProfiledDbDataReader(dataReader, this);
            _inProgressDataReaders[reader] = dbTiming;
            //_inProgressDataReaders[reader.Id] = dbTiming;

            return(reader);
        }
Пример #13
0
        /// <summary>
        /// Executes &amp; profiles the execution of the specified <see cref="DbCommand"/> asynchronously.
        /// </summary>
        /// <param name="executeType">The <see cref="DbExecuteType"/>.</param>
        /// <param name="command">The <see cref="DbCommand"/> to be executed &amp; profiled.</param>
        /// <param name="execute">
        ///     The execute handler,
        ///     which should return a scalar value.
        /// </param>
        /// <param name="tags">The tags of the <see cref="DbTiming"/> which will be created internally.</param>
        public async Task <object> ExecuteDbCommandAsync(DbExecuteType executeType, DbCommand command, Func <Task <object> > execute, TagCollection tags)
        {
            if (execute == null)
            {
                return(null);
            }

            if (command == null)
            {
                var o = (await execute());
                return(null);
            }
            object   result   = "";
            DbTiming dbTiming = null;

            var data = (await execute());

            var dataReader = data as IDataReader;

            if (dataReader == null)
            {
                if (data != null)
                {
                    if (executeType == DbExecuteType.NonQuery)
                    {
                        result = Convert.ToInt32(data);
                    }
                    else if (executeType == DbExecuteType.Scalar)
                    {
                        result = (data);
                    }
                }


                dbTiming = new DbTiming(_profiler, executeType, command, result)
                {
                    Tags = tags
                };
                // if not executing reader, stop the sql timing right after execute()
                dbTiming.Stop();
                return(null);
            }

            //int readerCount = 0;
            //while (dataReader.Read())
            //{
            //    readerCount++;
            //}

            //result = dataReader;

            dbTiming = new DbTiming(_profiler, executeType, command, result)
            {
                Tags = tags
            };
            dbTiming.FirstFetch();
            var reader = dataReader as ProfiledDbDataReader ??
                         new ProfiledDbDataReader(dataReader, this);

            //var reader = new ProfiledDbDataReader(dataReader, this);
            _inProgressDataReaders[reader] = dbTiming;
            //_inProgressDataReaders[reader.Id] = dbTiming;

            return(reader);

            //if (execute == null)
            //{
            //    return null;
            //}

            //if (command == null)
            //{
            //    return await execute();
            //}

            //var dbTiming = new DbTiming(_profiler, executeType, command) { Tags = tags };

            //if (executeType == DbExecuteType.Reader)
            //{
            //    // for ExecuteReader
            //    var dataReader = (await execute()) as DbDataReader;
            //    if (dataReader == null)
            //    {
            //        // if not executing reader, stop the sql timing right after execute()
            //        dbTiming.Stop();
            //        return null;
            //    }

            //    dbTiming.FirstFetch();
            //    var reader = dataReader as ProfiledDbDataReader ??
            //        new ProfiledDbDataReader(dataReader, this);
            //    _inProgressDataReaders[reader] = dbTiming;

            //    return reader;
            //}

            //// for ExecuteNonQuery and ExecuteScalar
            //try
            //{
            //    return await execute();
            //}
            //finally
            //{
            //    dbTiming.Stop();
            //}
        }
Пример #14
0
 IDataReader IDbProfiler.ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func <object> execute, TagCollection tags)
 {
     return(ExecuteDbCommand(executeType, command, execute, tags));
 }