Skip to content

Commit

Permalink
MiniProfiler.Minimal: Hello World (#636)
Browse files Browse the repository at this point in the history
This adds a `MiniProfiler.Minimal` build with only the bare bones needed - zero dependencies for services that need a bare package to do the basics.
  • Loading branch information
NickCraver authored Feb 9, 2023
1 parent 9f222b0 commit 9113616
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 4 deletions.
7 changes: 7 additions & 0 deletions MiniProfiler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples.AspNet", "samples\Samples.AspNet\Samples.AspNet.csproj", "{9C7822CD-BE8E-424F-A0C6-86CC986ABB42}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniProfiler.Minimal", "src\MiniProfiler.Minimal\MiniProfiler.Minimal.csproj", "{A1DCACC2-0D9A-401E-B6A4-856E5DD00541}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -204,6 +206,10 @@ Global
{9C7822CD-BE8E-424F-A0C6-86CC986ABB42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9C7822CD-BE8E-424F-A0C6-86CC986ABB42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9C7822CD-BE8E-424F-A0C6-86CC986ABB42}.Release|Any CPU.Build.0 = Release|Any CPU
{A1DCACC2-0D9A-401E-B6A4-856E5DD00541}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1DCACC2-0D9A-401E-B6A4-856E5DD00541}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1DCACC2-0D9A-401E-B6A4-856E5DD00541}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1DCACC2-0D9A-401E-B6A4-856E5DD00541}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -236,6 +242,7 @@ Global
{BD7A4E74-ECFE-4BBE-86D9-D6DC2BED86DB} = {6A510DBF-E85F-4D2C-B8F7-006DA31B3418}
{A14EA029-60E2-485A-B757-3626511CF2C4} = {5C8ED0C0-7DBB-4660-836E-A760E25160A0}
{9C7822CD-BE8E-424F-A0C6-86CC986ABB42} = {E0DA4035-4D64-4BB8-8EA1-42197DE62519}
{A1DCACC2-0D9A-401E-B6A4-856E5DD00541} = {6A510DBF-E85F-4D2C-B8F7-006DA31B3418}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
LessCompiler = 6a2b5b70-1c32-482f-b5c6-0597e2d4e376
Expand Down
25 changes: 25 additions & 0 deletions src/MiniProfiler.Minimal/MiniProfiler.Minimal.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AssemblyName>MiniProfiler.Minimal</AssemblyName>
<Title>MiniProfiler.Minimal</Title>
<Authors>Nick Craver</Authors>
<Description>MiniProfiler's no-frills headless build for no-UI applications.</Description>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<DefineConstants>MINIMAL</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Compile Include="../MiniProfiler.Shared/**/*.cs" Exclude="../MiniProfiler.Shared/obj/**/*.cs" />

<Compile Remove="../MiniProfiler.Shared/ClientTiming*.cs" />
<Compile Remove="../MiniProfiler.Shared/ColorScheme.cs" />
<Compile Remove="../MiniProfiler.Shared/DiagnosticInitializer.cs" />
<Compile Remove="../MiniProfiler.Shared/IMiniProfilerBuilder.cs" />
<Compile Remove="../MiniProfiler.Shared/Render*.cs" />

<Compile Remove="../MiniProfiler.Shared/Internal/Render.cs" />
<Compile Remove="../MiniProfiler.Shared/Internal/ResultRequest.cs" />
<Compile Remove="../MiniProfiler.Shared/SqlFormatters/**.cs" />
<Compile Remove="../MiniProfiler.Shared/Storage/DatabaseStorageBase.cs" />
</ItemGroup>
</Project>
15 changes: 11 additions & 4 deletions src/MiniProfiler.Shared/Data/ProfiledDbCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Threading.Tasks;
#if !MINIMAL
using System.Reflection;
using System.Reflection.Emit;
#endif

namespace StackExchange.Profiling.Data
{
Expand All @@ -15,19 +17,21 @@ namespace StackExchange.Profiling.Data
[System.ComponentModel.DesignerCategory("")]
public partial class ProfiledDbCommand : DbCommand
{
private static Link<Type, Action<IDbCommand, bool>> bindByNameCache;
private DbCommand _command;
private DbConnection _connection;
private DbTransaction _transaction;
private IDbProfiler _profiler;
private bool _bindByName;

/// <summary>
/// Whether to always wrap data readers, even if there isn't an active profiler on this connect.
/// This allows depending on overrides for things inheriting from <see cref="ProfiledDbDataReader"/> to actually execute.
/// </summary>
protected virtual bool AlwaysWrapReaders => false;

#if !MINIMAL
private static Link<Type, Action<IDbCommand, bool>> bindByNameCache;
private bool _bindByName;

/// <summary>
/// Gets or sets a value indicating whether or not to bind by name.
/// If the underlying command supports BindByName, this sets/clears the underlying
Expand All @@ -49,6 +53,7 @@ public bool BindByName
}
}
}
#endif

/// <summary>
/// Initializes a new instance of the <see cref="ProfiledDbCommand"/> class.
Expand All @@ -73,6 +78,7 @@ public ProfiledDbCommand(DbCommand command, DbConnection connection, IDbProfiler
}
}

#if !MINIMAL
/// <summary>
/// Get the binding name.
/// </summary>
Expand Down Expand Up @@ -108,6 +114,7 @@ private static Action<IDbCommand, bool> GetBindByName(Type commandType)
Link<Type, Action<IDbCommand, bool>>.TryAdd(ref bindByNameCache, commandType, ref action);
return action;
}
#endif

/// <summary>
/// Gets or sets the text command to run against the data source.
Expand Down
5 changes: 5 additions & 0 deletions src/MiniProfiler.Shared/Internal/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
using System.Text;
using System;
using System.Collections.Generic;
#if !MINIMAL
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
#endif

namespace StackExchange.Profiling.Internal
{
Expand Down Expand Up @@ -76,6 +78,7 @@ public static string ToJson(this List<Guid> guids)
return sb.ToString();
}

#if !MINIMAL
private static readonly JsonSerializerSettings defaultSettings = new()
{
NullValueHandling = NullValueHandling.Ignore,
Expand Down Expand Up @@ -122,6 +125,8 @@ public static string ToJson(this object o) =>
public static T FromJson<T>(this string s) where T : class =>
!string.IsNullOrEmpty(s) ? JsonConvert.DeserializeObject<T>(s, defaultSettings) : null;

#endif

/// <summary>
/// <see cref="Dictionary{TKey, TValue}"/> equivalent of ConcurrentDictionary's .TryRemove();
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/MiniProfiler.Shared/Internal/MiniProfilerBaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ public class MiniProfilerBaseOptions
/// </summary>
public decimal TrivialDurationThresholdMilliseconds { get; set; } = 2.0M;

#if !MINIMAL
/// <summary>
/// Dictates if the "time with children" column is displayed by default, defaults to false.
/// For a per-page override you can use .RenderIncludes(showTimeWithChildren: true/false)
Expand Down Expand Up @@ -164,6 +165,7 @@ public class MiniProfilerBaseOptions
nameof(ProfiledDbConnection.Close),
"CloseAsync" // RelationalDiagnosticListener
};
#endif

/// <summary>
/// By default, <see cref="CustomTiming"/>s created by this assembly will grab a stack trace to help
Expand Down
6 changes: 6 additions & 0 deletions src/MiniProfiler.Shared/MiniProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public MiniProfiler(string name, MiniProfilerBaseOptions options)
[DataMember(Order = 5)]
public string MachineName { get; set; }

#if !MINIMAL
/// <summary>
/// Keys are names, values are URLs, allowing additional links to be added to a profiler result, e.g. perhaps a deeper
/// diagnostic page for the current request.
Expand All @@ -113,6 +114,7 @@ public string CustomLinksJson
}
}
}
#endif

private Timing _root;

Expand All @@ -137,6 +139,7 @@ public Timing Root
/// </summary>
public Guid? RootTimingId { get; set; }

#if !MINIMAL
/// <summary>
/// Gets or sets timings collected from the client
/// </summary>
Expand All @@ -147,6 +150,7 @@ public Timing Root
/// RedirectCount in ClientTimings. Used for sql storage.
/// </summary>
public int? ClientTimingsRedirectCount { get; set; }
#endif

/// <summary>
/// Gets or sets a string identifying the user/client that is profiling this request.
Expand Down Expand Up @@ -283,11 +287,13 @@ private bool InnerStop()
return true;
}

#if !MINIMAL
/// <summary>
/// Deserializes the JSON string parameter to a <see cref="MiniProfiler"/>.
/// </summary>
/// <param name="json">The string to deserialize into a <see cref="MiniProfiler"/>.</param>
public static MiniProfiler FromJson(string json) => json.FromJson<MiniProfiler>();
#endif

/// <summary>
/// Returns the <see cref="Root"/>'s <see cref="Timing.Name"/> and <see cref="DurationMilliseconds"/> this profiler recorded.
Expand Down
2 changes: 2 additions & 0 deletions src/MiniProfiler.Shared/MiniProfilerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ public static void AddProfilerResults(this MiniProfiler profiler, MiniProfiler e
profiler.Head.AddChild(externalProfiler.Root);
}

#if !MINIMAL
/// <summary>
/// Adds the <paramref name="text"/> and <paramref name="url"/> pair to <paramref name="profiler"/>'s
/// <see cref="MiniProfiler.CustomLinks"/> dictionary; will be displayed on the client in the bottom of the profiler popup.
Expand Down Expand Up @@ -213,5 +214,6 @@ public static string RenderPlainText(this MiniProfiler profiler, bool htmlEncode

return text.ToStringRecycle();
}
#endif
}
}
2 changes: 2 additions & 0 deletions src/MiniProfiler.Shared/Timing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ public List<Timing> Children
[DataMember(Order = 7)]
public TimingDebugInfo DebugInfo { get; set; }

#if !MINIMAL
/// <summary>
/// JSON representing the Custom Timings associated with this timing.
/// </summary>
public string CustomTimingsJson {
get => CustomTimings?.ToJson();
set => CustomTimings = value.FromJson<Dictionary<string, List<CustomTiming>>>();
}
#endif

/// <summary>
/// Returns true when there exists any <see cref="CustomTiming"/> objects in this <see cref="CustomTimings"/>.
Expand Down

0 comments on commit 9113616

Please sign in to comment.