-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathFbDatabaseInfoTests.cs
More file actions
180 lines (155 loc) · 7.14 KB
/
FbDatabaseInfoTests.cs
File metadata and controls
180 lines (155 loc) · 7.14 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using FirebirdSql.Data.TestsBase;
using NUnit.Framework;
namespace FirebirdSql.Data.FirebirdClient.Tests;
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Default))]
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Embedded))]
public class FbDatabaseInfoTests : FbTestsBase
{
public FbDatabaseInfoTests(FbServerType serverType, bool compression, FbWireCrypt wireCrypt)
: base(serverType, compression, wireCrypt)
{ }
[Test]
public void CompleteDatabaseInfoTest()
{
var dbInfo = new FbDatabaseInfo(Connection);
foreach (var m in dbInfo.GetType()
.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.Where(x => !x.IsSpecialName)
.Where(x => x.Name.EndsWith("Async")))
{
if (ServerVersion < new Version(4, 0, 0, 0)
&& new[] {
nameof(FbDatabaseInfo.GetWireCryptAsync),
nameof(FbDatabaseInfo.GetCryptPluginAsync),
nameof(FbDatabaseInfo.GetNextAttachmentAsync),
nameof(FbDatabaseInfo.GetNextStatementAsync),
nameof(FbDatabaseInfo.GetReplicaModeAsync),
nameof(FbDatabaseInfo.GetDbFileIdAsync),
nameof(FbDatabaseInfo.GetDbGuidAsync),
nameof(FbDatabaseInfo.GetCreationTimestampAsync),
nameof(FbDatabaseInfo.GetProtocolVersionAsync),
nameof(FbDatabaseInfo.GetStatementTimeoutDatabaseAsync),
nameof(FbDatabaseInfo.GetStatementTimeoutAttachmentAsync),
}.Contains(m.Name))
continue;
Assert.DoesNotThrowAsync(() => (Task)m.Invoke(dbInfo, new object[] { CancellationToken.None }), m.Name);
}
}
[Test]
public void PerformanceAnalysis_SELECT_Test()
{
IDictionary<string, short> tableNameList = GetTableNameList();
short tableIdTest = tableNameList["TEST"];
var dbInfo = new FbDatabaseInfo(Connection);
IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount();
IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount();
IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount();
IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount();
var fbCommand = new FbCommand("SELECT MAX(INT_FIELD) FROM TEST", Connection);
var maxIntField = fbCommand.ExecuteScalar() as int?;
insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount());
updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount());
readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount());
readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount());
Assert.That(insertCount.ContainsKey(tableIdTest), Is.False);
Assert.That(updateCount.ContainsKey(tableIdTest), Is.False);
Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.True);
Assert.That(readSeqCount[tableIdTest], Is.EqualTo(maxIntField + 1));
Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.False);
}
[Test]
public void PerformanceAnalysis_INSERT_Test()
{
IDictionary<string, short> tableNameList = GetTableNameList();
short tableIdTest = tableNameList["TEST"];
var dbInfo = new FbDatabaseInfo(Connection);
IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount();
IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount();
IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount();
IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount();
var fbCommand = new FbCommand("INSERT INTO TEST (INT_FIELD) VALUES (900)", Connection);
fbCommand.ExecuteNonQuery();
insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount());
updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount());
readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount());
readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount());
Assert.That(insertCount.ContainsKey(tableIdTest), Is.True);
Assert.That(insertCount[tableIdTest], Is.EqualTo(1));
Assert.That(updateCount.ContainsKey(tableIdTest), Is.False);
Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.False);
Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.False);
}
[Test]
public void PerformanceAnalysis_UPDATE_Test()
{
IDictionary<string, short> tableNameList = GetTableNameList();
short tableIdTest = tableNameList["TEST"];
var fbCommand = new FbCommand("INSERT INTO TEST (INT_FIELD) VALUES (900)", Connection);
fbCommand.ExecuteNonQuery();
var dbInfo = new FbDatabaseInfo(Connection);
IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount();
IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount();
IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount();
IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount();
fbCommand.CommandText = "UPDATE TEST SET SMALLINT_FIELD = 900 WHERE (INT_FIELD = 900)";
fbCommand.ExecuteNonQuery();
insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount());
updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount());
readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount());
readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount());
Assert.That(insertCount.ContainsKey(tableIdTest), Is.False);
Assert.That(updateCount.ContainsKey(tableIdTest), Is.True);
Assert.That(updateCount[tableIdTest], Is.EqualTo(1));
Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.False);
Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.True);
Assert.That(readIdxCount[tableIdTest], Is.EqualTo(1));
}
private IDictionary<short, ulong> GetAffectedTables(IDictionary<short, ulong> aStatisticInfoBefore, IDictionary<short, ulong> aStatisticInfoAfter)
{
var result = new Dictionary<short, ulong>();
foreach (KeyValuePair<short, ulong> keyValuePair in aStatisticInfoAfter)
{
if (aStatisticInfoBefore.TryGetValue(keyValuePair.Key, out ulong value))
{
ulong counter = keyValuePair.Value - value;
if (counter > 0)
result.Add(keyValuePair.Key, counter);
}
else
result.Add(keyValuePair.Key, keyValuePair.Value);
}
return result;
}
private IDictionary<string, short> GetTableNameList()
{
IDictionary<string, short> result = new Dictionary<string, short>();
var command = new FbCommand("select R.RDB$RELATION_ID, R.RDB$RELATION_NAME from RDB$RELATIONS R WHERE RDB$SYSTEM_FLAG = 0", Connection);
FbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
result.Add(reader.GetString(1).Trim(), reader.GetInt16(0));
}
return result;
}
}