-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add performance tests for format method * feat: add two performance test for most used methods * feat: remove nullable warnings for performance tests project
- Loading branch information
Showing
7 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
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,21 @@ | ||
name: run_performance_tests_windows | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
run_performance_tests_windows: | ||
runs-on: windows-latest | ||
timeout-minutes: 20 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: 8.x | ||
- name: Run performance tests | ||
run: dotnet run -c Release --framework net8.0 | ||
working-directory: ./csharp/PhoneNumbers.PerformanceTest |
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
37 changes: 37 additions & 0 deletions
37
csharp/PhoneNumbers.PerformanceTest/Benchmarks/PhoneNumberFormatBenchmark.cs
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,37 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
|
||
namespace PhoneNumbers.PerformanceTest.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net60)] | ||
[SimpleJob(RuntimeMoniker.Net70)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class PhoneNumberFormatBenchmark | ||
{ | ||
#if NETFRAMEWORK | ||
private PhoneNumberUtil _phoneNumberUtil = null; | ||
#else | ||
private PhoneNumberUtil _phoneNumberUtil = null!; | ||
#endif | ||
|
||
#if NETFRAMEWORK | ||
private PhoneNumber _phoneNumber; | ||
#else | ||
private PhoneNumber _phoneNumber = null!; | ||
#endif | ||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_phoneNumberUtil = PhoneNumberUtil.GetInstance(); | ||
_phoneNumber = _phoneNumberUtil.Parse("+14156667777", "US"); | ||
} | ||
|
||
[Benchmark] | ||
public void FormatPhoneNumber() | ||
{ | ||
_phoneNumberUtil.Format(_phoneNumber, PhoneNumberFormat.INTERNATIONAL); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
csharp/PhoneNumbers.PerformanceTest/Benchmarks/PhoneNumberParseBenchmark.cs
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,31 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using BenchmarkDotNet.Jobs; | ||
|
||
namespace PhoneNumbers.PerformanceTest.Benchmarks | ||
{ | ||
[MemoryDiagnoser] | ||
[SimpleJob(RuntimeMoniker.Net48)] | ||
[SimpleJob(RuntimeMoniker.Net60)] | ||
[SimpleJob(RuntimeMoniker.Net70)] | ||
[SimpleJob(RuntimeMoniker.Net80)] | ||
public class PhoneNumberParseBenchmark | ||
{ | ||
#if NETFRAMEWORK | ||
private PhoneNumberUtil _phoneNumberUtil = null; | ||
#else | ||
private PhoneNumberUtil _phoneNumberUtil = null!; | ||
#endif | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_phoneNumberUtil = PhoneNumberUtil.GetInstance(); | ||
} | ||
|
||
[Benchmark] | ||
public void ParsePhoneNumber() | ||
{ | ||
_phoneNumberUtil.Parse("+14156667777", "US"); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
csharp/PhoneNumbers.PerformanceTest/PhoneNumbers.PerformanceTest.csproj
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,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFrameworks>netframework4.8;net6.0;net7.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>disable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net8.0'"> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' "> | ||
<PackageReference Include="System.Memory" Version="4.5.4" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\PhoneNumbers\PhoneNumbers.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,15 @@ | ||
using BenchmarkDotNet.Running; | ||
using PhoneNumbers.PerformanceTest.Benchmarks; | ||
|
||
namespace PhoneNumbers.PerformanceTest | ||
{ | ||
public static class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
BenchmarkRunner.Run<PhoneNumberFormatBenchmark>(); | ||
|
||
BenchmarkRunner.Run<PhoneNumberParseBenchmark>(); | ||
} | ||
} | ||
} |
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,13 @@ | ||
## Performance testing history | ||
|
||
See [Github Actions](https://github.com/twcclegg/libphonenumber-csharp/actions/workflows/run_performance_tests_windows.yml) for a history of previous runs, in the logs, you can see the performance results for each method being tested | ||
|
||
Below you can see a sample of what the results might look like | ||
|
||
| Method | Job | Runtime | Mean | Error | StdDev | Gen0 | Allocated | | ||
|------------------ |------------------- |------------------- |---------:|----------:|----------:|-------:|----------:| | ||
| FormatPhoneNumber | .NET 6.0 | .NET 6.0 | 1.234 us | 0.0158 us | 0.0124 us | 0.0076 | 152 B | | ||
| FormatPhoneNumber | .NET 7.0 | .NET 7.0 | 1.307 us | 0.0015 us | 0.0013 us | 0.0076 | 152 B | | ||
| FormatPhoneNumber | .NET 8.0 | .NET 8.0 | 1.141 us | 0.0080 us | 0.0071 us | 0.0076 | 152 B | | ||
| FormatPhoneNumber | .NET Framework 4.8 | .NET Framework 4.8 | 2.746 us | 0.0114 us | 0.0101 us | 0.1335 | 851 B | | ||
|