Skip to content

Commit

Permalink
feat: add performance tests (#240)
Browse files Browse the repository at this point in the history
* 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
wmundev authored Apr 2, 2024
1 parent 1d147bf commit ff3e8e0
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/run_performance_tests_windows.yml
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
7 changes: 5 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
Expand Down Expand Up @@ -201,4 +201,7 @@ FakesAssemblies/

# Zipped Geocoding Data
geocoding.zip
testgeocoding.zip
testgeocoding.zip

# Performance tests artifacts
csharp/PhoneNumbers.PerformanceTest/BenchmarkDotNet.Artifacts/
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);
}
}
}
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");
}
}
}
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>
15 changes: 15 additions & 0 deletions csharp/PhoneNumbers.PerformanceTest/Program.cs
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>();
}
}
}
13 changes: 13 additions & 0 deletions csharp/PhoneNumbers.PerformanceTest/README.md
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 |

0 comments on commit ff3e8e0

Please sign in to comment.