-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: improve RuntimeSupport integration test performance
- Loading branch information
Showing
10 changed files
with
233 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...Support.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/CommandLineWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers; | ||
|
||
public static class CommandLineWrapper | ||
{ | ||
public static async Task Run(string command, string arguments, string workingDirectory, CancellationToken cancellationToken = default) | ||
{ | ||
var processStartInfo = new ProcessStartInfo | ||
{ | ||
FileName = command, | ||
Arguments = arguments, | ||
WorkingDirectory = workingDirectory, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
}; | ||
|
||
using (var process = new Process { StartInfo = processStartInfo, EnableRaisingEvents = true }) | ||
{ | ||
var tcs = new TaskCompletionSource<bool>(); | ||
|
||
// Handle process exit event | ||
process.Exited += (sender, args) => | ||
{ | ||
tcs.TrySetResult(true); | ||
}; | ||
|
||
try | ||
{ | ||
// Attach event handlers | ||
process.OutputDataReceived += (sender, args) => | ||
{ | ||
if (!string.IsNullOrEmpty(args.Data)) | ||
{ | ||
Console.WriteLine(args.Data); | ||
} | ||
}; | ||
|
||
process.ErrorDataReceived += (sender, args) => | ||
{ | ||
if (!string.IsNullOrEmpty(args.Data)) | ||
{ | ||
Console.WriteLine(args.Data); | ||
} | ||
}; | ||
|
||
// Start the process | ||
process.Start(); | ||
|
||
// Begin asynchronous read operations | ||
process.BeginOutputReadLine(); | ||
process.BeginErrorReadLine(); | ||
|
||
// Wait for the process to exit or cancellation | ||
var completedTask = await Task.WhenAny(tcs.Task, Task.Delay(Timeout.Infinite, cancellationToken)); | ||
|
||
if (completedTask == tcs.Task) | ||
{ | ||
// Process exited normally | ||
await tcs.Task; // Just to propagate any exceptions | ||
} | ||
else | ||
{ | ||
// Cancellation requested | ||
if (!process.HasExited) | ||
{ | ||
process.Kill(); | ||
} | ||
throw new OperationCanceledException(cancellationToken); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine("Exception: " + ex); | ||
if (!process.HasExited) | ||
{ | ||
process.Kill(); | ||
} | ||
} | ||
|
||
Assert.True(process.ExitCode == 0, $"Command '{command} {arguments}' failed."); | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...eSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/Helpers/LambdaToolsHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System.IO; | ||
using System.Runtime.InteropServices; | ||
using System.Threading.Tasks; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers; | ||
|
||
public static class LambdaToolsHelper | ||
{ | ||
private static readonly string FunctionArchitecture = RuntimeInformation.OSArchitecture == System.Runtime.InteropServices.Architecture.Arm64 ? "arm64" : "x86_64"; | ||
|
||
public static string GetTempTestAppDirectory(string workingDirectory, string testAppPath) | ||
{ | ||
var customTestAppPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(customTestAppPath); | ||
|
||
var currentDir = new DirectoryInfo(workingDirectory); | ||
CopyDirectory(currentDir, customTestAppPath); | ||
|
||
return Path.Combine(customTestAppPath, testAppPath); | ||
} | ||
|
||
public static async Task<string> InstallLambdaTools() | ||
{ | ||
var customToolPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
Directory.CreateDirectory(customToolPath); | ||
await CommandLineWrapper.Run( | ||
"dotnet", | ||
$"tool install Amazon.Lambda.Tools --tool-path {customToolPath}", | ||
Directory.GetCurrentDirectory()); | ||
return customToolPath; | ||
} | ||
|
||
public static async Task LambdaPackage(string toolPath, string framework, string workingDirectory) | ||
{ | ||
string lambdaToolPath = Path.Combine(toolPath, "dotnet-lambda"); | ||
await CommandLineWrapper.Run( | ||
lambdaToolPath, | ||
$"package -c Release --framework {framework} --function-architecture {FunctionArchitecture}", | ||
workingDirectory); | ||
} | ||
|
||
public static void CleanUp(string toolPath) | ||
{ | ||
if (!string.IsNullOrEmpty(toolPath) && Directory.Exists(toolPath)) | ||
{ | ||
Directory.Delete(toolPath, true); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories"/> | ||
/// </summary> | ||
private static void CopyDirectory(DirectoryInfo dir, string destDirName) | ||
{ | ||
if (!dir.Exists) | ||
{ | ||
throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: {dir.FullName}"); | ||
} | ||
|
||
var dirs = dir.GetDirectories(); | ||
|
||
Directory.CreateDirectory(destDirName); | ||
|
||
var files = dir.GetFiles(); | ||
foreach (var file in files) | ||
{ | ||
var tempPath = Path.Combine(destDirName, file.Name); | ||
file.CopyTo(tempPath, false); | ||
} | ||
|
||
foreach (var subdir in dirs) | ||
{ | ||
var tempPath = Path.Combine(destDirName, subdir.Name); | ||
var subDir = new DirectoryInfo(subdir.FullName); | ||
CopyDirectory(subDir, tempPath); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...eSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/IntegrationTestCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Xunit; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests; | ||
|
||
[CollectionDefinition("Integration Tests")] | ||
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture> | ||
{ | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
...timeSupport.Tests/Amazon.Lambda.RuntimeSupport.IntegrationTests/IntegrationTestFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Amazon.Lambda.RuntimeSupport.IntegrationTests.Helpers; | ||
using Xunit; | ||
|
||
namespace Amazon.Lambda.RuntimeSupport.IntegrationTests; | ||
|
||
public class IntegrationTestFixture : IAsyncLifetime | ||
{ | ||
private readonly List<string> _tempPaths = new(); | ||
|
||
public async Task InitializeAsync() | ||
{ | ||
var testAppPath = LambdaToolsHelper.GetTempTestAppDirectory( | ||
"../../../../../../..", | ||
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeFunctionTest"); | ||
var toolPath = await LambdaToolsHelper.InstallLambdaTools(); | ||
_tempPaths.AddRange([testAppPath, toolPath] ); | ||
await LambdaToolsHelper.LambdaPackage(toolPath, "net6.0", testAppPath); | ||
await LambdaToolsHelper.LambdaPackage(toolPath, "net8.0", testAppPath); | ||
|
||
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory( | ||
"../../../../../../..", | ||
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiTest"); | ||
toolPath = await LambdaToolsHelper.InstallLambdaTools(); | ||
_tempPaths.AddRange([testAppPath, toolPath] ); | ||
await LambdaToolsHelper.LambdaPackage(toolPath, "net6.0", testAppPath); | ||
|
||
testAppPath = LambdaToolsHelper.GetTempTestAppDirectory( | ||
"../../../../../../..", | ||
"Libraries/test/Amazon.Lambda.RuntimeSupport.Tests/CustomRuntimeAspNetCoreMinimalApiCustomSerializerTest"); | ||
toolPath = await LambdaToolsHelper.InstallLambdaTools(); | ||
_tempPaths.AddRange([testAppPath, toolPath] ); | ||
await LambdaToolsHelper.LambdaPackage(toolPath, "net6.0", testAppPath); | ||
} | ||
|
||
public Task DisposeAsync() | ||
{ | ||
foreach (var tempPath in _tempPaths) | ||
{ | ||
LambdaToolsHelper.CleanUp(tempPath); | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters