Repository navigation
Expand file tree
/
Copy pathStringReaderBenchmark.cs
More file actions
75 lines (59 loc) · 2.62 KB
/
Copy pathStringReaderBenchmark.cs
File metadata and controls
75 lines (59 loc) · 2.62 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
using BenchmarkDotNet.Attributes;
using DuckDB.NET.Data;
namespace DuckDB.NET.Benchmarks;
// One VARCHAR column per benchmark, read with GetString. The columns differ in length and in whether the text is
// ASCII, because decoding costs depend on both. DuckDB keeps a string of up to 12 bytes inside the vector.
[MemoryDiagnoser]
public class StringReaderBenchmark
{
private DuckDBConnection connection = null!;
[Params(200_000)]
public int RowCount { get; set; }
[GlobalSetup]
public void Setup()
{
connection = new DuckDBConnection("DataSource=:memory:");
connection.Open();
LoadedLibrary.Print(connection);
using var command = connection.CreateCommand();
command.CommandText = "CREATE TABLE bench AS SELECT " +
"i::VARCHAR AS short_ascii, " + // 1 to 6 bytes, inline
"'customer-' || md5(i::VARCHAR) AS medium_ascii, " + // 41 bytes
"repeat(md5(i::VARCHAR), 16) AS long_ascii, " + // 512 bytes
"'ქართული 🦆 ' || i::VARCHAR AS short_unicode, " + // about 30 bytes, 3 and 4 byte characters
"repeat('ქართული ტექსტი 🦆 ', 12) || i::VARCHAR AS long_unicode, " + // about 560 bytes
"CASE i % 5 WHEN 0 THEN '' WHEN 1 THEN i::VARCHAR WHEN 2 THEN md5(i::VARCHAR) " +
"WHEN 3 THEN repeat(md5(i::VARCHAR), 16) ELSE 'ქართული ტექსტი 🦆 ' || i::VARCHAR END AS mixed " +
$"FROM range({RowCount}) t(i);";
command.ExecuteNonQuery();
}
[GlobalCleanup]
public void Cleanup()
{
connection.Dispose();
}
private long Read(string column)
{
using var command = connection.CreateCommand();
command.CommandText = $"SELECT {column} FROM bench";
using var reader = command.ExecuteReader();
var length = 0L;
while (reader.Read())
{
length += reader.GetString(0).Length;
}
return length;
}
[Benchmark]
public long ShortAscii() => Read("short_ascii");
[Benchmark]
public long MediumAscii() => Read("medium_ascii");
[Benchmark]
public long LongAscii() => Read("long_ascii");
[Benchmark]
public long ShortUnicode() => Read("short_unicode");
[Benchmark]
public long LongUnicode() => Read("long_unicode");
[Benchmark]
public long Mixed() => Read("mixed");
}