Skip to content

Commit 729b32f

Browse files
committed
trace2: add performance format target
Add the performance format target for TRACE2 tracing. This change includes a few notable components: 1. A new GetMessage method in each child of Trace2Message so that message strings written to normal and performance targets are not duplicated. 2. A new PerformanceFormatComponent class to track sizing of optional properties of performance format messages. 3. A BuildSpan method that creates correctly-sized "spans" of performance format messages. A span is a piece of the message beginning with a pipe (|) and ending just before the next pipe or the end of the message. 4. Unit tests for various string and numerical cases for building spans.
1 parent 86c4b08 commit 729b32f

File tree

6 files changed

+384
-24
lines changed

6 files changed

+384
-24
lines changed

src/shared/Core.Tests/Trace2Tests.cs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
using System;
2+
using System.Runtime.InteropServices;
23
using System.Text.RegularExpressions;
34
using GitCredentialManager.Tests.Objects;
45
using Xunit;
6+
using Xunit.Sdk;
57

68
namespace GitCredentialManager.Tests;
79

10+
internal class TestTrace2Message : Trace2Message
11+
{
12+
public double ElapsedTime { get; set; }
13+
14+
public string Repo { get; set; }
15+
16+
public string Category { get; set; }
17+
18+
public override string ToJson()
19+
{
20+
return "";
21+
}
22+
23+
public override string ToNormalString()
24+
{
25+
return "";
26+
}
27+
28+
public override string ToPerformanceString()
29+
{
30+
return "";
31+
}
32+
}
33+
834
public class Trace2Tests
935
{
1036
[PlatformTheory(Platforms.Posix)]
@@ -30,4 +56,112 @@ public void TryGetPipeName_Windows_Returns_Expected_Value(string input, string e
3056
Assert.True(isSuccessful);
3157
Assert.Matches(actual, expected);
3258
}
59+
60+
[Theory]
61+
[InlineData("", 0)]
62+
[InlineData("foo", 0)]
63+
[InlineData("foo/bar", 1)]
64+
[InlineData("foo/bar/baz", 2)]
65+
public void TryGetProcessDepth_Returns_Expected_Depth(string input, int expected)
66+
{
67+
SidManager.Sid = input;
68+
var commandContext = new TestCommandContext();
69+
var trace2 = new Trace2(commandContext);
70+
trace2.Initialize(DateTimeOffset.UtcNow);
71+
72+
var actual = trace2.GetProcessDepth();
73+
74+
Assert.Equal(expected, actual);
75+
}
76+
77+
[Fact]
78+
public void BuildSpan_NoMatch_Returns_Expected_String()
79+
{
80+
var expected = "| ";
81+
var component = new PerformanceFormatComponent()
82+
{
83+
Name = "Foo",
84+
SpanSize = 10,
85+
BeginPadding = 1,
86+
EndPadding = 1
87+
};
88+
89+
var trace2Message = new TestTrace2Message();
90+
var actual = trace2Message.BuildSpan(component);
91+
92+
Assert.Equal(expected, actual);
93+
}
94+
95+
[Theory]
96+
[InlineData(0.013772, "| 0.013772 ")]
97+
[InlineData(26.316083, "| 26.316083 ")]
98+
[InlineData(100.316083, "|100.316083 ")]
99+
public void BuildSpan_Time_Match_Returns_Expected_String(double input, string expected)
100+
{
101+
var component = new PerformanceFormatComponent()
102+
{
103+
Name = "ElapsedTime",
104+
SpanSize = 11,
105+
BeginPadding = 2,
106+
EndPadding = 1
107+
};
108+
109+
var trace2Message = new TestTrace2Message()
110+
{
111+
ElapsedTime = input
112+
};
113+
114+
var actual = trace2Message.BuildSpan(component);
115+
116+
Assert.Equal(expected, actual);
117+
}
118+
119+
[Theory]
120+
[InlineData("r1", "| r1 ")]
121+
[InlineData("r10", "| r10 ")]
122+
[InlineData("r100", "| r100")]
123+
[InlineData("r1000", "|r1000")]
124+
public void BuildSpan_Repo_Match_Returns_Expected_String(string input, string expected)
125+
{
126+
var component = new PerformanceFormatComponent()
127+
{
128+
Name = "Repo",
129+
SpanSize = 5,
130+
BeginPadding = 1,
131+
EndPadding = 1
132+
};
133+
134+
var trace2Message = new TestTrace2Message()
135+
{
136+
Repo = input
137+
};
138+
139+
var actual = trace2Message.BuildSpan(component);
140+
141+
Assert.Equal(expected, actual);
142+
}
143+
144+
[Theory]
145+
[InlineData("foo", "| foo ")]
146+
[InlineData("foobarbazfooba", "| foobarbazfooba")]
147+
[InlineData("foobarbazfoobar", "|foobarbazfoobar")]
148+
public void BuildSpan_Category_Match_Returns_Expected_String(string input, string expected)
149+
{
150+
var component = new PerformanceFormatComponent()
151+
{
152+
Name = "Category",
153+
SpanSize = 15,
154+
BeginPadding = 1,
155+
EndPadding = 1
156+
};
157+
158+
var trace2Message = new TestTrace2Message()
159+
{
160+
Category = input
161+
};
162+
163+
var actual = trace2Message.BuildSpan(component);
164+
165+
Assert.Equal(expected, actual);
166+
}
33167
}

src/shared/Core/Constants.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public static class EnvironmentVariables
5656
public const string GcmAllowWia = "GCM_ALLOW_WINDOWSAUTH";
5757
public const string GitTrace2Event = "GIT_TRACE2_EVENT";
5858
public const string GitTrace2Normal = "GIT_TRACE2";
59+
public const string GitTrace2Performance = "GIT_TRACE2_PERF";
5960

6061
/*
6162
* Unlike other environment variables, these proxy variables are normally lowercase only.
@@ -169,9 +170,10 @@ public static class Remote
169170

170171
public static class Trace2
171172
{
172-
public const string SectionName = "trace2";
173-
public const string EventTarget = "eventtarget";
174-
public const string NormalTarget = "normaltarget";
173+
public const string SectionName = "trace2";
174+
public const string EventTarget = "eventtarget";
175+
public const string NormalTarget = "normaltarget";
176+
public const string PerformanceTarget = "perftarget";
175177
}
176178
}
177179

src/shared/Core/ITrace2Writer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace GitCredentialManager;
1010
public enum Trace2FormatTarget
1111
{
1212
Event,
13-
Normal
13+
Normal,
14+
Performance
1415
}
1516

1617
public interface ITrace2Writer : IDisposable
@@ -44,6 +45,9 @@ protected string Format(Trace2Message message)
4445
case Trace2FormatTarget.Normal:
4546
sb.Append(message.ToNormalString());
4647
break;
48+
case Trace2FormatTarget.Performance:
49+
sb.Append(message.ToPerformanceString());
50+
break;
4751
default:
4852
Console.WriteLine($"warning: unrecognized format target '{_formatTarget}', disabling TRACE2 tracing.");
4953
Failed = true;

src/shared/Core/Settings.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,12 @@ public Trace2Settings GetTrace2Settings()
526526
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Normal, value);
527527
}
528528

529+
if (TryGetSetting(Constants.EnvironmentVariables.GitTrace2Performance, KnownGitCfg.Trace2.SectionName,
530+
Constants.GitConfiguration.Trace2.PerformanceTarget, out value))
531+
{
532+
settings.FormatTargetsAndValues.Add(Trace2FormatTarget.Performance, value);
533+
}
534+
529535
return settings;
530536
}
531537

src/shared/Core/SidManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SidManager
66
{
77
private const string SidEnvar = "GIT_TRACE2_PARENT_SID";
88

9-
public static string Sid { get; private set; }
9+
public static string Sid { get; internal set; }
1010

1111
public static void CreateSid()
1212
{

0 commit comments

Comments
 (0)