Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Added UseFirstLevelCache plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Sep 6, 2015
1 parent 939dfd1 commit 0fdfe68
Show file tree
Hide file tree
Showing 42 changed files with 1,216 additions and 38 deletions.
4 changes: 4 additions & 0 deletions DNTProfiler.Common/Models/ColumnInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
public class ColumnInfo
{
public string ColumnName { set; get; }

public string DataType { set; get; }

public int Ordinal { set; get; }

public bool IsKey { set; get; }
}
}
12 changes: 11 additions & 1 deletion DNTProfiler.Common/Profiler/DbCommandContext.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
using System.Data;
using System.Collections.Generic;
using System.Data;

namespace DNTProfiler.Common.Profiler
{
public class DbCommandContext : DbContextBase
{
public DataTable DataTable { set; get; }

public long? ElapsedMilliseconds { set; get; }

public object Result { set; get; }

public ISet<string> Keys { set; get; }

public DbCommandContext()
{
Keys = new HashSet<string>();
}
}
}
4 changes: 2 additions & 2 deletions DNTProfiler.Common/Profiler/DbProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ public void LogResult(DbCommand command, DbCommandContext interceptionCommandCon
{
ColumnName = column.ColumnName,
DataType = column.DataType.ToString(),
Ordinal = column.Ordinal
Ordinal = column.Ordinal,
IsKey = interceptionCommandContext.Keys != null && interceptionCommandContext.Keys.Contains(column.ColumnName)
});
}
}

_baseInfoQueue.Enqueue(commandResult);
}


public void LogTransaction(DbTransaction transaction,
DbTransactionContext interceptionTransactionContext,
CommandTransactionType type)
Expand Down
41 changes: 39 additions & 2 deletions DNTProfiler.Core/EFProfilerContextProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Data;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
Expand All @@ -12,6 +15,8 @@ namespace DNTProfiler.EntityFramework.Core
{
public static class EFProfilerContextProvider
{
private static readonly ConcurrentDictionary<int, HashSet<string>> _keys = new ConcurrentDictionary<int, HashSet<string>>();

public static DbCommandContext GetLoggedDbCommand<TResult>(DbCommand command, DbCommandInterceptionContext<TResult> interceptionContext)
{
var context = new DbCommandContext
Expand Down Expand Up @@ -82,12 +87,44 @@ public static DbCommandContext GetLoggedResult<TResult>(DbCommand command, DbCom
Exception = interceptionContext.OriginalException ?? interceptionContext.Exception,
Result = interceptionContext.Result,
ElapsedMilliseconds = elapsedMilliseconds,
DataTable = dataTable
DataTable = dataTable,
Keys = getKeys(interceptionContext)
};
setBaseInfo(interceptionContext, context);
return context;
}

private static ISet<string> getKeys(DbInterceptionContext interceptionContext)
{
if (interceptionContext.ObjectContexts == null || !interceptionContext.ObjectContexts.Any())
{
return new HashSet<string>();
}

var objectContext = interceptionContext.ObjectContexts.First();
var objectContextId = UniqueIdExtensions<ObjectContext>.GetUniqueId(objectContext).ToInt();

HashSet<string> keys;
if (_keys.TryGetValue(objectContextId, out keys))
{
return keys;
}

var results = new HashSet<string>();
var entityProps = objectContext.MetadataWorkspace.GetItems<EntityType>(DataSpace.SSpace);
foreach (var entityType in entityProps)
{
foreach (var edmProperty in entityType.KeyProperties)
{
results.Add(edmProperty.Name);
}
}

_keys.TryAdd(objectContextId, results);

return results;
}

private static void setBaseInfo(DbInterceptionContext interceptionContext, DbContextBase info)
{
if (interceptionContext.ObjectContexts == null || !interceptionContext.ObjectContexts.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<Compile Include="ScriptDomVisitors\SqlNormalizerVisitor.cs" />
<Compile Include="ScriptDomVisitors\UnboundedResultSetVisitor.cs" />
<Compile Include="ScriptDomVisitors\UnparametrizedWhereClausesVisitor.cs" />
<Compile Include="ScriptDomVisitors\UseFirstLevelCacheVisitor.cs" />
<Compile Include="StackTraces\MethodDeclarations.cs" />
<Compile Include="StackTraces\OpenStackTraceFile.cs" />
<Compile Include="ViewModels\ChartsViewModelBase.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private string resolveMultiPartIdentifier(MultiPartIdentifier identifier)
_aliases.ContainsKey(identifier.Identifiers[0].Value))
{
return
_aliases[identifier.Identifiers[0].Value] + "." + identifier.Identifiers[1].Value;
string.Format("{0}.{1}", _aliases[identifier.Identifiers[0].Value], identifier.Identifiers[1].Value);
}
return identifier.AsObjectName();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using Microsoft.SqlServer.TransactSql.ScriptDom;
using System.Collections.Generic;

namespace DNTProfiler.Infrastructure.ScriptDomVisitors
{
public class UseFirstLevelCacheVisitor : SqlFragmentVisitorBase
{
private int _comparisonCount;
private bool _hasTopRowFilter;

public ISet<string> Keys { set; get; }

public override void ExplicitVisit(BooleanComparisonExpression node)
{
if (!_hasTopRowFilter)
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}

_comparisonCount++;
if (_comparisonCount > 1)
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}

if (node.ComparisonType != BooleanComparisonType.Equals)
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}

var secondExpression = node.SecondExpression.FirstNoneParenthesisExpression();
var firstExpression = node.FirstExpression.FirstNoneParenthesisExpression();

if (isCastOrFunctionCall(firstExpression) || isCastOrFunctionCall(secondExpression))
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}

var firstColumnReferenceExpression = firstExpression as ColumnReferenceExpression;
if (firstColumnReferenceExpression != null)
{
var firstColumnResolved = resolveMultiPartIdentifier(firstColumnReferenceExpression.MultiPartIdentifier);
if (Keys != null && Keys.Contains(firstColumnResolved))
{
IsSuspected = true;
_comparisonCount--;
base.ExplicitVisit(node);
return;
}

if (IsSuspected)
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}
}

var secondColumnReferenceExpression = secondExpression as ColumnReferenceExpression;
if (secondColumnReferenceExpression != null)
{
var secondColumnResolved = resolveMultiPartIdentifier(secondColumnReferenceExpression.MultiPartIdentifier);
if (Keys != null && Keys.Contains(secondColumnResolved))
{
IsSuspected = true;
_comparisonCount--;
base.ExplicitVisit(node);
return;
}

if (IsSuspected)
{
IsSuspected = false;
base.ExplicitVisit(node);
return;
}
}

base.ExplicitVisit(node);
}

public override void ExplicitVisit(TopRowFilter node)
{
_hasTopRowFilter = true;
base.ExplicitVisit(node);
}

private static bool isCastOrFunctionCall(ScalarExpression expression)
{
return expression is CastCall || expression is FunctionCall;
}

private static string resolveMultiPartIdentifier(MultiPartIdentifier identifier)
{
return identifier.Identifiers.Count == 2 ? identifier.Identifiers[1].Value : identifier.AsObjectName();
}
}
}
4 changes: 2 additions & 2 deletions DNTProfiler.MetaData/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#endif

// Assembly Versions are incremented manually when branching the code for a release.
[assembly: AssemblyVersion("1.6.930.0")]
[assembly: AssemblyVersion("1.7.978.0")]
// Assembly File Version should be incremented automatically as part of the build process.
[assembly: AssemblyFileVersion("1.6.930.0")]
[assembly: AssemblyFileVersion("1.7.978.0")]

2 changes: 1 addition & 1 deletion DNTProfiler.MetaData/SharedAssemblyInfo.tt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ using System.Runtime.InteropServices;
// Manually incremented for major releases, such as adding many new features to the solution or introducing breaking changes.
int MajorVersion = 1;
// Manually incremented for minor releases, such as introducing small changes to existing features or adding new features.
int MinorVersion = 6;
int MinorVersion = 7;
// Typically incremented automatically as part of every build performed on the Build Server.
int BuildNumber = (int)(DateTime.UtcNow - new DateTime(2013,1,1)).TotalDays;
// Incremented for QFEs (a.k.a. “hotfixes” or patches) to builds released into the Production environment.
Expand Down
11 changes: 11 additions & 0 deletions DNTProfiler.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DNTProfiler", "DNTProfiler\
{2481A200-5DAA-4EC2-9133-D110AD0DE78F} = {2481A200-5DAA-4EC2-9133-D110AD0DE78F}
{345D9D04-1D19-43B8-BEC9-2EBDE25C3E5B} = {345D9D04-1D19-43B8-BEC9-2EBDE25C3E5B}
{F387CD06-60FB-4596-84EE-F722E3510284} = {F387CD06-60FB-4596-84EE-F722E3510284}
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640} = {8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}
{1E7D6B0C-1740-410C-A2D7-19FE249CF24F} = {1E7D6B0C-1740-410C-A2D7-19FE249CF24F}
{4C5A830F-93B5-4E6C-83D0-C86EE3DD9E5C} = {4C5A830F-93B5-4E6C-83D0-C86EE3DD9E5C}
{ADA0E719-984D-4875-8029-F847E69E9C08} = {ADA0E719-984D-4875-8029-F847E69E9C08}
Expand Down Expand Up @@ -344,6 +345,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DNTProfiler.LongConnections
{25641353-AEA2-403D-993E-B0EDDC73D7A4} = {25641353-AEA2-403D-993E-B0EDDC73D7A4}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DNTProfiler.UseFirstLevelCache", "Plugins\DNTProfiler.UseFirstLevelCache\DNTProfiler.UseFirstLevelCache.csproj", "{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}"
ProjectSection(ProjectDependencies) = postProject
{25641353-AEA2-403D-993E-B0EDDC73D7A4} = {25641353-AEA2-403D-993E-B0EDDC73D7A4}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -570,6 +576,10 @@ Global
{F60056D6-874A-4D38-9F59-D9C742C9407A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F60056D6-874A-4D38-9F59-D9C742C9407A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F60056D6-874A-4D38-9F59-D9C742C9407A}.Release|Any CPU.Build.0 = Release|Any CPU
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -629,6 +639,7 @@ Global
{29AA79EE-157F-481F-9B01-7C30C53236D0} = {687C659F-E351-4279-8B5F-8C5EE58339A2}
{29DC75AF-D49E-42C9-8C19-CE0E45F22BD0} = {687C659F-E351-4279-8B5F-8C5EE58339A2}
{F60056D6-874A-4D38-9F59-D9C742C9407A} = {687C659F-E351-4279-8B5F-8C5EE58339A2}
{8A4A3C08-CF06-4D36-BC4C-B9598B8F8640} = {687C659F-E351-4279-8B5F-8C5EE58339A2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
VisualSVNWorkingCopyRoot = .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
<Private>True</Private>
</Reference>
<Reference Include="NHibernate, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\NHibernate.4.0.3.4000\lib\net40\NHibernate.dll</HintPath>
<HintPath>..\..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
2 changes: 1 addition & 1 deletion NHibernate/DNTProfiler.NHibernate.Core/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<package id="Expression.Blend.Sdk" version="1.0.2" targetFramework="net40" />
<package id="Iesi.Collections" version="4.0.1.4000" targetFramework="net40" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net40" />
<package id="NHibernate" version="4.0.3.4000" targetFramework="net40" />
<package id="NHibernate" version="4.0.4.4000" targetFramework="net40" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
<HintPath>..\..\packages\Iesi.Collections.4.0.1.4000\lib\net40\Iesi.Collections.dll</HintPath>
</Reference>
<Reference Include="NHibernate, Version=4.0.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\NHibernate.4.0.3.4000\lib\net40\NHibernate.dll</HintPath>
<HintPath>..\..\packages\NHibernate.4.0.4.4000\lib\net40\NHibernate.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NHibernate.Validator">
<HintPath>..\..\packages\NHibernate.Validator.1.3.2.4000\lib\Net35\NHibernate.Validator.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion NHibernate/NHibernateConsoleTests/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Iesi.Collections" version="4.0.1.4000" targetFramework="net40" />
<package id="NHibernate" version="4.0.3.4000" targetFramework="net40" />
<package id="NHibernate" version="4.0.4.4000" targetFramework="net40" />
<package id="NHibernate.Validator" version="1.3.2.4000" targetFramework="net40" />
</packages>
Loading

0 comments on commit 0fdfe68

Please sign in to comment.