Create a gist now

Instantly share code, notes, and snippets.

Performance comparison code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web.UI;
using Sitecore;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
namespace Website
{
public partial class Comparison : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var db = Factory.GetDatabase("web");
var authors = db.GetItem("/sitecore/content/Solution Data/Authors").GetChildren().ToArray().ToList();
var categories = db.GetItem("/sitecore/content/Solution Data/Categories").GetChildren().ToArray().ToList();
var newsTemplateId = new ID("{5CB0FC6D-DCAA-4A51-8745-D9933F77679A}");
var newsRoot = db.GetItem("/sitecore/content/Home/news");
// Make sure Sitecore is warmed up
newsRoot.Axes.GetDescendants();
var results = new Result[categories.Count, authors.Count];
var sw = new Stopwatch();
Response.Write("<table border=\"1\">");
for (var c = 0; c < categories.Count; c++)
{
Response.Write("<tr>");
for (var a = 0; a < authors.Count; a++)
{
Item[] newsArticles;
int previousCount;
var r = new Result();
sw.Start();
newsArticles = GetNewsArticlesSitecoreQuery(newsRoot, authors[a], categories[c]);
sw.Stop();
r.SitecoreQuery = sw.ElapsedMilliseconds;
sw.Reset();
previousCount = newsArticles.Length;
sw.Start();
newsArticles = GetNewsArticlesSitecoreFastQuery(newsRoot, authors[a], categories[c]);
sw.Stop();
r.SitecoreFastQuery = sw.ElapsedMilliseconds;
sw.Reset();
if (newsArticles.Length != previousCount)
throw new Exception("Inconsistent results returned");
sw.Start();
newsArticles = GetNewsArticlesSitecoreLinkDatabase(newsRoot, authors[a], categories[c]);
sw.Stop();
r.SitecoreLinkDatabase = sw.ElapsedMilliseconds;
sw.Reset();
if (newsArticles.Length != previousCount)
throw new Exception("Inconsistent results returned");
results[c, a] = r;
Response.Write("<td>SQ:" + r.SitecoreQuery + ", FQ: " + r.SitecoreFastQuery + ", LDB: " + r.SitecoreLinkDatabase + "</td>");
}
Response.Write("</tr>");
}
Response.Write("</table>");
}
private Item[] GetNewsArticlesSitecoreQuery(Item newsRoot, Item authorItem, Item categoryItem)
{
var query = newsRoot.Paths.FullPath + "//*[contains(@Author, '" + authorItem.ID + "') and contains(@Categories, '" + categoryItem.ID + "')]";
return newsRoot.Database.SelectItems(query);
}
private Item[] GetNewsArticlesSitecoreFastQuery(Item newsRoot, Item authorItem, Item categoryItem)
{
var query = "fast:" + newsRoot.Paths.FullPath + "//*[@Author = '%" + authorItem.ID + "%' and @Categories = '%" + categoryItem.ID + "%']";
return newsRoot.Database.SelectItems(query);
}
private Item[] GetNewsArticlesSitecoreLinkDatabase(Item newsRoot, Item authorItem, Item categoryItem)
{
// Note. SLDB version of this actually needs to make 2 lookups. First to find all news articles referencing our author, then all that reference the category.
// Note #2: Still, my hand is not shaking ;-)
var authorFieldId = new ID("{438E45E5-9F85-4705-976E-FC76E563F5EF}");
var categoriesFieldId = new ID("{31D2A6CC-6984-4CA0-BB73-9D39C3B8D0AA}");
var authorLinks = Globals.LinkDatabase.GetItemReferrers(authorItem, false).Where(al => al.SourceFieldID == authorFieldId).ToList();
var categoryLinks = Globals.LinkDatabase.GetItemReferrers(categoryItem, false).Where(cl => cl.SourceFieldID == categoriesFieldId).ToList();
var newsArticles = new List<Item>();
foreach (var authorIl in authorLinks)
{
var categoryIl = categoryLinks.ToList().Find(cl => cl.SourceItemID == authorIl.SourceItemID);
if (categoryIl != null && categoryIl.SourceDatabaseName == newsRoot.Database.Name)
{
newsArticles.Add(categoryIl.GetSourceItem());
}
}
return newsArticles.ToArray();
}
}
public struct Result
{
public long NumResults;
public long SitecoreQuery;
public long SitecoreFastQuery;
public long SitecoreLinkDatabase;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment