Skip to content

Commit a3811aa

Browse files
authored
Log profile.ps1 invocation time (#361)
Log profile.ps1 invocation time
1 parent 330481e commit a3811aa

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/PowerShell/PowerShellManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Collections;
88
using System.Collections.Generic;
99
using System.Collections.ObjectModel;
10+
using System.Diagnostics;
1011
using System.Reflection;
1112
using System.Threading;
1213
using Microsoft.Azure.Functions.PowerShellWorker.Durable;
@@ -163,6 +164,9 @@ internal void InvokeProfile(string profilePath)
163164

164165
try
165166
{
167+
var stopwatch = new Stopwatch();
168+
stopwatch.Start();
169+
166170
// Import-Module on a .ps1 file will evaluate the script in the global scope.
167171
_pwsh.AddCommand(Utils.ImportModuleCmdletInfo)
168172
.AddParameter("Name", profilePath)
@@ -171,6 +175,8 @@ internal void InvokeProfile(string profilePath)
171175
.AddParameter("Force", Utils.BoxedTrue)
172176
.AddParameter("ErrorAction", "SilentlyContinue")
173177
.InvokeAndClearCommands();
178+
179+
Logger.Log(isUserOnlyLog: false, LogLevel.Trace, string.Format(PowerShellWorkerStrings.ProfileInvocationCompleted, stopwatch.ElapsedMilliseconds));
174180
}
175181
catch (Exception e)
176182
{

src/resources/PowerShellWorkerStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@
289289
<data name="FirstFunctionLoadCompleted" xml:space="preserve">
290290
<value>First function load request completed in {0} ms.</value>
291291
</data>
292+
<data name="ProfileInvocationCompleted" xml:space="preserve">
293+
<value>Profile invocation completed in {0} ms.</value>
294+
</data>
292295
<data name="FunctionInvocationStarting" xml:space="preserve">
293296
<value>Invoking function '{0}' code {1} ms after receiving request. Invocation performance details: {2}</value>
294297
</data>

test/Unit/PowerShell/PowerShellManagerTests.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace Microsoft.Azure.Functions.PowerShellWorker.Test
1717
{
18+
using System.Collections.ObjectModel;
1819
using System.Management.Automation;
1920

2021
internal class TestUtils
@@ -285,8 +286,23 @@ public void ProfileShouldWork()
285286
s_testLogger.FullLog.Clear();
286287
testManager.InvokeProfile(profilePath);
287288

288-
Assert.Single(s_testLogger.FullLog);
289-
Assert.Equal("Information: INFORMATION: Hello PROFILE", s_testLogger.FullLog[0]);
289+
var relevantLogs = s_testLogger.FullLog.Where(message => !message.StartsWith("Trace:")).ToList();
290+
Assert.Single(relevantLogs);
291+
Assert.Equal("Information: INFORMATION: Hello PROFILE", relevantLogs[0]);
292+
}
293+
294+
[Fact]
295+
public void ProfileExecutionTimeShouldBeLogged()
296+
{
297+
var profilePath = Path.Join(s_funcDirectory, "ProfileBasic", "profile.ps1");
298+
var testManager = NewTestPowerShellManager(s_testLogger);
299+
300+
s_testLogger.FullLog.Clear();
301+
testManager.InvokeProfile(profilePath);
302+
303+
Assert.Equal(1, s_testLogger.FullLog.Count(
304+
message => message.StartsWith("Trace")
305+
&& message.Contains("Profile invocation completed in")));
290306
}
291307

292308
[Fact]
@@ -299,8 +315,9 @@ public void ProfileWithTerminatingError()
299315
s_testLogger.FullLog.Clear();
300316

301317
Assert.Throws<CmdletInvocationException>(() => testManager.InvokeProfile(profilePath));
302-
Assert.Single(s_testLogger.FullLog);
303-
Assert.Matches("Error: Failed to run profile.ps1. See logs for detailed errors. Profile location: ", s_testLogger.FullLog[0]);
318+
var relevantLogs = s_testLogger.FullLog.Where(message => !message.StartsWith("Trace:")).ToList();
319+
Assert.Single(relevantLogs);
320+
Assert.Matches("Error: Failed to run profile.ps1. See logs for detailed errors. Profile location: ", relevantLogs[0]);
304321
}
305322

306323
[Fact]
@@ -313,10 +330,11 @@ public void ProfileWithNonTerminatingError()
313330
s_testLogger.FullLog.Clear();
314331
testManager.InvokeProfile(profilePath);
315332

316-
Assert.Equal(2, s_testLogger.FullLog.Count);
317-
Assert.StartsWith("Error: ERROR: ", s_testLogger.FullLog[0]);
318-
Assert.Contains("help me!", s_testLogger.FullLog[0]);
319-
Assert.Matches("Error: Failed to run profile.ps1. See logs for detailed errors. Profile location: ", s_testLogger.FullLog[1]);
333+
var relevantLogs = s_testLogger.FullLog.Where(message => !message.StartsWith("Trace:")).ToList();
334+
Assert.Equal(2, relevantLogs.Count);
335+
Assert.StartsWith("Error: ERROR: ", relevantLogs[0]);
336+
Assert.Contains("help me!", relevantLogs[0]);
337+
Assert.Matches("Error: Failed to run profile.ps1. See logs for detailed errors. Profile location: ", relevantLogs[1]);
320338
}
321339

322340
[Fact]

0 commit comments

Comments
 (0)