/// <summary> /// Sets <paramref name="profiler"/> to be active (read to start profiling) /// This should be called once a new MiniProfiler has been created. /// </summary> /// <param name="profiler">The profiler to set to active</param> /// <exception cref="ArgumentNullException">If <paramref name="profiler"/> is null</exception> protected static void SetProfilerActive(MiniProfiler profiler) { if (profiler == null) throw new ArgumentNullException("profiler"); profiler.IsActive = true; }
/// <summary> /// Stops the profiler and marks it as inactive. /// </summary> /// <param name="profiler">The profiler to stop</param> /// <returns>True if successful, false if Stop had previously been called on this profiler</returns> /// <exception cref="ArgumentNullException">If <paramref name="profiler"/> is null</exception> protected static bool StopProfiler(MiniProfiler profiler) { if (profiler == null) throw new ArgumentNullException("profiler"); if (!profiler.StopImpl()) return false; profiler.IsActive = false; return true; }
/// <summary> /// Creates a new Timing named 'name' in the 'profiler's session, with 'parent' as this Timing's immediate ancestor. /// </summary> public Timing(MiniProfiler profiler, Timing parent, string name) { this.Id = Guid.NewGuid(); Profiler = profiler; Profiler.Head = this; if (parent != null) // root will have no parent { parent.AddChild(this); } Name = name; _startTicks = profiler.ElapsedTicks; StartMilliseconds = profiler.GetRoundedMilliseconds(_startTicks); }
/// <summary> /// Creates a new SqlTiming to profile 'command'. /// </summary> public SqlTiming(DbCommand command, ExecuteType type, MiniProfiler profiler) { Id = Guid.NewGuid(); RawCommandString = CommandString = AddSpacesToParameters(command.CommandText); Parameters = GetCommandParameters(command); ExecuteType = type; if (!MiniProfiler.Settings.ExcludeStackTraceSnippetFromSqlTimings) StackTraceSnippet = Helpers.StackTraceSnippet.Get(); _profiler = profiler; _profiler.AddSqlTiming(this); _startTicks = _profiler.ElapsedTicks; StartMilliseconds = MiniProfiler.GetRoundedMilliseconds(_startTicks); }
/// <summary> /// Returns an open connection that will have its queries profiled. /// </summary> public static DbConnection GetConnection(MiniProfiler profiler = null) { using (profiler.Step("GetOpenConnection")) { DbConnection cnn = new System.Data.SQLite.SQLiteConnection(WcfCommon.ConnectionString); // to get profiling times, we have to wrap whatever connection we're using in a ProfiledDbConnection // when MiniProfiler.Current is null, this connection will not record any database timings if (MiniProfiler.Current != null) { cnn = new MvcMiniProfiler.Data.ProfiledDbConnection(cnn, MiniProfiler.Current); } cnn.Open(); return cnn; } }
private void UpsertRouteHit(ActionDescriptor actionDesc, MiniProfiler profiler) { var routeName = actionDesc.ControllerDescriptor.ControllerName + "/" + actionDesc.ActionName; using (var conn = GetConnection(profiler)) { var param = new { routeName = routeName }; using (profiler.Step("Insert RouteHits")) { conn.Execute("insert or ignore into RouteHits (RouteName, HitCount) values (@routeName, 0)", param); } using (profiler.Step("Update RouteHits")) { // let's put some whitespace in this query to demonstrate formatting conn.Execute( @"update RouteHits set HitCount = HitCount + 1 where RouteName = @routeName", param); } } }
public ProfiledSqlDbConnection(SqlConnection connection, MiniProfiler profiler) : base(connection, profiler) { Connection = connection; }
/// <summary> /// Returns a new SqlProfiler to be used in the 'profiler' session. /// </summary> public SqlProfiler(MiniProfiler profiler) { Profiler = profiler; }
/// <summary> /// Calls <see cref="MiniProfiler.Settings.EnsureStorageStrategy"/> to save the current /// profiler using the current storage settings /// </summary> /// <param name="current"></param> protected static void SaveProfiler(MiniProfiler current) { // because we fetch profiler results after the page loads, we have to put them somewhere in the meantime MvcMiniProfiler.MiniProfiler.Settings.EnsureStorageStrategy(); MvcMiniProfiler.MiniProfiler.Settings.Storage.Save(current); }
/// <summary> /// Calls <see cref="MiniProfiler.Settings.EnsureStorageStrategy"/> to save the current /// profiler using the current storage settings /// </summary> /// <param name="current"></param> protected static void SaveProfiler(MiniProfiler current) { // because we fetch profiler results after the page loads, we have to put them somewhere in the meantime MvcMiniProfiler.MiniProfiler.Settings.EnsureStorageStrategy(); MvcMiniProfiler.MiniProfiler.Settings.Storage.Save(current); }
/// <summary> /// Makes sure 'profiler' has a Name, pulling it from route data or url. /// </summary> private static void EnsureName(MiniProfiler profiler, HttpRequest request) { // also set the profiler name to Controller/Action or /url if (profiler.Name.IsNullOrWhiteSpace()) { //var rc = request.RequestContext; //RouteValueDictionary values; //if (rc != null && rc.RouteData != null && (values = rc.RouteData.Values).Count > 0) //{ // var controller = values["Controller"]; // var action = values["Action"]; // if (controller != null && action != null) // profiler.Name = controller.ToString() + "/" + action.ToString(); //} if (profiler.Name.IsNullOrWhiteSpace()) { profiler.Name = request.Path ?? ""; if (profiler.Name.Length > 70) profiler.Name = profiler.Name.Remove(70); } } }
/// <summary> /// Ends the current profiling session, if one exists. /// </summary> /// <param name="discardResults"> /// When true, clears the <see cref="MiniProfiler.Current"/> for this HttpContext, allowing profiling to /// be prematurely stopped and discarded. Useful for when a specific route does not need to be profiled. /// </param> public override void Stop(bool discardResults) { var context = HttpContext.Current; if (context == null) return; var current = Current; if (current == null) return; // stop our timings - when this is false, we've already called .Stop before on this session if (!StopProfiler(current)) return; if (discardResults) { Current = null; return; } var request = context.Request; var response = context.Response; // set the profiler name to Controller/Action or /url EnsureName(current, request); // save the profiler SaveProfiler(current); try { var arrayOfIds = MvcMiniProfiler.MiniProfiler.Settings.Storage.GetUnviewedIds(current.User).ToJson(); // allow profiling of ajax requests response.AppendHeader("X-MiniProfiler-Ids", arrayOfIds); } catch { } // headers blew up }
private void RecursiveMethod(ref int i, DbConnection conn, MiniProfiler profiler) { Thread.Sleep(5); // ensure we show up in the profiler if (i >= 10) return; using (profiler.Step("Nested call " + i)) { // run some meaningless queries to illustrate formatting conn.Query( @"select * from MiniProfilers where Name like @name or Name = @name or DurationMilliseconds >= @duration or HasSqlTimings = @hasSqlTimings or Started > @yesterday ", new { name = "Home/Index", duration = 100.5, hasSqlTimings = true, yesterday = DateTime.UtcNow.AddDays(-1) }); conn.Query(@"select RouteName, HitCount from RouteHits where HitCount < 100000000 or HitCount > 0 order by HitCount, RouteName -- this should hopefully wrap"); // massive query to test if max-height is properly removed from <pre> stylings conn.Query( @"select * from (select RouteName, HitCount from RouteHits where HitCount between 0 and 9 union all select RouteName, HitCount from RouteHits where HitCount between 10 and 19 union all select RouteName, HitCount from RouteHits where HitCount between 20 and 29 union all select RouteName, HitCount from RouteHits where HitCount between 30 and 39 union all select RouteName, HitCount from RouteHits where HitCount between 40 and 49 union all select RouteName, HitCount from RouteHits where HitCount between 50 and 59 union all select RouteName, HitCount from RouteHits where HitCount between 60 and 69 union all select RouteName, HitCount from RouteHits where HitCount between 70 and 79 union all select RouteName, HitCount from RouteHits where HitCount between 80 and 89 union all select RouteName, HitCount from RouteHits where HitCount between 90 and 99 union all select RouteName, HitCount from RouteHits where HitCount > 100) order by RouteName"); using (profiler.Step("Incrementing a reference parameter named i")) // need a long title to test max-width { i++; } RecursiveMethod(ref i, conn, profiler); } }
public ProfiledSqlDbCommand(SqlCommand cmd, SqlConnection conn, MiniProfiler profiler) : base(cmd, conn, profiler) { Command = cmd; }
/// <summary> /// Returns a new SqlProfiler to be used in the 'profiler' session. /// </summary> public SqlProfiler(MiniProfiler profiler) { Profiler = profiler; }
/// <summary> /// Starts a new MiniProfiler and associates it with the current <see cref="HttpContext.Current"/>. /// </summary> public override MiniProfiler Start(ProfileLevel level) { var context = HttpContext.Current; if (context == null) return null; var url = context.Request.Url; var path = context.Request.AppRelativeCurrentExecutionFilePath.Substring(1); // don't profile /content or /scripts, either - happens in web.dev foreach (var ignored in MvcMiniProfiler.MiniProfiler.Settings.IgnoredPaths ?? new string[0]) { if (path.ToUpperInvariant().Contains((ignored ?? "").ToUpperInvariant())) return null; } var result = new MiniProfiler(url.OriginalString, level); Current = result; SetProfilerActive(result); // don't really want to pass in the context to MiniProfler's constructor or access it statically in there, either result.User = (Settings.UserProvider ?? new IpAddressIdentity()).GetUser(context.Request); return result; }
/// <summary> /// Returns an <see cref="IDisposable"/> that will time the code between its creation and disposal. /// </summary> /// <param name="profiler">The current profiling session or null.</param> /// <param name="name">A descriptive name for the code that is encapsulated by the resulting IDisposable's lifetime.</param> /// <param name="level">This step's visibility level; allows filtering when <see cref="MiniProfiler.Start"/> is called.</param> public static IDisposable Step(this MiniProfiler profiler, string name, ProfileLevel level = ProfileLevel.Info) { return(profiler == null ? null : profiler.StepImpl(name, level)); }