-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use System.Text.Json instead of Newton.Json (#525)
* Use System.Text.Json instead of Newtonsoft.Json. Approximately 2x faster result output serialization with significantly reduced memory usage (for JSON format). * Adds simple benchmark for json writer. --------- Co-authored-by: Gabe Stocco <[email protected]>
- Loading branch information
Showing
40 changed files
with
414 additions
and
250 deletions.
There are no files selected for viewing
File renamed without changes.
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
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
using DotLiquid; | ||
using Microsoft.ApplicationInspector.CLI; | ||
using Microsoft.ApplicationInspector.Commands; | ||
using Microsoft.ApplicationInspector.RulesEngine; | ||
|
||
namespace ApplicationInspector.Benchmarks; | ||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net70)] | ||
public class WriterBench | ||
{ | ||
[Params(1000, 10000)] | ||
public int N; | ||
|
||
// Holds the result object which will be serialized | ||
private AnalyzeResult _result; | ||
|
||
[GlobalSetup] | ||
public void GlobalSetup() | ||
{ | ||
var _exerpt = "Hello World"; | ||
var helper = new MetaDataHelper(".."); | ||
var matchRecord = new MatchRecord("rule-id", "rule-name") | ||
{ | ||
Boundary = new Boundary() { Index = 0, Length = 1 }, | ||
EndLocationColumn = 0, | ||
EndLocationLine = 1, | ||
Excerpt = _exerpt, | ||
FileName = "TestFile", | ||
LanguageInfo = new LanguageInfo(), | ||
Tags = new []{"TestTag"} | ||
}; | ||
for (int i = 0; i < N; i++) | ||
{ | ||
helper.AddMatchRecord(matchRecord); | ||
} | ||
helper.PrepareReport(); | ||
|
||
_result = new AnalyzeResult() { Metadata = helper.Metadata, ResultCode = 0 }; | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public void ExportRecordsToJson() | ||
{ | ||
var tmpPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
CLIAnalyzeCmdOptions analyzeOpts = new CLIAnalyzeCmdOptions() | ||
{ | ||
OutputFileFormat = "json", | ||
OutputFilePath = tmpPath | ||
}; | ||
var writerFactory = new WriterFactory(); | ||
var writer = writerFactory.GetWriter(analyzeOpts); | ||
writer.WriteResults(_result,analyzeOpts); | ||
File.Delete(tmpPath); | ||
} | ||
|
||
public static string GetExecutingDirectoryName() | ||
{ | ||
if (Assembly.GetEntryAssembly()?.GetName().CodeBase is string codeBaseLoc) | ||
{ | ||
var location = new Uri(codeBaseLoc); | ||
return new FileInfo(location.AbsolutePath).Directory?.FullName ?? string.Empty; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} |
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
Oops, something went wrong.