Skip to content

Commit ff3e8e0

Browse files
authored
feat: add performance tests (#240)
* feat: add performance tests for format method * feat: add two performance test for most used methods * feat: remove nullable warnings for performance tests project
1 parent 1d147bf commit ff3e8e0

File tree

7 files changed

+147
-2
lines changed

7 files changed

+147
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: run_performance_tests_windows
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
run_performance_tests_windows:
11+
runs-on: windows-latest
12+
timeout-minutes: 20
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: Setup .NET
16+
uses: actions/setup-dotnet@v4
17+
with:
18+
dotnet-version: 8.x
19+
- name: Run performance tests
20+
run: dotnet run -c Release --framework net8.0
21+
working-directory: ./csharp/PhoneNumbers.PerformanceTest

.gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ publish/
129129
# Publish Web Output
130130
*.[Pp]ublish.xml
131131
*.azurePubxml
132-
# TODO: Comment the next line if you want to checkin your web deploy settings
132+
# TODO: Comment the next line if you want to checkin your web deploy settings
133133
# but database connection strings (with potential passwords) will be unencrypted
134134
*.pubxml
135135
*.publishproj
@@ -201,4 +201,7 @@ FakesAssemblies/
201201

202202
# Zipped Geocoding Data
203203
geocoding.zip
204-
testgeocoding.zip
204+
testgeocoding.zip
205+
206+
# Performance tests artifacts
207+
csharp/PhoneNumbers.PerformanceTest/BenchmarkDotNet.Artifacts/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
4+
namespace PhoneNumbers.PerformanceTest.Benchmarks
5+
{
6+
[MemoryDiagnoser]
7+
[SimpleJob(RuntimeMoniker.Net48)]
8+
[SimpleJob(RuntimeMoniker.Net60)]
9+
[SimpleJob(RuntimeMoniker.Net70)]
10+
[SimpleJob(RuntimeMoniker.Net80)]
11+
public class PhoneNumberFormatBenchmark
12+
{
13+
#if NETFRAMEWORK
14+
private PhoneNumberUtil _phoneNumberUtil = null;
15+
#else
16+
private PhoneNumberUtil _phoneNumberUtil = null!;
17+
#endif
18+
19+
#if NETFRAMEWORK
20+
private PhoneNumber _phoneNumber;
21+
#else
22+
private PhoneNumber _phoneNumber = null!;
23+
#endif
24+
[GlobalSetup]
25+
public void Setup()
26+
{
27+
_phoneNumberUtil = PhoneNumberUtil.GetInstance();
28+
_phoneNumber = _phoneNumberUtil.Parse("+14156667777", "US");
29+
}
30+
31+
[Benchmark]
32+
public void FormatPhoneNumber()
33+
{
34+
_phoneNumberUtil.Format(_phoneNumber, PhoneNumberFormat.INTERNATIONAL);
35+
}
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Jobs;
3+
4+
namespace PhoneNumbers.PerformanceTest.Benchmarks
5+
{
6+
[MemoryDiagnoser]
7+
[SimpleJob(RuntimeMoniker.Net48)]
8+
[SimpleJob(RuntimeMoniker.Net60)]
9+
[SimpleJob(RuntimeMoniker.Net70)]
10+
[SimpleJob(RuntimeMoniker.Net80)]
11+
public class PhoneNumberParseBenchmark
12+
{
13+
#if NETFRAMEWORK
14+
private PhoneNumberUtil _phoneNumberUtil = null;
15+
#else
16+
private PhoneNumberUtil _phoneNumberUtil = null!;
17+
#endif
18+
19+
[GlobalSetup]
20+
public void Setup()
21+
{
22+
_phoneNumberUtil = PhoneNumberUtil.GetInstance();
23+
}
24+
25+
[Benchmark]
26+
public void ParsePhoneNumber()
27+
{
28+
_phoneNumberUtil.Parse("+14156667777", "US");
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>netframework4.8;net6.0;net7.0;net8.0</TargetFrameworks>
6+
<ImplicitUsings>disable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0' Or '$(TargetFramework)' == 'net7.0' Or '$(TargetFramework)' == 'net8.0'">
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
15+
</ItemGroup>
16+
17+
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
18+
<PackageReference Include="System.Memory" Version="4.5.4" />
19+
</ItemGroup>
20+
21+
<ItemGroup>
22+
<ProjectReference Include="..\PhoneNumbers\PhoneNumbers.csproj" />
23+
</ItemGroup>
24+
25+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using BenchmarkDotNet.Running;
2+
using PhoneNumbers.PerformanceTest.Benchmarks;
3+
4+
namespace PhoneNumbers.PerformanceTest
5+
{
6+
public static class Program
7+
{
8+
public static void Main(string[] args)
9+
{
10+
BenchmarkRunner.Run<PhoneNumberFormatBenchmark>();
11+
12+
BenchmarkRunner.Run<PhoneNumberParseBenchmark>();
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Performance testing history
2+
3+
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
4+
5+
Below you can see a sample of what the results might look like
6+
7+
| Method | Job | Runtime | Mean | Error | StdDev | Gen0 | Allocated |
8+
|------------------ |------------------- |------------------- |---------:|----------:|----------:|-------:|----------:|
9+
| FormatPhoneNumber | .NET 6.0 | .NET 6.0 | 1.234 us | 0.0158 us | 0.0124 us | 0.0076 | 152 B |
10+
| FormatPhoneNumber | .NET 7.0 | .NET 7.0 | 1.307 us | 0.0015 us | 0.0013 us | 0.0076 | 152 B |
11+
| FormatPhoneNumber | .NET 8.0 | .NET 8.0 | 1.141 us | 0.0080 us | 0.0071 us | 0.0076 | 152 B |
12+
| FormatPhoneNumber | .NET Framework 4.8 | .NET Framework 4.8 | 2.746 us | 0.0114 us | 0.0101 us | 0.1335 | 851 B |
13+

0 commit comments

Comments
 (0)