Skip to content

Instantly share code, notes, and snippets.

@alan-null
Last active February 29, 2024 12:45
Show Gist options
  • Save alan-null/9bcc770b9987045ee7e1201be0bfe761 to your computer and use it in GitHub Desktop.
Save alan-null/9bcc770b9987045ee7e1201be0bfe761 to your computer and use it in GitHub Desktop.
Get items - the fastest way

Get items - the fastest way

This is a supplement for following blog post: Get items - the fastest way

Conents

  • Generate test data.ps1 - use this first to create items for tests,
  • Logic.cs - compile it into DLL and deploy to your Sitecore instance or use compiled DLL (Alan.Sitecore.Test.dll)
  • Run tests.ps1 - run tests and see results

image

$root = "/sitecore/content"
$testTemplate = New-Item -Path "/sitecore/templates/User Defined" -Name "TestItem" -ItemType "System/Templates/Template"
[Sitecore.Data.Items.TemplateItem]$testTemplate = $testTemplate
$performanceRoot = New-Item -Path $root -Name "PerformanceRoot" -ItemType $testTemplate.FullName
0..9 | % {
$level0 = $_
$level0Parent = New-Item -Parent $performanceRoot -Name $level0 -ItemType $testTemplate.FullName
0..9 | % {
$level1 = $_
$level1Parent = New-Item -Parent $level0Parent -Name $level1 -ItemType $testTemplate.FullName
0..9 | % {
$level2 = $_
New-Item -Parent $level1Parent -Name $level2 -ItemType $testTemplate.FullName | Out-Null
}
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Sitecore.Caching;
using Sitecore.Collections;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Managers;
using Sitecore.Reflection;
namespace Alan.Sitecore.Test
{
public class Result
{
public List<Item> Items { get; set; }
public TimeSpan TimeSpan { get; set; }
public string FunctionName { get; set; }
public Result()
{
Items = new ItemList();
}
}
public static class Tests
{
public static bool ShouldClearCache { get; set; } = false;
public static void ClearCache()
{
foreach (ICacheInfo c in CacheManager.GetAllCaches())
{
c.Clear();
}
TypeUtil.ClearSizeCache();
}
public static Result GetResult(Func<Result> f)
{
if (ShouldClearCache)
{
ClearCache();
}
var stopwatch = Stopwatch.StartNew();
var result = f.Invoke();
stopwatch.Stop();
result.TimeSpan = stopwatch.Elapsed;
return result;
}
public static Result GetDescendantsSingleItem(Item item, ID id, int count)
{
return GetResult(() => Logic.GetDescendantsSingleItem(item, id, count));
}
public static Result GetDescendantsAllItems(Item item, ID id, int count)
{
return GetResult(() => Logic.GetDescendantsAllItems(item, id, count));
}
public static Result ChildrenSingleLevel(Item item, ID id, int count)
{
return GetResult(() => Logic.ChildrenSingleLevel(item, id, count));
}
public static Result ChildrenRecursive(Item item, ID id, int count)
{
return GetResult(() => Logic.ChildrenRecursive(item, id, count));
}
public static Result ChildrenSingleItem(Item item, ID id, int count)
{
return GetResult(() => Logic.ChildrenSingleItem(item, id, count));
}
public static Result SelectItems(Item item, string query, int count)
{
return GetResult(() => Logic.SelectItems(item, query, count));
}
public static Result SelectItem(Item item, string query, int count)
{
return GetResult(() => Logic.SelectItem(item, query, count));
}
public static Result SelectItemsFast(Item item, string query, int count)
{
return GetResult(() => Logic.SelectItemsFast(item, query, count));
}
public static Result SelectItemFast(Item item, string query, int count)
{
return GetResult(() => Logic.SelectItemFast(item, query, count));
}
}
public static class Logic
{
public static Result GetDescendantsSingleItem(Item item, ID id, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
var items = item.Axes.GetDescendants().FirstOrDefault(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id));
r.Items.Add(items);
}
return r;
}
public static Result GetDescendantsAllItems(Item item, ID id, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
var items = item.Axes.GetDescendants().Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList();
r.Items.AddRange(items);
}
return r;
}
public static Result ChildrenSingleLevel(Item item, ID id, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
var items = item.Children.Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList();
r.Items.AddRange(items);
}
return r;
}
public static Result ChildrenSingleItem(Item item, ID id, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
var items = item.Children.FirstOrDefault(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id));
r.Items.Add(items);
}
return r;
}
public static Result ChildrenRecursive(Item item, ID id, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
var itemChildren = item.Children;
var items = itemChildren.Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList();
r.Items.AddRange(items);
foreach (Item item1 in itemChildren)
{
if (item1.HasChildren)
{
r.Items.AddRange(ChildrenRecursive(item1, id, 1).Items);
}
}
}
return r;
}
public static Result SelectItems(Item item, string name, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
r.Items.AddRange(item.Axes.SelectItems(name));
}
return r;
}
public static Result SelectItem(Item item, string name, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
r.Items.Add(item?.Axes.SelectSingleItem(name));
}
return r;
}
public static Result SelectItemFast(Item item, string name, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
r.Items.Add(item.Database.SelectSingleItem(name));
}
return r;
}
public static Result SelectItemsFast(Item item, string name, int count)
{
var r = new Result();
for (int i = 0; i < count; i++)
{
r.Items.AddRange(item.Database.SelectItems(name));
}
return r;
}
}
}
# configuration
$count = 1
[Alan.Sitecore.Test.Tests]::ShouldClearCache = $false
# configuration - end
$results = @()
$item = Get-Item -Path "/sitecore/content/PerformanceRoot"
$templateItem = Get-Item -Path "/sitecore/templates/User Defined/TestItem"
$GetDescendantsAllItems = [Alan.Sitecore.Test.Tests]::GetDescendantsAllItems($item, $templateItem.ID, $count)
$GetDescendantsAllItems.FunctionName = "GetDescendants - AllItems"
$results += $GetDescendantsAllItems
$ChildrenRecursive = [Alan.Sitecore.Test.Tests]::ChildrenRecursive($item, $templateItem.ID, $count)
$ChildrenRecursive.FunctionName = "Children - Recursive"
$results += $ChildrenRecursive
$SelectItemsRecursive = [Alan.Sitecore.Test.Tests]::SelectItems($item, ".//*[@@templatename='TestItem']", $count)
$SelectItemsRecursive.FunctionName = "Select Items - AllItems"
$results += $SelectItemsRecursive
$query = "fast:$($item.Paths.path)//*[@@templatename='TestItem']"
$SelectItemsFast = [Alan.Sitecore.Test.Tests]::SelectItemsFast($item, $query, $count)
$SelectItemsFast.FunctionName = "Select Items Fast - AllItems"
$results += $SelectItemsFast
$ChildrenSingleLevel = [Alan.Sitecore.Test.Tests]::ChildrenSingleLevel($item, $templateItem.ID, $count)
$ChildrenSingleLevel.FunctionName = "Children - SingleLevel"
$results += $ChildrenSingleLevel
$SelectItems = [Alan.Sitecore.Test.Tests]::SelectItems($item, "./*[@@templatename='TestItem']", $count)
$SelectItems.FunctionName = "Select Items - SingleLevel"
$results += $SelectItems
$GetDescendantsSingleItem = [Alan.Sitecore.Test.Tests]::GetDescendantsSingleItem($item, $templateItem.ID, $count)
$GetDescendantsSingleItem.FunctionName = "GetDescendants - SingleItem"
$results += $GetDescendantsSingleItem
$ChildrenSingleItem = [Alan.Sitecore.Test.Tests]::ChildrenSingleItem($item, $templateItem.ID, $count)
$ChildrenSingleItem.FunctionName = "Children - SingleItem"
$results += $ChildrenSingleItem
$SelectItem = [Alan.Sitecore.Test.Tests]::SelectItem($item, "./*[@@templatename='TestItem']", $count)
$SelectItem.FunctionName = "Single Item - SingleLevel"
$results += $SelectItem
$SelectItemRecursive = [Alan.Sitecore.Test.Tests]::SelectItem($item, ".//*[@@templatename='TestItem']", $count)
$SelectItemRecursive.FunctionName = "Single Item - Recursive"
$results += $SelectItemRecursive
$query = "fast:$($item.Paths.path)/*[@@templatename='TestItem']"
$SelectItemFast = [Alan.Sitecore.Test.Tests]::SelectItemFast($item, $query, $count)
$SelectItemFast.FunctionName = "Select Item Fast - SingleItem"
$results += $SelectItemFast
$query = "fast:$($item.Paths.path)//*[@@templatename='TestItem']"
$SelectItemFast = [Alan.Sitecore.Test.Tests]::SelectItemFast($item, $query, $count)
$SelectItemFast.FunctionName = "Select Item Fast - AllItems"
$results += $SelectItemFast
$results | Format-Table @{n = 'Function'; e = { $_.FunctionName } }, @{n = 'Time[ms]'; e = { $_.TimeSpan.TotalMilliseconds } }, @{n = 'Items'; e = { $_.Items.Count } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment