forked from roji/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandExecuteBenchmarks.cs
More file actions
62 lines (54 loc) · 2.13 KB
/
CommandExecuteBenchmarks.cs
File metadata and controls
62 lines (54 loc) · 2.13 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
58
59
60
61
62
using System;
using System.Diagnostics.CodeAnalysis;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
// ReSharper disable UnusedMember.Global
namespace Npgsql.Benchmarks
{
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
[Config(typeof(Config))]
public class CommandExecuteBenchmarks
{
readonly NpgsqlCommand _executeNonQueryCmd;
readonly NpgsqlCommand _executeNonQueryWithParamCmd;
readonly NpgsqlCommand _executeNonQueryPreparedCmd;
readonly NpgsqlCommand _executeScalarCmd;
readonly NpgsqlCommand _executeReaderCmd;
public CommandExecuteBenchmarks()
{
var conn = BenchmarkEnvironment.OpenConnection();
_executeNonQueryCmd = new NpgsqlCommand("SET lock_timeout = 1000", conn);
_executeNonQueryWithParamCmd = new NpgsqlCommand("SET lock_timeout = 1000", conn);
_executeNonQueryWithParamCmd.Parameters.AddWithValue("not_used", DBNull.Value);
_executeNonQueryPreparedCmd = new NpgsqlCommand("SET lock_timeout = 1000", conn);
_executeNonQueryPreparedCmd.Prepare();
_executeScalarCmd = new NpgsqlCommand("SELECT 1", conn);
_executeReaderCmd = new NpgsqlCommand("SELECT 1", conn);
}
[Benchmark]
public int ExecuteNonQuery() => _executeNonQueryCmd.ExecuteNonQuery();
[Benchmark]
public int ExecuteNonQueryWithParam() => _executeNonQueryWithParamCmd.ExecuteNonQuery();
[Benchmark]
public int ExecuteNonQueryPrepared() => _executeNonQueryPreparedCmd.ExecuteNonQuery();
[Benchmark]
public object ExecuteScalar() => _executeScalarCmd.ExecuteScalar();
[Benchmark]
public object ExecuteReader()
{
using (var reader = _executeReaderCmd.ExecuteReader())
{
reader.Read();
return reader.GetValue(0);
}
}
class Config : ManualConfig
{
public Config()
{
Add(StatisticColumn.OperationsPerSecond);
}
}
}
}