Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add performance tests #240

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |