diff --git a/README.md b/README.md index d384f2c33..d96ae1e5f 100644 --- a/README.md +++ b/README.md @@ -495,8 +495,9 @@ public class Foo To apply the benefits of the new [JSON source generator](https://devblogs.microsoft.com/dotnet/try-the-new-system-text-json-source-generator/) for System.Text.Json added in .NET 6, you can use `SystemTextJsonContentSerializer` with a custom instance of `RefitSettings` and `JsonSerializerOptions`: ```csharp -var options = new JsonSerializerOptions(); -options.AddContext(); +var options = new JsonSerializerOptions() { + TypeInfoResolver = MyJsonSerializerContext.Default +}; var gitHubApi = RestService.For("https://api.github.com", new RefitSettings { diff --git a/Refit.Benchmarks/Program.cs b/Refit.Benchmarks/Program.cs index 55f81ef15..02129c15f 100644 --- a/Refit.Benchmarks/Program.cs +++ b/Refit.Benchmarks/Program.cs @@ -10,4 +10,5 @@ BenchmarkRunner.Run(); // BenchmarkRunner.Run(); // BenchmarkRunner.Run(); + // BenchmarkRunner.Run(); } diff --git a/Refit.Benchmarks/Refit.Benchmarks.csproj b/Refit.Benchmarks/Refit.Benchmarks.csproj index 357353e74..5bf18d2c8 100644 --- a/Refit.Benchmarks/Refit.Benchmarks.csproj +++ b/Refit.Benchmarks/Refit.Benchmarks.csproj @@ -20,6 +20,7 @@ + diff --git a/Refit.Benchmarks/SourceGeneratorBenchmark.cs b/Refit.Benchmarks/SourceGeneratorBenchmark.cs new file mode 100644 index 000000000..d5b93ce66 --- /dev/null +++ b/Refit.Benchmarks/SourceGeneratorBenchmark.cs @@ -0,0 +1,121 @@ +using System.Reflection; + +using BenchmarkDotNet.Attributes; + +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; + +using Refit.Generator; + +namespace Refit.Benchmarks; + +[MemoryDiagnoser] +public class SourceGeneratorBenchmark +{ + #region SourceText + private const string SmallInterface = + """ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using Refit; + + namespace RefitGeneratorTest; + + public interface IReallyExcitingCrudApi where T : class + { + [Post("")] + Task Create([Body] T payload); + + [Get("")] + Task> ReadAll(); + + [Get("/{key}")] + Task ReadOne(TKey key); + + [Put("/{key}")] + Task Update(TKey key, [Body]T payload); + + [Delete("/{key}")] + Task Delete(TKey key); + } + """; + #endregion + + static readonly MetadataReference RefitAssembly = MetadataReference.CreateFromFile( + typeof(GetAttribute).Assembly.Location, + documentation: XmlDocumentationProvider.CreateFromFile( + Path.ChangeExtension(typeof(GetAttribute).Assembly.Location, ".xml") + ) + ); + static readonly Type[] ImportantAssemblies = { + typeof(Binder), + typeof(GetAttribute), + typeof(Enumerable), + typeof(Newtonsoft.Json.JsonConvert), + typeof(HttpContent), + typeof(Attribute) + }; + + static Assembly[] AssemblyReferencesForCodegen => + AppDomain.CurrentDomain + .GetAssemblies() + .Concat(ImportantAssemblies.Select(x=>x.Assembly)) + .Distinct() + .Where(a => !a.IsDynamic) + .ToArray(); + + private Compilation compilation; + private CSharpGeneratorDriver driver; + + private void Setup(string sourceText) + { + var references = new List(); + var assemblies = AssemblyReferencesForCodegen; + foreach (var assembly in assemblies) + { + if (!assembly.IsDynamic) + { + references.Add(MetadataReference.CreateFromFile(assembly.Location)); + } + } + + references.Add(RefitAssembly); + compilation = CSharpCompilation.Create( + "compilation", + [CSharpSyntaxTree.ParseText(sourceText)], + references, + new CSharpCompilationOptions(OutputKind.ConsoleApplication) + ); + + var generator = new InterfaceStubGeneratorV2().AsSourceGenerator(); + driver = CSharpGeneratorDriver.Create(generator); + } + + [GlobalSetup(Target = nameof(Compile))] + public void SetupSmall() => Setup(SmallInterface); + + [Benchmark] + public GeneratorDriver Compile() + { + return driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _); + } + + [GlobalSetup(Target = nameof(Cached))] + public void SetupCached() + { + Setup(SmallInterface); + driver = (CSharpGeneratorDriver)driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _); + compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText("struct MyValue {}")); + } + + [Benchmark] + public GeneratorDriver Cached() + { + return driver.RunGeneratorsAndUpdateCompilation(compilation, out _, out _); + } +} diff --git a/Refit.GeneratorTests/Refit.GeneratorTests.csproj b/Refit.GeneratorTests/Refit.GeneratorTests.csproj index 74506732f..76d4f1e27 100644 --- a/Refit.GeneratorTests/Refit.GeneratorTests.csproj +++ b/Refit.GeneratorTests/Refit.GeneratorTests.csproj @@ -14,7 +14,7 @@ - + diff --git a/Refit.Tests/Refit.Tests.csproj b/Refit.Tests/Refit.Tests.csproj index 1761bed83..be87476f6 100644 --- a/Refit.Tests/Refit.Tests.csproj +++ b/Refit.Tests/Refit.Tests.csproj @@ -20,7 +20,7 @@ - +