Permalink
Browse files

Using Benchmarks and pooling sessions

  • Loading branch information...
sebastienros committed May 18, 2017
1 parent 7b4757b commit 705cd3dea623fdc2d5bdf71c9c4dc4f28c24dfb5

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,36 +0,0 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("YesSql.Samples.Performance")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YesSql.Samples.Performance")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0dcc3c69-89a1-407c-9c88-5b7fc778779d")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,25 @@
using YesSql.Indexes;
namespace YesSql.Samples.Performance
{
public class User
{
//public int Id { get; set; }
public string Email { get; set; }
public string Name { get; set; }
}
public class UserByName : MapIndex
{
public string Name { get; set; }
}
public class UserIndexProvider : IndexProvider<User>
{
public override void Describe(DescribeContext<User> context)
{
context.For<UserByName>()
.Map(user => new UserByName { Name = user.Name });
}
}
}
@@ -1,37 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net451;netcoreapp1.0</TargetFrameworks>
<RuntimeIdentifiers>win7-x64;win7-x86;osx.10.10-x64;osx.10.11-x64;ubuntu.14.04-x64;ubuntu.16.04-x64;centos.7-x64;rhel.7.2-x64;debian.8-x64;fedora.23-x64;opensuse.13.2-x64</RuntimeIdentifiers>
<RuntimeIdentifier Condition=" '$(TargetFramework)' == 'net451' ">win7-x86</RuntimeIdentifier>
<TargetFrameworks>net46;netcoreapp1.1</TargetFrameworks>
<DebugType>portable</DebugType>
<PreserveCompilationContext>true</PreserveCompilationContext>
<AssemblyName>YesSql.Samples.Performance</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>YesSql.Samples.Performance</PackageId>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.10.6" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="1.0.1" />
<PackageReference Include="xunit" Version="2.2.0-beta5-build3474" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System.Runtime" />
<Reference Include="System.Reflection" />
<Reference Include="System.Threading.Tasks" />
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\YesSql.Core\YesSql.Core.csproj" />
<ProjectReference Include="..\..\src\YesSql.Provider.SqlServer\YesSql.Provider.SqlServer.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
@@ -10,6 +10,7 @@ public interface IConfiguration
IConnectionFactory ConnectionFactory { get; set; }
IContentSerializer ContentSerializer { get; set; }
string TablePrefix { get; set; }
int SessionPoolSize { get; set; }
}
public static class ConfigurationExtensions
@@ -43,8 +44,14 @@ public static IConfiguration SetContentSerializer(this IConfiguration configurat
configuration.ContentSerializer = contentSerializer;
return configuration;
}
public static IConfiguration SetSessionPoolSize(this IConfiguration configuration, int size)
{
configuration.SessionPoolSize = size;
return configuration;
}
}
public class DbConnectionFactory<TDbConnection> : IConnectionFactory
where TDbConnection : DbConnection, new()
{
@@ -12,12 +12,14 @@ public Configuration()
ContentSerializer = new JsonContentSerializer();
IsolationLevel = IsolationLevel.ReadCommitted;
TablePrefix = "";
SessionPoolSize = 16;
}
public IIdentifierFactory IdentifierFactory { get; set; }
public IsolationLevel IsolationLevel { get; set; }
public IConnectionFactory ConnectionFactory { get; set; }
public IContentSerializer ContentSerializer { get; set; }
public string TablePrefix { get; set; }
public int SessionPoolSize { get; set; }
}
}
View
@@ -26,9 +26,9 @@ public class Session : ISession
private readonly HashSet<object> _deleted = new HashSet<object>();
private readonly Store _store;
private readonly ISqlDialect _dialect;
private readonly IsolationLevel _isolationLevel;
private readonly IDbConnection _connection;
private volatile bool _disposed;
internal IsolationLevel _isolationLevel;
private IDbConnection _connection;
protected bool _cancel;
@@ -353,6 +353,35 @@ public void Dispose()
}
}
}
Release();
}
/// <summary>
/// Called when the instance is available to an object pool.
/// </summary>
internal void Release()
{
_updated.Clear();
_saved.Clear();
_deleted.Clear();
_commands.Clear();
_maps.Clear();
_identityMap.Clear();
_store.ReleaseSession(this);
}
/// <summary>
/// Called when the instance is reused from an object pool and doesn't go
/// through the constructor.
/// </summary>
internal void StartLease(IsolationLevel isolationLevel)
{
_disposed = false;
_cancel = false;
_isolationLevel = isolationLevel;
_connection = _store.Configuration.ConnectionFactory.CreateConnection();
}
public async Task CommitAsync()
@@ -397,6 +426,7 @@ public async Task CommitAsync()
_saved.Clear();
_deleted.Clear();
_commands.Clear();
_maps.Clear();
}
private async Task ReduceAsync()
View
@@ -1,4 +1,5 @@
using Dapper;
using Roslyn.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -18,6 +19,7 @@ public class Store : IStore
{
protected List<IIndexProvider> Indexes;
protected LinearBlockIdGenerator IdGenerator;
private ObjectPool<Session> SessionPool;
public IConfiguration Configuration { get; set; }
@@ -34,7 +36,7 @@ public class Store : IStore
new ConcurrentDictionary<Type, Func<IDescriptor>>();
public const string DocumentTable = "Document";
static Store()
{
SqlMapper.ResetTypeHandlers();
@@ -71,6 +73,8 @@ public void AfterConfigurationAssigned()
Indexes = new List<IIndexProvider>();
ValidateConfiguration();
IdGenerator = new LinearBlockIdGenerator(Configuration.ConnectionFactory, 20, Configuration.TablePrefix);
SessionPool = new ObjectPool<Session>(MakeSession, Configuration.SessionPoolSize);
}
public Task InitializeAsync()
@@ -140,12 +144,24 @@ private void ValidateConfiguration()
public ISession CreateSession()
{
return new Session(this, Configuration.IsolationLevel);
return CreateSession(Configuration.IsolationLevel);
}
public ISession CreateSession(IsolationLevel isolationLevel)
{
return new Session(this, isolationLevel);
var session = SessionPool.Allocate();
session.StartLease(isolationLevel);
return session;
}
private Session MakeSession()
{
return new Session(this, Configuration.IsolationLevel);
}
internal void ReleaseSession(Session session)
{
SessionPool.Free(session);
}
public void Dispose()
Oops, something went wrong.

0 comments on commit 705cd3d

Please sign in to comment.