コード例 #1
0
ファイル: DbProfiler.cs プロジェクト: lemon18/nanoprofiler
        /// <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
ファイル: DbProfiler.cs プロジェクト: geffzhang/CoreProfiler
        /// <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
ファイル: DbProfiler.cs プロジェクト: lemon18/nanoprofiler
        /// <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
ファイル: DbProfiler.cs プロジェクト: lemon18/nanoprofiler
        /// <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
ファイル: DbTiming.cs プロジェクト: wy182000/CoreProfiler
        /// <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
ファイル: DbTiming.cs プロジェクト: volkd/nanoprofiler
 /// <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
ファイル: DbProfiler.cs プロジェクト: wy182000/CoreProfiler
        /// <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
ファイル: DbProfiler.cs プロジェクト: lemon18/nanoprofiler
        /// <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
ファイル: DbProfiler.cs プロジェクト: volkd/nanoprofiler
 void IDbProfiler.ExecuteDbCommand(DbExecuteType executeType, IDbCommand command, Func<IDataReader> execute, IEnumerable<string> tags)
 {
     ExecuteDbCommand(executeType, command, execute, tags);
 }
コード例 #10
0
ファイル: DbProfiler.cs プロジェクト: volkd/nanoprofiler
        /// <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));
 }