forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumeric.cs
More file actions
101 lines (91 loc) · 3.13 KB
/
Numeric.cs
File metadata and controls
101 lines (91 loc) · 3.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
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
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using Npgsql.TypeHandlers.NumericHandlers;
using System;
using System.IO;
using System.Text;
namespace Npgsql.Benchmarks
{
[Config(typeof(Config))]
public class Numeric
{
class Config : ManualConfig
{
public Config()
{
Add(StatisticColumn.OperationsPerSecond);
Add(MemoryDiagnoser.Default);
}
}
class EndlessStream : Stream
{
public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => true;
public override long Length => long.MaxValue;
public override long Position { get => 0L; set { } }
public override void Flush() { }
public override int Read(byte[] buffer, int offset, int count) => count;
public override long Seek(long offset, SeekOrigin origin) => 0L;
public override void SetLength(long value) { }
public override void Write(byte[] buffer, int offset, int count) { }
}
readonly EndlessStream _stream = new EndlessStream();
readonly NumericHandler _handler = new NumericHandler();
readonly NpgsqlReadBuffer _readBuffer;
readonly NpgsqlWriteBuffer _writeBuffer;
decimal _value;
int _elementSize;
public Numeric()
{
_stream = new EndlessStream(); _handler = new NumericHandler();
_readBuffer = new NpgsqlReadBuffer(null, _stream, NpgsqlReadBuffer.MinimumSize, Encoding.UTF8);
_writeBuffer = new NpgsqlWriteBuffer(null, _stream, NpgsqlWriteBuffer.MinimumSize, Encoding.UTF8);
}
public decimal Value
{
get => _value;
set
{
_value = value;
_elementSize = _handler.ValidateAndGetLength(value, null);
_handler.Write(_value, _writeBuffer, null);
Buffer.BlockCopy(_writeBuffer.Buffer, 0, _readBuffer.Buffer, 0, _elementSize);
_readBuffer.FilledBytes = _elementSize;
_writeBuffer.WritePosition = 0;
}
}
public static decimal[] Values => new decimal[]
{
0.0000000000000000000000000001M,
0.000000000000000000000001M,
0.00000000000000000001M,
0.0000000000000001M,
0.000000000001M,
0.00000001M,
0.0001M,
1M,
10000M,
100000000M,
1000000000000M,
10000000000000000M,
100000000000000000000M,
1000000000000000000000000M,
10000000000000000000000000000M,
};
[Benchmark]
public void Read()
{
_readBuffer.ReadPosition = 0;
_handler.Read(_readBuffer, _elementSize);
}
[Benchmark]
public void Write()
{
_writeBuffer.WritePosition = 0;
_handler.Write(_value, _writeBuffer, null);
}
}
}