Mvc-Mini-Profiler: Why so many X-MiniProfiler-Ids?
I'm using Mvc-Mini-Profiler (what a great product!). Using my ordinary web browser everything seems to work fine but as soon as I use my own http client (basic http 1.1 without cookie support) the amount of X-MiniProfiler-Ids in the http header increases. This happens quite rapidly and becomes quite many in a short amount of time (11kB and开发者_运维技巧 above of data).
Can the lack of cookies make Mvc-Mini-Profiler work this way or could anything be wrong with my implementation?
This is by design I think. Though we could improve the implementation a bit.
The X-MiniProfiler-Ids
need to be "consumed", they only show up when profiling is enabled. The reason it works this way is so you can profile POST
and redirects.
We probably should set some clear upper limit there (like 20 or so) - please post a bug for that.
However, since you never really plan to consume any profiling blocks for you HTTP client, I would recommend abandoning profiling if the useragent is your HTTP client.
You can do this by adding a conditional before:
// don't run if UserAgent is "my http client"
if(notMyUserAgent)
MvcMiniProfiler.MiniProfiler.Start();
Another option is to override the SqlServerStorage class and default the UserHasViewed field to true. This will keep the X-MiniProfiler-Id
string down to a minimum.
public class MvcMiniProfilerStorage : SqlServerStorage
{
public MvcMiniProfilerStorage(string connectionString) : base(connectionString)
{
}
/// <summary>
/// Stores to dbo.MiniProfilers under its ;
/// stores all child Timings and SqlTimings to their respective tables.
/// </summary>
public override void Save(MiniProfiler profiler)
{
const string sql =
@"insert into MiniProfilers
(Id,
Name,
Started,
MachineName,
[User],
Level,
RootTimingId,
DurationMilliseconds,
DurationMillisecondsInSql,
HasSqlTimings,
HasDuplicateSqlTimings,
HasTrivialTimings,
HasAllTrivialTimings,
TrivialDurationThresholdMilliseconds,
HasUserViewed)
select @Id,
@Name,
@Started,
@MachineName,
@User,
@Level,
@RootTimingId,
@DurationMilliseconds,
@DurationMillisecondsInSql,
@HasSqlTimings,
@HasDuplicateSqlTimings,
@HasTrivialTimings,
@HasAllTrivialTimings,
@TrivialDurationThresholdMilliseconds,
@HasUserViewed
where not exists (select 1 from MiniProfilers where Id = @Id)";
// this syntax works on both mssql and sqlite
using (DbConnection conn = GetOpenConnection())
{
int insertCount = conn.Execute(sql,
new
{
profiler.Id,
Name = Truncate(profiler.Name, 200),
profiler.Started,
MachineName = Truncate(profiler.MachineName, 100),
User = Truncate(profiler.User, 100),
profiler.Level,
RootTimingId = profiler.Root.Id,
profiler.DurationMilliseconds,
profiler.DurationMillisecondsInSql,
profiler.HasSqlTimings,
profiler.HasDuplicateSqlTimings,
profiler.HasTrivialTimings,
profiler.HasAllTrivialTimings,
profiler.TrivialDurationThresholdMilliseconds,
// BUG: Too many X-MiniProfiler-Id headers cause
// Firefox to stop all requests
//
// This hack marks all entries as read so that
// they do not end up part of that header.
HasUserViewed = true
});
if (insertCount > 0)
{
SaveTiming(conn, profiler, profiler.Root);
}
}
}
private static string Truncate(string s, int maxLength)
{
return s != null && s.Length >
maxLength ? s.Substring(0, maxLength) : s;
}
}
精彩评论