Skip to content
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

Minor performance improvements. #364

Merged
merged 4 commits into from
May 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions AppInspector/Commands/AnalyzeCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void ProcessAndAddToMetadata(FileEntry file)

fileRecord.ScanTime = sw.Elapsed;

_metaDataHelper?.Files.Enqueue(fileRecord);
_metaDataHelper?.Files.Add(fileRecord);
}
}

Expand Down Expand Up @@ -474,7 +474,7 @@ async Task ProcessAndAddToMetadata(FileEntry file, CancellationToken cancellatio

fileRecord.ScanTime = sw.Elapsed;

_metaDataHelper?.Files.Enqueue(fileRecord);
_metaDataHelper?.Files.Add(fileRecord);
}
}

Expand Down Expand Up @@ -723,7 +723,7 @@ void DoProcessing(IEnumerable<FileEntry> fileEntries)
// Populate skips for all the entries we didn't process
foreach (var entry in fileEntries.Where(x => !_metaDataHelper.Files.Any(y => x.FullPath == y.FileName)))
{
_metaDataHelper.Files.Enqueue(new FileRecord() { AccessTime = entry.AccessTime, CreateTime = entry.CreateTime, ModifyTime = entry.ModifyTime, FileName = entry.FullPath, Status = ScanState.TimeOutSkipped });
_metaDataHelper.Files.Add(new FileRecord() { AccessTime = entry.AccessTime, CreateTime = entry.CreateTime, ModifyTime = entry.ModifyTime, FileName = entry.FullPath, Status = ScanState.TimeOutSkipped });
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions AppInspector/Commands/GetTagsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ void ProcessAndAddToMetadata(FileEntry file)

fileRecord.ScanTime = sw.Elapsed;

_metaDataHelper?.Files.Enqueue(fileRecord);
_metaDataHelper?.Files.Add(fileRecord);
}
}

Expand Down Expand Up @@ -525,7 +525,7 @@ async Task ProcessAndAddToMetadata(FileEntry file, CancellationToken cancellatio

fileRecord.ScanTime = sw.Elapsed;

_metaDataHelper?.Files.Enqueue(fileRecord);
_metaDataHelper?.Files.Add(fileRecord);
}
}

Expand Down Expand Up @@ -710,7 +710,7 @@ void DoProcessing(IEnumerable<FileEntry> fileEntries)
// Populate skips for all the entries we didn't process
foreach (var entry in fileEntries.Where(x => !_metaDataHelper.Files.Any(y => x.FullPath == y.FileName)))
{
_metaDataHelper.Files.Enqueue(new FileRecord() { AccessTime = entry.AccessTime, CreateTime = entry.CreateTime, ModifyTime = entry.ModifyTime, FileName = entry.FullPath, Status = ScanState.TimeOutSkipped });
_metaDataHelper.Files.Add(new FileRecord() { AccessTime = entry.AccessTime, CreateTime = entry.CreateTime, ModifyTime = entry.ModifyTime, FileName = entry.FullPath, Status = ScanState.TimeOutSkipped });
}
}
}
Expand Down
94 changes: 35 additions & 59 deletions AppInspector/MetaDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class MetaDataHelper
private ConcurrentDictionary<string, MetricTagCounter> TagCounters { get; set; } = new ConcurrentDictionary<string, MetricTagCounter>();
private ConcurrentDictionary<string, int> Languages { get; set; } = new ConcurrentDictionary<string, int>();

internal ConcurrentQueue<MatchRecord> Matches { get; set; } = new ConcurrentQueue<MatchRecord>();
internal ConcurrentQueue<FileRecord> Files { get; set; } = new ConcurrentQueue<FileRecord>();
internal ConcurrentBag<MatchRecord> Matches { get; set; } = new ConcurrentBag<MatchRecord>();
internal ConcurrentBag<FileRecord> Files { get; set; } = new ConcurrentBag<FileRecord>();

public int UniqueTagsCount { get { return UniqueTags.Keys.Count; } }

Expand Down Expand Up @@ -118,7 +118,7 @@ public void AddTagsFromMatchRecord(MatchRecord matchRecord)
if (!CounterOnlyTagSet)
{
//update list of unique tags as we go
foreach (string tag in matchRecord.Tags ?? new string[] { })
foreach (string tag in matchRecord.Tags ?? Array.Empty<string>())
{
UniqueTags.TryAdd(tag, 0);
}
Expand All @@ -133,7 +133,7 @@ public void AddTagsFromMatchRecord(MatchRecord matchRecord)
public void AddMatchRecord(MatchRecord matchRecord)
{
//special handling for standard characteristics in report
foreach (var tag in matchRecord.Tags ?? new string[] { })
foreach (var tag in matchRecord.Tags ?? Array.Empty<string>())
{
switch (tag)
{
Expand Down Expand Up @@ -165,6 +165,7 @@ public void AddMatchRecord(MatchRecord matchRecord)
{
Tag = tag
});
TagCounters[tag].IncrementCount();
}
else if (tag.Contains(".Platform.OS"))
{
Expand All @@ -185,24 +186,18 @@ public void AddMatchRecord(MatchRecord matchRecord)
AppTypes.TryAdd(solutionType, 0);
}

bool CounterOnlyTagSet = false;
var selected = matchRecord.Tags is not null ? TagCounters.Where(x => matchRecord.Tags.Any(y => y.Contains(x.Value.Tag ?? ""))) : new Dictionary<string, MetricTagCounter>();
foreach (var select in selected)
{
CounterOnlyTagSet = true;
select.Value.IncrementCount();
}
var nonCounters = matchRecord.Tags?.Where(x => !TagCounters.Any(y => y.Key == x)) ?? Array.Empty<string>();

//omit adding if it is a counter metric tag
if (!CounterOnlyTagSet)
//omit adding if it if all the tags were counters
if (nonCounters.Any())
{
//update list of unique tags as we go
foreach (string tag in matchRecord.Tags ?? Array.Empty<string>())
foreach (string tag in nonCounters)
{
UniqueTags.TryAdd(tag, 0);
}

Matches.Enqueue(matchRecord);
Matches.Add(matchRecord);
}
}

Expand Down Expand Up @@ -262,22 +257,20 @@ public void AddLanguage(string language)
/// <returns></returns>
private string GetDefaultProjectName(string sourcePath)
{
string applicationName = "";
string applicationName = string.Empty;

if (Directory.Exists(sourcePath))
{
if (sourcePath[sourcePath.Length - 1] == Path.DirectorySeparatorChar) //in case path ends with dir separator; remove
{
applicationName = sourcePath.Substring(0, sourcePath.Length - 1);
}

try
if (sourcePath != string.Empty)
{
applicationName = applicationName.Substring(applicationName.LastIndexOf(Path.DirectorySeparatorChar)).Replace(Path.DirectorySeparatorChar, ' ').Trim();
}
catch (Exception)
{
applicationName = Path.GetFileNameWithoutExtension(sourcePath);
if (sourcePath[^1] == Path.DirectorySeparatorChar) //in case path ends with dir separator; remove
{
applicationName = sourcePath.Trim(Path.DirectorySeparatorChar);
}
if (applicationName.LastIndexOf(Path.DirectorySeparatorChar) is int idx && idx != -1)
{
applicationName = applicationName[idx..].Trim();
}
}
}
else
Expand Down Expand Up @@ -378,55 +371,38 @@ private string ExtractValue(string s)

private static string ExtractJSONValue(string s)
{
string result = "";
try
{
var parts = s.Split(':');
var value = parts[1];
value = value.Replace("\"", "");
result = value.Trim();
}
catch (Exception)
var parts = s.Split(':');
if (parts.Length == 2)
{
result = s;
return parts[1].Replace("\"", "").Trim();
}

return result;
return s;
}

private string ExtractXMLValue(string s)
{
string result = "";
try
int firstTag = s.IndexOf(">");
if (firstTag > -1 && firstTag < s.Length - 1)
{
int firstTag = s.IndexOf(">");
int endTag = s.IndexOf("</", firstTag);
var value = s.Substring(firstTag + 1, endTag - firstTag - 1);
result = value;
}
catch (Exception)
{
result = s;
if (endTag > -1)
{
return s[(firstTag + 1)..endTag];
}
}

return result;
return s;
}

private string ExtractXMLValueMultiLine(string s)
{
string result = "";
try
int firstTag = s.IndexOf(">");
if (firstTag > -1 && firstTag < s.Length - 1)
{
int firstTag = s.IndexOf(">");
var value = s.Substring(firstTag + 1);
result = value;
return s[(firstTag + 1)..];
}
catch (Exception)
{
result = s;
}

return result;
return s;
}
}

Expand Down
24 changes: 12 additions & 12 deletions Benchmarks/AnalyzeBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ namespace Benchmarks
public class AnalyzeBenchmark
{
// Manually put the file you want to benchmark. But don't put this in a path with "Test" in the name ;)
private const string path = "D:\\runtime-master.zip";
private const string path = "C:\\Users\\gstocco\\Downloads\\runtime-main\\runtime-main.zip\\runtime-main\\src\\coreclr\\binder";

public AnalyzeBenchmark()
{
}

[Benchmark(Baseline = true)]
public void AnalyzeSingleThreaded()
{
AnalyzeCommand command = new AnalyzeCommand(new AnalyzeOptions()
{
SourcePath = path,
SingleThread = true,
IgnoreDefaultRules = false
});
//[Benchmark(Baseline = true)]
//public void AnalyzeSingleThreaded()
//{
// AnalyzeCommand command = new AnalyzeCommand(new AnalyzeOptions()
// {
// SourcePath = path,
// SingleThread = true,
// IgnoreDefaultRules = false
// });

AnalyzeResult analyzeResult = command.GetResult();
}
// AnalyzeResult analyzeResult = command.GetResult();
//}

[Benchmark]
public void AnalyzeMultiThread()
Expand Down