|
| 1 | +// Copyright (C) 2023 Xtensive LLC. |
| 2 | +// This code is distributed under MIT license terms. |
| 3 | +// See the License.txt file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using NUnit.Framework; |
| 9 | +using Xtensive.Core; |
| 10 | +using Xtensive.Orm.Configuration; |
| 11 | +using Xtensive.Orm.Providers; |
| 12 | +using Xtensive.Orm.Tests.Storage.TemporaryTablePopulationTestModel; |
| 13 | + |
| 14 | +namespace Xtensive.Orm.Tests.Storage.TemporaryTablePopulationTestModel |
| 15 | +{ |
| 16 | + [HierarchyRoot] |
| 17 | + [KeyGenerator(KeyGeneratorKind.None)] |
| 18 | + public sealed class TestEntity : Entity |
| 19 | + { |
| 20 | + [Field, Key] |
| 21 | + public int Id { get; private set; } |
| 22 | + |
| 23 | + [Field(Length = 50)] |
| 24 | + public string Name { get; private set; } |
| 25 | + |
| 26 | + public TestEntity(Session session, int id) |
| 27 | + : base(session, id) |
| 28 | + { |
| 29 | + Name = id.ToString(); |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +namespace Xtensive.Orm.Tests.Storage |
| 35 | +{ |
| 36 | + [TestFixture] |
| 37 | + public class TemporaryTablePopulationTest : AutoBuildTest |
| 38 | + { |
| 39 | + protected override void CheckRequirements() => |
| 40 | + Require.AnyFeatureSupported(ProviderFeatures.TemporaryTables | ProviderFeatures.TemporaryTableEmulation); |
| 41 | + |
| 42 | + protected override DomainConfiguration BuildConfiguration() |
| 43 | + { |
| 44 | + var domainConfiguration = DomainConfigurationFactory.Create(); |
| 45 | + domainConfiguration.MaxNumberOfConditions = 2; // basically force to use temp table |
| 46 | + domainConfiguration.Types.Register(typeof(TestEntity)); |
| 47 | + domainConfiguration.UpgradeMode = DomainUpgradeMode.Recreate; |
| 48 | + return domainConfiguration; |
| 49 | + } |
| 50 | + |
| 51 | + protected override void PopulateData() |
| 52 | + { |
| 53 | + using(var session = Domain.OpenSession()) |
| 54 | + using(var tx = session.OpenTransaction()) { |
| 55 | + _ = new TestEntity(session, -300); |
| 56 | + |
| 57 | + tx.Complete(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + [Test] |
| 62 | + [TestCase(WellKnown.MultiRowInsertSmallBatchSize - 1)] |
| 63 | + [TestCase(WellKnown.MultiRowInsertSmallBatchSize)] |
| 64 | + [TestCase(WellKnown.MultiRowInsertSmallBatchSize + 2)] |
| 65 | + [TestCase(WellKnown.MultiRowInsertSmallBatchSize * 2 + 2)] |
| 66 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize - 1)] |
| 67 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize)] |
| 68 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize + 2)] |
| 69 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize + WellKnown.MultiRowInsertSmallBatchSize * 2)] |
| 70 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize + WellKnown.MultiRowInsertSmallBatchSize * 2 + 2)] |
| 71 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize * 2)] |
| 72 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize * 2 + WellKnown.MultiRowInsertSmallBatchSize * 2)] |
| 73 | + [TestCase(WellKnown.MultiRowInsertBigBatchSize * 2 + WellKnown.MultiRowInsertSmallBatchSize * 2 + 2)] |
| 74 | + public void MainTest(int numOfValues) |
| 75 | + { |
| 76 | + var commands = new List<(string Command, int ParamtersCount)>(); |
| 77 | + |
| 78 | + var values = Enumerable.Range(0, numOfValues).ToArray(numOfValues); |
| 79 | + |
| 80 | + var sessionConfig = new SessionConfiguration("DefaultNoBatching", SessionOptions.Default) { BatchSize = 1 }; |
| 81 | + |
| 82 | + using (var session = Domain.OpenSession(sessionConfig)) |
| 83 | + using (var tx = session.OpenTransaction()) { |
| 84 | + session.Events.DbCommandExecuting += LogCommands; |
| 85 | + _ = session.Query.All<TestEntity>().Where(e => e.Id.In(values)).ToList(); |
| 86 | + session.Events.DbCommandExecuting += LogCommands; |
| 87 | + } |
| 88 | + |
| 89 | + var (bigBatches, smallBatches, sigleRows) = GetExpectedCommandsCount(numOfValues); |
| 90 | + |
| 91 | + Assert.That(commands.Count, |
| 92 | + Is.EqualTo(bigBatches + smallBatches + sigleRows)); |
| 93 | + |
| 94 | + var bigBatchesCount = bigBatches; |
| 95 | + var smallBatchesCount = smallBatches; |
| 96 | + var singleRowsCount = sigleRows; |
| 97 | + |
| 98 | + for(var i = 0; i < commands.Count; i++) { |
| 99 | + var command = commands[i]; |
| 100 | + if (bigBatchesCount != 0) { |
| 101 | + Assert.That(command.ParamtersCount, Is.EqualTo(WellKnown.MultiRowInsertBigBatchSize)); |
| 102 | + bigBatchesCount--; |
| 103 | + } |
| 104 | + else if (smallBatchesCount != 0) { |
| 105 | + Assert.That(command.ParamtersCount, Is.EqualTo(WellKnown.MultiRowInsertSmallBatchSize)); |
| 106 | + smallBatchesCount--; |
| 107 | + } |
| 108 | + else { |
| 109 | + Assert.That(command.ParamtersCount, Is.EqualTo(1)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void LogCommands(object sender, DbCommandEventArgs args) |
| 114 | + { |
| 115 | + var c = args.Command; |
| 116 | + if (c.CommandText.Contains("INSERT")) { |
| 117 | + commands.Add((c.CommandText, c.Parameters.Count)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + [Test] |
| 123 | + public void BatchesParameterCountControlCompatibilityTest() |
| 124 | + { |
| 125 | + Require.AllFeaturesSupported(ProviderFeatures.DmlBatches); |
| 126 | + |
| 127 | + var maxParametersCount = StorageProviderInfo.Instance.Info.MaxQueryParameterCount; |
| 128 | + if(maxParametersCount == int.MaxValue) { |
| 129 | + throw new IgnoreException("Test does not support unlimited parameter count."); |
| 130 | + } |
| 131 | + |
| 132 | + var commands = new List<(string Command, int ParamtersCount)>(); |
| 133 | + |
| 134 | + // to not fit into one batch of statements |
| 135 | + var possibleParametersInBatch = SessionConfiguration.DefaultBatchSize * WellKnown.MultiRowInsertBigBatchSize; |
| 136 | + |
| 137 | + var numOfValues = (maxParametersCount > possibleParametersInBatch) |
| 138 | + ? ((possibleParametersInBatch / WellKnown.MultiRowInsertBigBatchSize) + 1) * WellKnown.MultiRowInsertBigBatchSize |
| 139 | + : ((maxParametersCount / WellKnown.MultiRowInsertBigBatchSize) + 1) * WellKnown.MultiRowInsertBigBatchSize; |
| 140 | + |
| 141 | + var values = Enumerable.Range(0, numOfValues).ToArray(numOfValues); |
| 142 | + |
| 143 | + using (var session = Domain.OpenSession()) |
| 144 | + using (var tx = session.OpenTransaction()) { |
| 145 | + session.Events.DbCommandExecuting += LogCommands; |
| 146 | + _ = session.Query.All<TestEntity>().Where(e => e.Id.In(values)).ToList(); |
| 147 | + session.Events.DbCommandExecuting += LogCommands; |
| 148 | + } |
| 149 | + |
| 150 | + Assert.That(commands.Count, Is.EqualTo(2)); |
| 151 | + |
| 152 | + commands = new List<(string Command, int ParamtersCount)>(); |
| 153 | + |
| 154 | + // to not fit into one batch of statements |
| 155 | + numOfValues = (maxParametersCount > possibleParametersInBatch) |
| 156 | + ? (possibleParametersInBatch / WellKnown.MultiRowInsertBigBatchSize) * WellKnown.MultiRowInsertBigBatchSize |
| 157 | + : (maxParametersCount / WellKnown.MultiRowInsertBigBatchSize) * WellKnown.MultiRowInsertBigBatchSize; |
| 158 | + values = Enumerable.Range(0, numOfValues).ToArray(numOfValues); |
| 159 | + |
| 160 | + using (var session = Domain.OpenSession()) |
| 161 | + using (var tx = session.OpenTransaction()) { |
| 162 | + session.Events.DbCommandExecuting += LogCommands; |
| 163 | + _ = session.Query.All<TestEntity>().Where(e => e.Id.In(values)).ToList(); |
| 164 | + session.Events.DbCommandExecuting += LogCommands; |
| 165 | + } |
| 166 | + |
| 167 | + Assert.That(commands.Count, Is.EqualTo(1)); |
| 168 | + |
| 169 | + void LogCommands(object sender, DbCommandEventArgs args) |
| 170 | + { |
| 171 | + var c = args.Command; |
| 172 | + if (c.CommandText.Contains("INSERT")) { |
| 173 | + commands.Add((c.CommandText, c.Parameters.Count)); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private (int bigBatches, int smallBatches, int sigleRows) GetExpectedCommandsCount(int numberOfValues) |
| 179 | + { |
| 180 | + var numberOfBigs = numberOfValues / WellKnown.MultiRowInsertBigBatchSize; |
| 181 | + var valuesLeft = numberOfValues - numberOfBigs * WellKnown.MultiRowInsertBigBatchSize; |
| 182 | + var numberOfSmalls = valuesLeft / WellKnown.MultiRowInsertSmallBatchSize; |
| 183 | + valuesLeft = valuesLeft - numberOfSmalls * WellKnown.MultiRowInsertSmallBatchSize; |
| 184 | + return (numberOfBigs, numberOfSmalls, valuesLeft); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments