Last active
February 10, 2016 14:06
-
-
Save esergueev/575d0782499e218d33b5 to your computer and use it in GitHub Desktop.
DBConnection per request or per query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbConnectionPerformanceTests | |
{ | |
private readonly ITestOutputHelper _outputHelper; | |
public DbConnectionPerformanceTests(ITestOutputHelper outputHelper) | |
{ | |
_outputHelper = outputHelper; | |
} | |
[Theory] | |
[MemberData(nameof(GetSut))] | |
public void Execute(DbConnectionBase sut) | |
{ | |
string[] sqlStatements = | |
{ | |
@"select content from account;", | |
@"select content from patient limit 100 offset 100;", | |
@"select content from observation limit 100 offset 100;" | |
}; | |
using (sut) | |
{ | |
var tester = new PerformanceTester(() => sut.Execute(sqlStatements)); | |
tester.MeasureExecTimeWithMetrics(100); | |
_outputHelper.WriteLine( | |
$"Type-{sut.GetType()}, Min-{tester.MinTime}, Average-{tester.AverageTime}, Max-{tester.MaxTime},"); | |
} | |
} | |
private static IEnumerable<object[]> MakeArgs(params object[] args) | |
{ | |
return args.Select(arg => new[] {arg}); | |
} | |
public static IEnumerable<object[]> GetSut() | |
{ | |
var connectionStringSettings = ConfigurationManager.ConnectionStrings["postgres"]; | |
return MakeArgs( | |
new DbConnectionPerQuery(connectionStringSettings.ConnectionString), | |
new DbConnectionPerRequest(connectionStringSettings.ConnectionString) | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class DbConnectionSutBase : IDisposable | |
{ | |
protected readonly string ConnectionString; | |
protected DbConnectionSutBase(string connectionString) | |
{ | |
ConnectionString = connectionString; | |
} | |
public void Execute(params string[] sqlStatements) | |
{ | |
sqlStatements.ToList().ForEach(statement => WithConnection((conn) => conn.Query(statement))); | |
} | |
protected abstract TResult WithConnection<TResult>(Func<IDbConnection, TResult> func); | |
public virtual void Dispose() | |
{ | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbConnectionSutPerQuery : DbConnectionSutBase | |
{ | |
public DbConnectionSutPerQuery(string connectionString) : base(connectionString) | |
{ | |
} | |
protected override TResult WithConnection<TResult>(Func<IDbConnection, TResult> func) | |
{ | |
using (var conn = new NpgsqlConnection(ConnectionString)) | |
{ | |
conn.Open(); | |
return func.Invoke(conn); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DbConnectionSutPerRequest : DbConnectionSutBase | |
{ | |
private NpgsqlConnection _connection; | |
public DbConnectionSutPerRequest(string connectionString) : base(connectionString) | |
{ | |
} | |
protected override TResult WithConnection<TResult>(Func<IDbConnection, TResult> func) | |
{ | |
if (_connection != null) | |
{ | |
return func.Invoke(_connection); | |
} | |
_connection = new NpgsqlConnection(ConnectionString); | |
_connection.Open(); | |
return func.Invoke(_connection); | |
} | |
public override void Dispose() | |
{ | |
_connection.Dispose(); | |
_connection = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment