forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteVaryingNumberOfParameters.cs
More file actions
57 lines (50 loc) · 1.68 KB
/
WriteVaryingNumberOfParameters.cs
File metadata and controls
57 lines (50 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using NpgsqlTypes;
namespace Npgsql.Benchmarks.Types
{
[Config("columns=OperationPerSecond"), MemoryDiagnoser]
public class WriteVaryingNumberOfParameters
{
NpgsqlConnection _conn;
NpgsqlCommand _cmd;
//[Params(0, 1, 10, 100)]
[Params(10)]
public int NumParams { get; set; }
[GlobalSetup]
public void Setup()
{
_conn = BenchmarkEnvironment.OpenConnection();
var funcParams = string.Join(",",
Enumerable.Range(0, NumParams)
.Select(i => $"IN p{i} int4")
);
using (var cmd = new NpgsqlCommand($"CREATE FUNCTION pg_temp.swallow({funcParams}) RETURNS void AS 'BEGIN END;' LANGUAGE 'plpgsql'", _conn))
cmd.ExecuteNonQuery();
var cmdParams = string.Join(",", Enumerable.Range(0, NumParams).Select(i => $"@p{i}"));
_cmd = new NpgsqlCommand($"SELECT pg_temp.swallow({cmdParams})", _conn);
for (var i = 0; i < NumParams; i++)
_cmd.Parameters.Add(new NpgsqlParameter("p" + i, NpgsqlDbType.Integer));
_cmd.Prepare();
}
[GlobalCleanup]
public void Cleanup()
{
_cmd.Unprepare();
_conn.Close();
}
[Benchmark]
public void WriteParameters()
{
for (var i = 0; i < NumParams; i++)
_cmd.Parameters[i].Value = i;
_cmd.ExecuteNonQuery();
}
}
}