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 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(); [Benchmark] public void ReadStringArray() => ReadArrayImpl(); [Benchmark] // ReSharper disable once InconsistentNaming public void ReadIPAddressArray() => ReadArrayImpl(); [Benchmark] public void ReadNpgsqlInetArray() // PSV for IPAddress => ReadArrayImpl>(); [Benchmark] public void ReadListOfInt() => ReadListImpl(); [Benchmark] public void ReadListOfString() => ReadListImpl(); [Benchmark] // ReSharper disable once InconsistentNaming public void ReadListOfIPAddress() => ReadListImpl(); [Benchmark] public void ReadListOfNpgsqlInet() // PSV for IPAddress => ReadListImpl>(); [MethodImpl(MethodImplOptions.AggressiveInlining)] void ReadArrayImpl() => _reader.GetFieldValue(0); [MethodImpl(MethodImplOptions.AggressiveInlining)] void ReadListImpl() => _reader.GetFieldValue>(0); class ReadArrayConfig : ManualConfig { public ReadArrayConfig() => Add(StatisticColumn.OperationsPerSecond); } } }