Skip to content

Commit

Permalink
Optimize progress reporting by reusing StringBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Apr 21, 2023
1 parent 40b402b commit 3136660
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/NUnitFramework/framework/Interfaces/TNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ public TNode(string name, string? value, bool valueIsCDATA)
/// </summary>
public TNode? FirstChild => ChildNodes.Count == 0 ? null : ChildNodes[0];

[ThreadStatic]
private static StringBuilder? _outerXmlStringBuilder;

/// <summary>
/// Gets the XML representation of this node.
/// </summary>
public string OuterXml
{
get
{
var stringWriter = new System.IO.StringWriter();
var builder = _outerXmlStringBuilder ??= new StringBuilder();
builder.Clear();

var stringWriter = new System.IO.StringWriter(builder);
var settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

Expand Down
34 changes: 29 additions & 5 deletions src/NUnitFramework/framework/Internal/TestProgressReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#nullable enable

using System;
using System.Text;
using System.Web.UI;
using NUnit.Framework.Interfaces;

Expand Down Expand Up @@ -30,6 +31,9 @@ public TestProgressReporter(ICallbackEventHandler handler)

#region ITestListener Members

[ThreadStatic]
private static StringBuilder? _testStartedStringBuilder;

/// <summary>
/// Called when a test has just started
/// </summary>
Expand All @@ -39,19 +43,39 @@ public void TestStarted(ITest test)
var parent = GetParent(test);
try
{
string report;
var report = _testStartedStringBuilder ??= new StringBuilder();
report.Clear();
if (test is TestSuite)
{
report
.Append("<start-suite id=\"").Append(test.Id).Append('"')
.Append(" parentId=\"").Append(parent != null ? parent.Id : string.Empty).Append('"')
.Append(" name=\"").Append(FormatAttributeValue(test.Name)).Append('"')
.Append(" fullname=\"").Append(FormatAttributeValue(test.FullName)).Append('"')
.Append(" type=\"").Append(test.TestType).Append('"');

// Only add framework-version for the Assembly start-suite
string version = test.TestType == "Assembly" ? $"framework-version=\"{typeof(TestProgressReporter).Assembly.GetName().Version}\" " : "";
report = $"<start-suite id=\"{test.Id}\" parentId=\"{(parent != null ? parent.Id : string.Empty)}\" name=\"{FormatAttributeValue(test.Name)}\" fullname=\"{FormatAttributeValue(test.FullName)}\" type=\"{test.TestType}\" {version}/>";
if (test.TestType == "Assembly")
{
report.Append(" framework-version=\"").Append(typeof(TestProgressReporter).Assembly.GetName().Version).Append('"');
}

report.Append("/>");
}
else
{
report = $"<start-test id=\"{test.Id}\" parentId=\"{(parent != null ? parent.Id : string.Empty)}\" name=\"{FormatAttributeValue(test.Name)}\" fullname=\"{FormatAttributeValue(test.FullName)}\" type=\"{test.TestType}\" classname=\"{FormatAttributeValue(test.ClassName ?? "")}\" methodname=\"{FormatAttributeValue(test.MethodName ?? "")}\"/>";
report
.Append("<start-test id=\"").Append(test.Id).Append('"')
.Append(" parentId=\"").Append(parent != null ? parent.Id : string.Empty).Append('"')
.Append(" name=\"").Append(FormatAttributeValue(test.Name)).Append('"')
.Append(" fullname=\"").Append(FormatAttributeValue(test.FullName)).Append('"')
.Append(" type=\"").Append(test.TestType).Append('"')
.Append(" classname=\"").Append(FormatAttributeValue(test.ClassName ?? string.Empty)).Append('"')
.Append(" methodname=\"").Append(FormatAttributeValue(test.MethodName ?? string.Empty)).Append('"')
.Append("/>");
}

handler.RaiseCallbackEvent(report);
handler.RaiseCallbackEvent(report.ToString());
}
catch (Exception ex)
{
Expand Down

0 comments on commit 3136660

Please sign in to comment.