|
16 | 16 | //$Authors = Carlos Guzman Alvarez, Jiri Cincura ([email protected])
|
17 | 17 |
|
18 | 18 | using System;
|
| 19 | +using System.Collections.Generic; |
19 | 20 | using System.Linq;
|
20 | 21 | using System.Reflection;
|
21 | 22 | using System.Threading;
|
@@ -61,4 +62,119 @@ public void CompleteDatabaseInfoTest()
|
61 | 62 | Assert.DoesNotThrowAsync(() => (Task)m.Invoke(dbInfo, new object[] { CancellationToken.None }), m.Name);
|
62 | 63 | }
|
63 | 64 | }
|
| 65 | + |
| 66 | + [Test] |
| 67 | + public void PerformanceAnalysis_SELECT_Test() |
| 68 | + { |
| 69 | + IDictionary<string, short> tableNameList = GetTableNameList(); |
| 70 | + short tableIdTest = tableNameList["TEST"]; |
| 71 | + |
| 72 | + var dbInfo = new FbDatabaseInfo(Connection); |
| 73 | + IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount(); |
| 74 | + IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount(); |
| 75 | + IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount(); |
| 76 | + IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount(); |
| 77 | + |
| 78 | + var fbCommand = new FbCommand("SELECT MAX(INT_FIELD) FROM TEST", Connection); |
| 79 | + var maxIntField = fbCommand.ExecuteScalar() as int?; |
| 80 | + |
| 81 | + insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount()); |
| 82 | + updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount()); |
| 83 | + readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount()); |
| 84 | + readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount()); |
| 85 | + |
| 86 | + Assert.That(insertCount.ContainsKey(tableIdTest), Is.False); |
| 87 | + Assert.That(updateCount.ContainsKey(tableIdTest), Is.False); |
| 88 | + Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.True); |
| 89 | + Assert.That(readSeqCount[tableIdTest], Is.EqualTo(maxIntField + 1)); |
| 90 | + Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.False); |
| 91 | + } |
| 92 | + |
| 93 | + [Test] |
| 94 | + public void PerformanceAnalysis_INSERT_Test() |
| 95 | + { |
| 96 | + IDictionary<string, short> tableNameList = GetTableNameList(); |
| 97 | + short tableIdTest = tableNameList["TEST"]; |
| 98 | + |
| 99 | + var dbInfo = new FbDatabaseInfo(Connection); |
| 100 | + IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount(); |
| 101 | + IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount(); |
| 102 | + IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount(); |
| 103 | + IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount(); |
| 104 | + |
| 105 | + var fbCommand = new FbCommand("INSERT INTO TEST (INT_FIELD) VALUES (900)", Connection); |
| 106 | + fbCommand.ExecuteNonQuery(); |
| 107 | + |
| 108 | + insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount()); |
| 109 | + updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount()); |
| 110 | + readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount()); |
| 111 | + readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount()); |
| 112 | + |
| 113 | + Assert.That(insertCount.ContainsKey(tableIdTest), Is.True); |
| 114 | + Assert.That(insertCount[tableIdTest], Is.EqualTo(1)); |
| 115 | + Assert.That(updateCount.ContainsKey(tableIdTest), Is.False); |
| 116 | + Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.False); |
| 117 | + Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.False); |
| 118 | + } |
| 119 | + |
| 120 | + [Test] |
| 121 | + public void PerformanceAnalysis_UPDATE_Test() |
| 122 | + { |
| 123 | + IDictionary<string, short> tableNameList = GetTableNameList(); |
| 124 | + short tableIdTest = tableNameList["TEST"]; |
| 125 | + |
| 126 | + var fbCommand = new FbCommand("INSERT INTO TEST (INT_FIELD) VALUES (900)", Connection); |
| 127 | + fbCommand.ExecuteNonQuery(); |
| 128 | + |
| 129 | + var dbInfo = new FbDatabaseInfo(Connection); |
| 130 | + IDictionary<short, ulong> insertCount = dbInfo.GetInsertCount(); |
| 131 | + IDictionary<short, ulong> updateCount = dbInfo.GetUpdateCount(); |
| 132 | + IDictionary<short, ulong> readSeqCount = dbInfo.GetReadSeqCount(); |
| 133 | + IDictionary<short, ulong> readIdxCount = dbInfo.GetReadIdxCount(); |
| 134 | + |
| 135 | + fbCommand.CommandText = "UPDATE TEST SET SMALLINT_FIELD = 900 WHERE (INT_FIELD = 900)"; |
| 136 | + fbCommand.ExecuteNonQuery(); |
| 137 | + |
| 138 | + insertCount = GetAffectedTables(insertCount, dbInfo.GetInsertCount()); |
| 139 | + updateCount = GetAffectedTables(updateCount, dbInfo.GetUpdateCount()); |
| 140 | + readSeqCount = GetAffectedTables(readSeqCount, dbInfo.GetReadSeqCount()); |
| 141 | + readIdxCount = GetAffectedTables(readIdxCount, dbInfo.GetReadIdxCount()); |
| 142 | + |
| 143 | + Assert.That(insertCount.ContainsKey(tableIdTest), Is.False); |
| 144 | + Assert.That(updateCount.ContainsKey(tableIdTest), Is.True); |
| 145 | + Assert.That(updateCount[tableIdTest], Is.EqualTo(1)); |
| 146 | + Assert.That(readSeqCount.ContainsKey(tableIdTest), Is.False); |
| 147 | + Assert.That(readIdxCount.ContainsKey(tableIdTest), Is.True); |
| 148 | + Assert.That(readIdxCount[tableIdTest], Is.EqualTo(1)); |
| 149 | + } |
| 150 | + |
| 151 | + private IDictionary<short, ulong> GetAffectedTables(IDictionary<short, ulong> aStatisticInfoBefore, IDictionary<short, ulong> aStatisticInfoAfter) |
| 152 | + { |
| 153 | + var result = new Dictionary<short, ulong>(); |
| 154 | + foreach (KeyValuePair<short, ulong> keyValuePair in aStatisticInfoAfter) |
| 155 | + { |
| 156 | + if (aStatisticInfoBefore.TryGetValue(keyValuePair.Key, out ulong value)) |
| 157 | + { |
| 158 | + ulong counter = keyValuePair.Value - value; |
| 159 | + if (counter > 0) |
| 160 | + result.Add(keyValuePair.Key, counter); |
| 161 | + } |
| 162 | + else |
| 163 | + result.Add(keyValuePair.Key, keyValuePair.Value); |
| 164 | + } |
| 165 | + return result; |
| 166 | + } |
| 167 | + |
| 168 | + private IDictionary<string, short> GetTableNameList() |
| 169 | + { |
| 170 | + IDictionary<string, short> result = new Dictionary<string, short>(); |
| 171 | + |
| 172 | + var command = new FbCommand("select R.RDB$RELATION_ID, R.RDB$RELATION_NAME from RDB$RELATIONS R WHERE RDB$SYSTEM_FLAG = 0", Connection); |
| 173 | + FbDataReader reader = command.ExecuteReader(); |
| 174 | + while (reader.Read()) |
| 175 | + { |
| 176 | + result.Add(reader.GetString(1).Trim(), reader.GetInt16(0)); |
| 177 | + } |
| 178 | + return result; |
| 179 | + } |
64 | 180 | }
|
0 commit comments