Performance: Reduce number of database calls in save and publish operations (#20485)

* Added request caching to media picker media retrieval, to improve performance in save operations.

* WIP: Update or insert in bulk when updating property data.

* Add tests verifying UpdateBatch.

* Fixed issue with UpdateBatch and SQL Server.

* Removed stopwatch.

* Fix test on SQLite (failing on SQLServer).

* Added temporary test for direct call to NPoco UpdateBatch.

* Fixed test on SQLServer.

* Add integration test verifying the same property data is persisted as before the performance refactor.

* Log expected warning in DocumentUrlService as debug.
This commit is contained in:
Andy Butland
2025-10-14 11:22:21 +02:00
committed by GitHub
parent 494674d354
commit 12adfd52bd
6 changed files with 142 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models.ContentPublishing;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Core.Services;
@@ -388,4 +389,31 @@ public partial class ContentPublishingServiceTests : UmbracoIntegrationTestWithC
?? throw new InvalidOperationException("Expected a publish date for EN");
Assert.Greater(lastPublishDateEn, firstPublishDateEn);
}
[Test]
public async Task Publishing_Single_Culture_Persists_Expected_Property_Data()
{
var (langEn, langDa, langBe, contentType) = await SetupVariantDoctypeAsync();
var content = await CreateVariantContentAsync(langEn, langDa, langBe, contentType);
using (var scope = ScopeProvider.CreateScope())
{
var dtos = scope.Database.Fetch<PropertyDataDto>();
Assert.AreEqual(18, dtos.Count); // 3 properties * 3 cultures * 2 (published + edited).
scope.Complete();
}
var publishAttempt = await ContentPublishingService.PublishAsync(
content.Key,
[new() { Culture = langEn.IsoCode }],
Constants.Security.SuperUserKey);
Assert.IsTrue(publishAttempt.Success);
using (var scope = ScopeProvider.CreateScope())
{
var dtos = scope.Database.Fetch<PropertyDataDto>();
Assert.AreEqual(19, dtos.Count); // + 3 for published populated title property, - 2 for existing published properties of other cultures.
scope.Complete();
}
}
}

View File

@@ -1,13 +1,10 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using System.Collections.Generic;
using System.Linq;
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
using Umbraco.Extensions;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.NPocoTests;

View File

@@ -0,0 +1,70 @@
// Copyright (c) Umbraco.
// See LICENSE for more details.
using NPoco;
using NUnit.Framework;
using Umbraco.Cms.Infrastructure.Persistence.Dtos;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
namespace Umbraco.Cms.Tests.Integration.Umbraco.Infrastructure.Persistence.NPocoTests;
[TestFixture]
[UmbracoTest(Database = UmbracoTestOptions.Database.NewSchemaPerTest)]
internal sealed class NPocoUpdateBatchTests : UmbracoIntegrationTest
{
[Test]
public void Can_Update_Batch()
{
// Arrange
var servers = new List<ServerRegistrationDto>();
for (var i = 0; i < 3; i++)
{
servers.Add(new ServerRegistrationDto
{
Id = i + 1,
ServerAddress = "address" + i,
ServerIdentity = "computer" + i,
DateRegistered = DateTime.Now,
IsActive = false,
DateAccessed = DateTime.Now,
});
}
using (var scope = ScopeProvider.CreateScope())
{
scope.Database.BulkInsertRecords(servers);
scope.Complete();
}
// Act
for (var i = 0; i < 3; i++)
{
servers[i].ServerAddress = "newaddress" + i;
servers[i].IsActive = true;
}
using (var scope = ScopeProvider.CreateScope())
{
var updateBatch = servers
.Select(x => UpdateBatch.For(x))
.ToList();
var updated = scope.Database.UpdateBatch(updateBatch, new BatchOptions { BatchSize = 100 });
Assert.AreEqual(3, updated);
scope.Complete();
}
// Assert
using (var scope = ScopeProvider.CreateScope())
{
var dtos = scope.Database.Fetch<ServerRegistrationDto>();
Assert.AreEqual(3, dtos.Count);
for (var i = 0; i < 3; i++)
{
Assert.AreEqual(servers[i].ServerAddress, dtos[i].ServerAddress);
Assert.AreEqual(servers[i].ServerIdentity, dtos[i].ServerIdentity);
Assert.AreEqual(servers[i].IsActive, dtos[i].IsActive);
}
}
}
}