Skip to content

Commit

Permalink
Refine perf and api usage around file creation and code writer (#3519)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash authored Jun 11, 2024
1 parent 295e68a commit 72c6ada
Show file tree
Hide file tree
Showing 65 changed files with 4,120 additions and 906 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Microsoft.Generator.CSharp.ClientModel.Snippets;
Expand Down Expand Up @@ -51,6 +52,8 @@ public MrwSerializationTypeProvider(ModelProvider model, InputModelType inputMod
Namespace = model.Namespace;
}

protected override string GetFileName() => Path.Combine("src", "Generated", "Models", $"{Name}.serialization.cs");

protected override TypeSignatureModifiers GetDeclarationModifiers() => _model.DeclarationModifiers;

public override string Name { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Generator.CSharp.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Generator.CSharp.Input.Tests.Perf", "Microsoft.Generator.CSharp.Input\perf\Microsoft.Generator.CSharp.Input.Tests.Perf.csproj", "{DF2E7916-D0C4-47CA-876A-5B78A3A3FD77}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Generator.CSharp.Tests.Perf", "Microsoft.Generator.CSharp\perf\Microsoft.Generator.CSharp.Tests.Perf.csproj", "{8AE5D726-610C-4A72-B2A9-5C53C423C485}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -68,6 +70,10 @@ Global
{DF2E7916-D0C4-47CA-876A-5B78A3A3FD77}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF2E7916-D0C4-47CA-876A-5B78A3A3FD77}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF2E7916-D0C4-47CA-876A-5B78A3A3FD77}.Release|Any CPU.Build.0 = Release|Any CPU
{8AE5D726-610C-4A72-B2A9-5C53C423C485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8AE5D726-610C-4A72-B2A9-5C53C423C485}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8AE5D726-610C-4A72-B2A9-5C53C423C485}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8AE5D726-610C-4A72-B2A9-5C53C423C485}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.IO;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Microsoft.Generator.CSharp.Input;
using Microsoft.Generator.CSharp.Providers;

namespace Microsoft.Generator.CSharp.Perf
{
public class CodeWriterBenchmark
{
private TypeProviderWriter _writer;

public CodeWriterBenchmark()
{
PluginHandler pluginHandler = new PluginHandler();
pluginHandler.LoadPlugin(Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location)!.FullName, "Projects", "Model"));

var properties = new[]
{
new InputModelProperty("MyProperty", "myProperty", "The property of mine", new InputPrimitiveType(InputPrimitiveTypeKind.Int32, false), true, false, false)
};
var inputModel = new InputModelType("MyModel", null, null, null, "Test model", InputModelTypeUsage.RoundTrip, properties, null, Array.Empty<InputModelType>(), null, null, null, false);
var modelProvider = new ModelProvider(inputModel);
_writer = new TypeProviderWriter(modelProvider);
}

[Benchmark]
public void WriteModel()
{
_writer.Write();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Microsoft.Generator.CSharp.ClientModel\src\Microsoft.Generator.CSharp.ClientModel.csproj" />
<ProjectReference Include="..\src\Microsoft.Generator.CSharp.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Projects\Model\Configuration.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Projects\Model\tspCodeModel.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using Perfolizer.Horology;

namespace Microsoft.Generator.CSharp.Perf;

public class Program
{
public static void Main(string[] args)
{
// To see the list of benchmarks that can be run
// dotnet run -c Release --framework net8.0 --list flat

// To run a specific benchmark class
// dotnet run -c Release --framework net8.0 --filter System.ClientModel.Tests.Internal.Perf.ResourceProviderDataModel*

// To run a specific benchmark method
// dotnet run -c Release --framework net8.0 --filter *ResourceProviderDataModel.Read_PublicInterface
// or
// dotnet run -c Release --framework net8.0 --filter System.ClientModel.Tests.Internal.Perf.ResourceProviderDataModel.Read_PublicInterface

// To run a specific benchmark class and category
// dotnet run -c Release --framework net8.0 --anyCategories PublicInterface --filter System.ClientModel.Tests.Internal.Perf.ResourceProviderDataModel*

var config = ManualConfig.Create(DefaultConfig.Instance);
config.Options = ConfigOptions.JoinSummary | ConfigOptions.StopOnFirstError;
config = config.AddDiagnoser(MemoryDiagnoser.Default);
config.AddJob(Job.Default
.WithWarmupCount(1)
.WithIterationTime(TimeInterval.FromMilliseconds(250))
.WithMinIterationCount(15)
.WithMaxIterationCount(20)
.AsDefault());
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"output-folder": ".",
"namespace": "UnbrandedTypeSpec",
"library-name": "UnbrandedTypeSpec",
"use-model-reader-writer": true
}
Loading

0 comments on commit 72c6ada

Please sign in to comment.