8000 Speed up execution by offloading logging by danmoseley · Pull Request #75 · dotnet/api-docs-sync · GitHub
[go: up one dir, main page]

Skip to content

Speed up execution by offloading logging #75

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

Merged
merged 9 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 74 additions & 24 deletions Libraries/Log.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,97 @@
#nullable enable
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace Libraries
{
internal class Log
public static class Log
{
private static void WriteLine(string format, params object[]? args)
private static Channel<(ConsoleColor, string, object[]?)> channel = Channel.CreateUnbounded<(ConsoleColor, string, object[]?)>();

public static async Task StartAsync()
{
if (args == null || args.Length == 0)
{
Console.WriteLine(format);
}
else
using FileStream fs = new(Path.GetTempFileName(), FileMode.Open);
using StreamWriter sw = new(fs);

ConsoleColor initialForeground = Console.ForegroundColor;
ConsoleColor foreground = initialForeground; // cheaper than reading it each time

StringBuilder combined = new(65_536);

bool unwrittenBlob = false;
(ConsoleColor color, string msg, object[]? args) blob = new((ConsoleColor)(-1), "", null); // compiler can't figure out we won't use this

Stopwatch stopwatch = Stopwatch.StartNew();

while (await channel.Reader.WaitToReadAsync())
{
Console.WriteLine(format, args);
while (unwrittenBlob || await channel.Reader.WaitToReadAsync())
{
if (unwrittenBlob && foreground != blob.color)
{
Console.ForegroundColor = blob.color;
foreground = blob.color;
}

if (!unwrittenBlob)
{
blob = await channel.Reader.ReadAsync();

if (blob.color != (ConsoleColor)(-1) && foreground != blob.color)
{
unwrittenBlob = true; // New color - emit what we have
break;
}
}

if (blob.args == null)
{
combined.Append(blob.msg);
}
else
{
combined.AppendFormat(blob.msg, blob.args);
}

unwrittenBlob = false;

if (stopwatch.ElapsedMilliseconds > 1000)
break;
}

stopwatch.Restart();

Console.Write(combined);
sw.Write(combined);

combined = combined.Length < 65_536 ? combined.Clear() : new StringBuilder();
}

if (foreground != initialForeground)
Console.ForegroundColor = initialForeground;

Console.WriteLine("Written log to {0}", fs.Name);
}

private static void Write(string format, params object[]? args)
public static void Finished()
{
if (args == null || args.Length == 0)
{
Console.Write(format);
}
else
{
Console.Write(format, args);
}
channel.Writer.Complete();
}

public static void Print(bool endline, ConsoleColor foregroundColor, string format, params object[]? args)
{
ConsoleColor initialColor = Console.ForegroundColor;
Console.ForegroundColor = foregroundColor;
if (endline)
{
WriteLine(format, args);
channel.Writer.WriteAsync((foregroundColor, format + Environment.NewLine, args));
}
else
{
Write(format, args);
channel.Writer.WriteAsync((foregroundColor, format, args));
}
Console.ForegroundColor = initialColor;
}

public static void Info(string format)
Expand Down Expand Up @@ -174,7 +224,7 @@ public static void Assert(bool endline, bool condition, string format, params ob

public static void Line()
{
Console.WriteLine();
Print(endline: true, (ConsoleColor)(-1), "", null);
}

public delegate void PrintHelpFunction();
Expand Down Expand Up @@ -449,4 +499,4 @@ the interface API.
");
}
}
}
}
6 changes: 2 additions & 4 deletions Libraries/ToTripleSlashPorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,8 @@ private void AddSymbolLocationsToResolvedLocations(ProjectInformation projectInf
Log.Info(false, $"Symbol '{symbol.Name}' not found in locations of project '{path}'.");
if (n < symbol.Locations.Count())
{
Log.Info(false, " Trying the next location...");
Log.Info(true, " Trying the next location...");
}
Console.WriteLine();
}
n++;
}
Expand Down Expand Up @@ -245,9 +244,8 @@ private void FindSymbolInReferencedProjects(DocsType docsType, IEnumerable<Proje
Log.Info(false, $"Symbol for '{docsType.FullName}' not found in referenced project '{projectPath}'.");
if (n < projectReferences.Count())
{
Log.Info(false, $" Trying the next project...");
Log.Info(true, $" Trying the next project...");
}
Console.WriteLine();
}
n++;
}
Expand Down
7 changes: 6 additions & 1 deletion Program/DocsPortingTool.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#nullable enable
using Libraries;
using System;
using System.Threading.Tasks;

namespace DocsPortingTool
{
class DocsPortingToo 6AAC l
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
Task loggingTask = Log.StartAsync();
Configuration config = Configuration.GetCLIArgumentsForDocsPortingTool(args);
switch (config.Direction)
{
Expand All @@ -25,6 +27,9 @@ public static void Main(string[] args)
default:
throw new ArgumentOutOfRangeException($"Unrecognized porting direction: {config.Direction}");
}

Log.Finished();
await loggingTask;
}
}
}
0