Skip to content

Commit

Permalink
manual Npgsql test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Nov 20, 2023
1 parent b11d22b commit 18bebf8
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 2 deletions.
164 changes: 162 additions & 2 deletions test/UsageBenchmark/CommandRewriteBenchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
using BenchmarkDotNet.Configs;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Testcontainers.PostgreSql;

namespace Dapper;
Expand Down Expand Up @@ -68,6 +67,167 @@ public class MyArgsType
[Benchmark]
public int DapperAOT_Rewrite() => npgsql.Command<MyArgsType>(BasicSql, handler: RewriteCommand.Instance).Execute(Args);

static DbCommand CreateCommand(DbConnection connection)
{
var cmd = connection.CreateCommand();
cmd.Connection = connection;
cmd.CommandText = BasicSql;
cmd.CommandType = CommandType.Text;

var ps = cmd.Parameters;

var p = cmd.CreateParameter();
p.ParameterName = "Name0";
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name0;
ps.Add(p);

p = cmd.CreateParameter();
p.ParameterName = "Name1";
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name1;
ps.Add(p);

p = cmd.CreateParameter();
p.ParameterName = "Name2";
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name1;
ps.Add(p);

p = cmd.CreateParameter();
p.ParameterName = "Name3";
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name1;
ps.Add(p);

return cmd;
}

static void UpdateCommand(DbCommand cmd, DbConnection connection)
{
cmd.Connection = connection;
var ps = cmd.Parameters;
ps[0].Value = Args.Name0;
ps[1].Value = Args.Name1;
ps[2].Value = Args.Name2;
ps[3].Value = Args.Name3;
}

[Benchmark]
public int AdoNetCommand()
{
using var cmd = CreateCommand(npgsql);
return cmd.ExecuteNonQuery();
}

static DbCommand? _spareCommand;

[Benchmark]
public int AdoNetCommandCached()
{
var cmd = Interlocked.Exchange(ref _spareCommand, null);
if (cmd is null)
{
cmd = CreateCommand(npgsql);
}
else
{
UpdateCommand(cmd, npgsql);
}
var result = cmd.ExecuteNonQuery();
cmd.Connection = null;
Interlocked.Exchange(ref _spareCommand, cmd)?.Dispose();
return result;
}
private static DbBatch CreateBatch(DbConnection connection)
{
var batch = connection.CreateBatch();
batch.Connection = connection;

var cmd = batch.CreateBatchCommand();
cmd.CommandText = "insert into RewriteCustomers(Name) values($1)";
cmd.CommandType = CommandType.Text;
var p = new NpgsqlParameter();
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name0;
cmd.Parameters.Add(p);
batch.BatchCommands.Add(cmd);

cmd = batch.CreateBatchCommand();
cmd.CommandText = "insert into RewriteCustomers(Name) values($1)";
cmd.CommandType = CommandType.Text;
p = new NpgsqlParameter();
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name1;
cmd.Parameters.Add(p);
batch.BatchCommands.Add(cmd);

cmd = batch.CreateBatchCommand();
cmd.CommandText = "insert into RewriteCustomers(Name) values($1)";
cmd.CommandType = CommandType.Text;
p = new NpgsqlParameter();
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name2;
cmd.Parameters.Add(p);
batch.BatchCommands.Add(cmd);

cmd = batch.CreateBatchCommand();
cmd.CommandText = "insert into RewriteCustomers(Name) values($1)";
cmd.CommandType = CommandType.Text;
p = new NpgsqlParameter();
p.DbType = DbType.String;
p.Size = -1;
p.Value = Args.Name3;
cmd.Parameters.Add(p);
batch.BatchCommands.Add(cmd);

return batch;
}

static void UpdateBatch(DbBatch batch, DbConnection connection)
{
batch.Connection = connection;
var cmds = batch.BatchCommands;
cmds[0].Parameters[0].Value = Args.Name0;
cmds[1].Parameters[0].Value = Args.Name1;
cmds[2].Parameters[0].Value = Args.Name2;
cmds[3].Parameters[0].Value = Args.Name3;
}

static DbBatch? _spareBatch;

[Benchmark]
public int AdoNetBatch()
{
using var batch = CreateBatch(npgsql);
return batch.ExecuteNonQuery();
}

[Benchmark]
public int AdoNetBatchCached()
{
var batch = Interlocked.Exchange(ref _spareBatch, null);
if (batch is null)
{
batch = CreateBatch(npgsql);
}
else
{
UpdateBatch(batch, npgsql);
}
var result = batch.ExecuteNonQuery();
batch.Connection = null;
Interlocked.Exchange(ref _spareBatch, batch)?.Dispose();
return result;
}

public async ValueTask DisposeAsync()
{
await npgsql.DisposeAsync();
Expand Down
4 changes: 4 additions & 0 deletions test/UsageBenchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ static async Task Main()
Console.WriteLine(obj.Dapper());
Console.WriteLine(obj.DapperAOT_Simple());
Console.WriteLine(obj.DapperAOT_Rewrite());
Console.WriteLine(obj.AdoNetCommand());
Console.WriteLine(obj.AdoNetBatch());
Console.WriteLine(obj.AdoNetCommandCached());
Console.WriteLine(obj.AdoNetBatchCached());
}
//await using (var obj = new BatchInsertBenchmarks())
//{
Expand Down

0 comments on commit 18bebf8

Please sign in to comment.