forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadArray.cs
More file actions
102 lines (84 loc) · 3.22 KB
/
ReadArray.cs
File metadata and controls
102 lines (84 loc) · 3.22 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
namespace Npgsql.Benchmarks
{
[Config(typeof(ReadArrayConfig))]
public class ReadArray
{
NpgsqlConnection _conn = default!;
NpgsqlCommand _cmd = default!;
NpgsqlDataReader _reader = default!;
[GlobalSetup(Target = nameof(ReadIntArray) + "," + nameof(ReadListOfInt))]
public void GlobalSetupForInt()
=> GlobalSetupImpl(42);
[GlobalSetup(Target = nameof(ReadStringArray) + "," + nameof(ReadListOfString))]
public void GlobalSetupForString()
=> GlobalSetupImpl("The Answer to the Ultimate Question of Life, The Universe, and Everything.");
[GlobalSetup(Target = nameof(ReadIPAddressArray)
+ "," + nameof(ReadNpgsqlInetArray)
+ "," + nameof(ReadListOfIPAddress)
+ "," + nameof(ReadListOfNpgsqlInet))]
public void GlobalSetupForInet()
=> GlobalSetupImpl(IPAddress.Loopback);
void GlobalSetupImpl<T>(T initializationValue)
{
_conn = BenchmarkEnvironment.OpenConnection();
_cmd = new NpgsqlCommand("SELECT @p1;", _conn);
_cmd.Parameters.AddWithValue("@p1", Enumerable.Repeat(initializationValue, NumArrayElements).ToArray());
_reader = _cmd.ExecuteReader();
_reader.Read();
}
[Params(0, 10, 1000, 100000)]
public int NumArrayElements { get; set; }
[GlobalCleanup]
public void Cleanup()
{
_reader.Dispose();
_cmd.Dispose();
_conn.Dispose();
}
[Benchmark]
public void ReadIntArray()
=> ReadArrayImpl<int>();
[Benchmark]
public void ReadStringArray()
=> ReadArrayImpl<string>();
[Benchmark]
// ReSharper disable once InconsistentNaming
public void ReadIPAddressArray()
=> ReadArrayImpl<IPAddress>();
[Benchmark]
public void ReadNpgsqlInetArray() // PSV for IPAddress
=> ReadArrayImpl<ValueTuple<IPAddress, int>>();
[Benchmark]
public void ReadListOfInt()
=> ReadListImpl<int>();
[Benchmark]
public void ReadListOfString()
=> ReadListImpl<string>();
[Benchmark]
// ReSharper disable once InconsistentNaming
public void ReadListOfIPAddress()
=> ReadListImpl<IPAddress>();
[Benchmark]
public void ReadListOfNpgsqlInet() // PSV for IPAddress
=> ReadListImpl<ValueTuple<IPAddress, int>>();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void ReadArrayImpl<T>()
=> _reader.GetFieldValue<T[]>(0);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void ReadListImpl<T>()
=> _reader.GetFieldValue<List<T>>(0);
class ReadArrayConfig : ManualConfig
{
public ReadArrayConfig() => Add(StatisticColumn.OperationsPerSecond);
}
}
}