-
Notifications
You must be signed in to change notification settings - Fork 564
[WIP] idea to speed up Xamarin.Android.Build.Tests #1008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jonathanpeppers
wants to merge
8
commits into
dotnet:master
from
jonathanpeppers:wip-test-performance
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
420d249
[tests] started switching over Builder logging logic
jonathanpeppers c853d62
[tests] a few tests working now with assertion changes
jonathanpeppers da96670
[tests] hack in TearDown for Windows
jonathanpeppers 86705c7
[tests] a couple more fixed
jonathanpeppers d04ac6a
[tests] fixed DesignTimeBuild tests
jonathanpeppers 5bb23b9
[tests] fixed two more
jonathanpeppers 7be483f
[tests] fixed one more
jonathanpeppers 67fede0
[tests] fix TargetGenerateJavaDesignerForComponentIsSkipped
jonathanpeppers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 68 additions & 60 deletions
128
...marin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/AndroidUpdateResourcesTest.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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
41 changes: 41 additions & 0 deletions
41
src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Common/Assertion.cs
This file contains hidden or 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,41 @@ | ||
| using System; | ||
| using System.Linq.Expressions; | ||
|
|
||
| namespace Xamarin.ProjectTools | ||
| { | ||
| /// <summary> | ||
| /// Represents an "assertion" that a condition is true as the build output streams through a callback | ||
| /// </summary> | ||
| public class Assertion | ||
| { | ||
| readonly Expression<Func<string, bool>> expression; | ||
| Func<string, bool> func; | ||
|
|
||
| public bool Passed { get; private set; } | ||
|
|
||
| public string Message { get; private set; } | ||
|
|
||
| public Assertion (Expression<Func<string, bool>> expression, string message = null) | ||
| { | ||
| this.expression = expression; | ||
| Message = message; | ||
| } | ||
|
|
||
| public void Assert(string line) | ||
| { | ||
| if (!Passed) { | ||
| if (func == null) | ||
| func = expression.Compile (); | ||
| Passed = func (line); | ||
| } | ||
| } | ||
|
|
||
| public override string ToString () | ||
| { | ||
| if (!string.IsNullOrEmpty (Message)) | ||
| return Message; | ||
|
|
||
| return $"Expression was false: {expression.Body}"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,11 +14,60 @@ public class Builder : IDisposable | |
| public bool IsUnix { get; set; } | ||
| public bool RunningMSBuild { get; set; } | ||
| public LoggerVerbosity Verbosity { get; set; } | ||
| public string LastBuildOutput { get; set; } | ||
| public TimeSpan LastBuildTime { get; protected set; } | ||
| public string BuildLogFile { get; set; } | ||
| public bool ThrowOnBuildFailure { get; set; } | ||
|
|
||
| //TODO: remove | ||
| public string LastBuildOutput { | ||
| get { throw new NotImplementedException (); } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR leaves a couple methods that would be removed if we moved forward with this. I left them here with |
||
| } | ||
|
|
||
| public List<Assertion> Assertions { get; set; } = new List<Assertion> (); | ||
|
|
||
| public void AssertTargetSkipped (string target) | ||
| { | ||
| Assertions.Add (new Assertion (o => o.Contains (target), $"Target '{target}' is not even in the build output.")); | ||
|
|
||
| Assertions.Add (new Assertion (o => o.Contains ($"Target {target} skipped due to ") || | ||
| o.Contains ($"Skipping target \"{target}\" because it has no outputs.") || | ||
| o.Contains ($"Target \"{target}\" skipped, due to") || | ||
| o.Contains ($"Skipping target \"{target}\" because its outputs are up-to-date") || | ||
| o.Contains ($"target {target}, skipping") || | ||
| o.Contains ($"Skipping target \"{target}\" because all output files are up-to-date"), $"Target {target} was not skipped.")); | ||
| } | ||
|
|
||
| public void AssertApkInstalled () | ||
| { | ||
| Assertions.Add (new Assertion (o => o.Contains (" pm install "))); | ||
| } | ||
|
|
||
| public void AssertAllTargetsSkipped (params string [] targets) | ||
| { | ||
| foreach (var t in targets) { | ||
| AssertTargetSkipped (t); | ||
| } | ||
| } | ||
|
|
||
| public void AssertTargetIsBuilt (string target) | ||
| { | ||
| Assertions.Add (new Assertion (o => o.Contains (target), $"Target '{target}' is not even in the build output.")); | ||
|
|
||
| Assertions.Add (new Assertion (o => !o.Contains ($"Target {target} skipped due to ") && | ||
| !o.Contains ($"Skipping target \"{target}\" because it has no outputs.") && | ||
| !o.Contains ($"Target \"{target}\" skipped, due to") && | ||
| !o.Contains ($"Skipping target \"{target}\" because its outputs are up-to-date") && | ||
| !o.Contains ($"target {target}, skipping") && | ||
| !o.Contains ($"Skipping target \"{target}\" because all output files are up-to-date"), $"Target {target} was not built.")); | ||
| } | ||
|
|
||
| public void AssertAllTargetsBuilt (params string [] targets) | ||
| { | ||
| foreach (var t in targets) { | ||
| AssertTargetIsBuilt (t); | ||
| } | ||
| } | ||
|
|
||
| string GetVisualStudio2017Directory () | ||
| { | ||
| var editions = new [] { | ||
|
|
@@ -187,11 +236,6 @@ protected bool BuildInternal (string projectOrSolution, string target, string [] | |
| ? Path.GetFullPath (Path.Combine (Root, Path.GetDirectoryName (projectOrSolution), BuildLogFile)) | ||
| : null; | ||
|
|
||
| var logger = buildLogFullPath == null | ||
| ? string.Empty | ||
| : string.Format ("/noconsolelogger \"/flp1:LogFile={0};Encoding=UTF-8;Verbosity={1}\"", | ||
| buildLogFullPath, Verbosity.ToString ().ToLower ()); | ||
|
|
||
| var start = DateTime.UtcNow; | ||
| var homeDirectory = Environment.GetFolderPath (Environment.SpecialFolder.Personal); | ||
| var androidSdkToolPath = Path.Combine (homeDirectory, "android-toolchain"); | ||
|
|
@@ -208,8 +252,8 @@ protected bool BuildInternal (string projectOrSolution, string target, string [] | |
|
|
||
| var args = new StringBuilder (); | ||
| var psi = new ProcessStartInfo (XABuildExe); | ||
| args.AppendFormat ("{0} /t:{1} {2}", | ||
| QuoteFileName(Path.Combine (Root, projectOrSolution)), target, logger); | ||
| args.AppendFormat ("{0} /t:{1} /v:{2}", | ||
| QuoteFileName(Path.Combine (Root, projectOrSolution)), target, Verbosity); | ||
| if (RunningMSBuild) | ||
| args.Append (" /p:BuildingOutOfProcess=true"); | ||
| else | ||
|
|
@@ -244,57 +288,75 @@ protected bool BuildInternal (string projectOrSolution, string target, string [] | |
|
|
||
| bool nativeCrashDetected = false; | ||
| bool result = false; | ||
| bool lastBuildTimeSet = false; | ||
| int attempts = 1; | ||
| for (int attempt = 0; attempt < attempts; attempt++) { | ||
| var p = Process.Start (psi); | ||
| var ranToCompletion = p.WaitForExit ((int)new TimeSpan (0, 10, 0).TotalMilliseconds); | ||
| result = ranToCompletion && p.ExitCode == 0; | ||
|
|
||
| LastBuildTime = DateTime.UtcNow - start; | ||
|
|
||
| var sb = new StringBuilder (); | ||
| sb.AppendLine (psi.FileName + " " + args.ToString () + Environment.NewLine); | ||
| if (!ranToCompletion) | ||
| sb.AppendLine ("Build Timed Out!"); | ||
| if (buildLogFullPath != null && File.Exists (buildLogFullPath)) { | ||
| using (var fs = File.OpenRead (buildLogFullPath)) { | ||
| using (var sr = new StreamReader (fs, Encoding.UTF8, true, 65536)) { | ||
| string line; | ||
| while ((line = sr.ReadLine ()) != null) { | ||
| sb.AppendLine (line); | ||
| if (line.StartsWith ("Time Elapsed", StringComparison.OrdinalIgnoreCase)) { | ||
| var match = timeElapsedRegEx.Match (line); | ||
| if (match.Success) { | ||
| LastBuildTime = TimeSpan.Parse (match.Groups ["TimeSpan"].Value); | ||
| Console.WriteLine ($"Found Time Elapsed {LastBuildTime}"); | ||
| } | ||
| } | ||
| if (line.StartsWith ("Got a SIGSEGV while executing native code", StringComparison.OrdinalIgnoreCase)) { | ||
| nativeCrashDetected = true; | ||
| break; | ||
| using (var p = new Process { StartInfo = psi }) { | ||
| StreamWriter file = null; | ||
| if (buildLogFullPath != null) { | ||
| Directory.CreateDirectory (Path.GetDirectoryName (buildLogFullPath)); | ||
| file = File.CreateText (buildLogFullPath); | ||
| } | ||
| try { | ||
| var standardError = new StringBuilder (); | ||
| file?.WriteLine ("#stdout begin"); | ||
|
|
||
| p.OutputDataReceived += (sender, e) => { | ||
| if (e.Data == null) | ||
| return; | ||
| if (e.Data.StartsWith ("Time Elapsed", StringComparison.OrdinalIgnoreCase)) { | ||
| var match = timeElapsedRegEx.Match (e.Data); | ||
| if (match.Success) { | ||
| LastBuildTime = TimeSpan.Parse (match.Groups ["TimeSpan"].Value); | ||
| lastBuildTimeSet = true; | ||
| Console.WriteLine ($"Found Time Elapsed {LastBuildTime}"); | ||
| } | ||
| } | ||
|
|
||
| if (e.Data.StartsWith ("Got a SIGSEGV while executing native code", StringComparison.OrdinalIgnoreCase)) { | ||
| nativeCrashDetected = true; | ||
| } | ||
|
|
||
| foreach (var assertion in Assertions) { | ||
| assertion.Assert (e.Data); | ||
| } | ||
|
|
||
| file?.WriteLine (e.Data); | ||
| }; | ||
| p.ErrorDataReceived += (sender, e) => standardError.AppendLine (e.Data); | ||
|
|
||
| p.Start (); | ||
| p.BeginOutputReadLine (); | ||
| p.BeginErrorReadLine (); | ||
|
|
||
| var ranToCompletion = p.WaitForExit ((int)new TimeSpan (0, 10, 0).TotalMilliseconds); | ||
| if (nativeCrashDetected) { | ||
| Console.WriteLine ($"Native crash detected! Running the build for {projectOrSolution} again."); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
| sb.AppendFormat ("\n#stdout begin\n{0}\n#stdout end\n", p.StandardOutput.ReadToEnd ()); | ||
| sb.AppendFormat ("\n#stderr begin\n{0}\n#stderr end\n", p.StandardError.ReadToEnd ()); | ||
| result = ranToCompletion && p.ExitCode == 0; | ||
| if (!lastBuildTimeSet) | ||
| LastBuildTime = DateTime.UtcNow - start; | ||
|
|
||
| LastBuildOutput = sb.ToString (); | ||
| if (nativeCrashDetected) { | ||
| Console.WriteLine ($"Native crash detected! Running the build for {projectOrSolution} again."); | ||
| continue; | ||
| if (file != null) { | ||
| file.WriteLine (); | ||
| file.WriteLine ("#stdout end"); | ||
| file.WriteLine (); | ||
| file.WriteLine ("#stderr begin"); | ||
| file.WriteLine (standardError.ToString ()); | ||
| file.WriteLine ("#stderr end"); | ||
| } | ||
| } finally { | ||
| file?.Dispose (); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| if (buildLogFullPath != null) { | ||
| Directory.CreateDirectory (Path.GetDirectoryName (buildLogFullPath)); | ||
| File.WriteAllText (buildLogFullPath, LastBuildOutput); | ||
| } | ||
| if (!result && ThrowOnBuildFailure) { | ||
| string message = "Build failure: " + Path.GetFileName (projectOrSolution) + (BuildLogFile != null && File.Exists (buildLogFullPath) ? "Build log recorded at " + buildLogFullPath : null); | ||
| throw new FailedBuildException (message, null, LastBuildOutput); | ||
|
|
||
| //TODO: do we really need the full build log here? It seems to lock up my VS test runner | ||
| throw new FailedBuildException (message, null); | ||
| } | ||
|
|
||
| return result; | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore this, this is something else to figure out about these tests on Windows.