-
-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add startup and performance benchmarks (#1731)
* chore: use file scoped namespaces * feat: add performance and startup benchmarks * feat: add first call benchmarks
- Loading branch information
1 parent
b75734a
commit 8a40692
Showing
8 changed files
with
480 additions
and
344 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,77 +1,76 @@ | ||
namespace Refit.Benchmarks | ||
namespace Refit.Benchmarks; | ||
|
||
public interface IGitHubService | ||
{ | ||
public interface IGitHubService | ||
{ | ||
//Task - throws | ||
[Get("/users")] | ||
public Task GetUsersTaskAsync(); | ||
|
||
[Post("/users")] | ||
public Task PostUsersTaskAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<string> - throws | ||
[Get("/users")] | ||
public Task<string> GetUsersTaskStringAsync(); | ||
|
||
[Post("/users")] | ||
public Task<string> PostUsersTaskStringAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<Stream> - throws | ||
[Get("/users")] | ||
public Task<Stream> GetUsersTaskStreamAsync(); | ||
|
||
[Post("/users")] | ||
public Task<Stream> PostUsersTaskStreamAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<HttpContent> - throws | ||
[Get("/users")] | ||
public Task<HttpContent> GetUsersTaskHttpContentAsync(); | ||
|
||
[Post("/users")] | ||
public Task<HttpContent> PostUsersTaskHttpContentAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<HttpResponseMessage> | ||
[Get("/users")] | ||
public Task<HttpResponseMessage> GetUsersTaskHttpResponseMessageAsync(); | ||
|
||
[Post("/users")] | ||
public Task<HttpResponseMessage> PostUsersTaskHttpResponseMessageAsync( | ||
[Body] IEnumerable<User> users | ||
); | ||
|
||
//IObservable<HttpResponseMessage> | ||
[Get("/users")] | ||
public IObservable<HttpResponseMessage> GetUsersObservableHttpResponseMessage(); | ||
|
||
[Post("/users")] | ||
public IObservable<HttpResponseMessage> PostUsersObservableHttpResponseMessage( | ||
[Body] IEnumerable<User> users | ||
); | ||
|
||
//Task<<T>> - throws | ||
[Get("/users")] | ||
public Task<List<User>> GetUsersTaskTAsync(); | ||
|
||
[Post("/users")] | ||
public Task<List<User>> PostUsersTaskTAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<ApiResponse<T>> | ||
[Get("/users")] | ||
public Task<ApiResponse<List<User>>> GetUsersTaskApiResponseTAsync(); | ||
|
||
[Post("/users")] | ||
public Task<ApiResponse<List<User>>> PostUsersTaskApiResponseTAsync( | ||
[Body] IEnumerable<User> users | ||
); | ||
} | ||
|
||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Bio { get; set; } | ||
public int Followers { get; set; } | ||
public int Following { get; set; } | ||
public string Url { get; set; } | ||
} | ||
//Task - throws | ||
[Get("/users")] | ||
public Task GetUsersTaskAsync(); | ||
|
||
[Post("/users")] | ||
public Task PostUsersTaskAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<string> - throws | ||
[Get("/users")] | ||
public Task<string> GetUsersTaskStringAsync(); | ||
|
||
[Post("/users")] | ||
public Task<string> PostUsersTaskStringAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<Stream> - throws | ||
[Get("/users")] | ||
public Task<Stream> GetUsersTaskStreamAsync(); | ||
|
||
[Post("/users")] | ||
public Task<Stream> PostUsersTaskStreamAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<HttpContent> - throws | ||
[Get("/users")] | ||
public Task<HttpContent> GetUsersTaskHttpContentAsync(); | ||
|
||
[Post("/users")] | ||
public Task<HttpContent> PostUsersTaskHttpContentAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<HttpResponseMessage> | ||
[Get("/users")] | ||
public Task<HttpResponseMessage> GetUsersTaskHttpResponseMessageAsync(); | ||
|
||
[Post("/users")] | ||
public Task<HttpResponseMessage> PostUsersTaskHttpResponseMessageAsync( | ||
[Body] IEnumerable<User> users | ||
); | ||
|
||
//IObservable<HttpResponseMessage> | ||
[Get("/users")] | ||
public IObservable<HttpResponseMessage> GetUsersObservableHttpResponseMessage(); | ||
|
||
[Post("/users")] | ||
public IObservable<HttpResponseMessage> PostUsersObservableHttpResponseMessage( | ||
[Body] IEnumerable<User> users | ||
); | ||
|
||
//Task<<T>> - throws | ||
[Get("/users")] | ||
public Task<List<User>> GetUsersTaskTAsync(); | ||
|
||
[Post("/users")] | ||
public Task<List<User>> PostUsersTaskTAsync([Body] IEnumerable<User> users); | ||
|
||
//Task<ApiResponse<T>> | ||
[Get("/users")] | ||
public Task<ApiResponse<List<User>>> GetUsersTaskApiResponseTAsync(); | ||
|
||
[Post("/users")] | ||
public Task<ApiResponse<List<User>>> PostUsersTaskApiResponseTAsync( | ||
[Body] IEnumerable<User> users | ||
); | ||
} | ||
|
||
public class User | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public string Bio { get; set; } | ||
public int Followers { get; set; } | ||
public int Following { get; set; } | ||
public string Url { get; set; } | ||
} |
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,28 @@ | ||
namespace Refit.Benchmarks; | ||
|
||
public interface IPerformanceService | ||
{ | ||
[Get("/users")] | ||
public Task<string> ConstantRoute(); | ||
|
||
[Get("/users/{id}")] | ||
public Task<string> DynamicRoute(int id); | ||
|
||
[Get("/users/{id}/{user}/{status}")] | ||
public Task<string> ComplexDynamicRoute(int id, string user, string status); | ||
|
||
[Get("/users/{request.someProperty}")] | ||
public Task<string> ObjectRequest(PathBoundObject request); | ||
|
||
[Post("/users/{id}/{request.someProperty}")] | ||
[Headers("User-Agent: Awesome Octocat App", "X-Emoji: :smile_cat:")] | ||
public Task<string> ComplexRequest(int id, PathBoundObject request, [Query(CollectionFormat.Multi)]int[] queries); | ||
} | ||
|
||
public class PathBoundObject | ||
{ | ||
public string SomeProperty { get; set; } | ||
|
||
[Query] | ||
public string SomeQuery { get; set; } | ||
} |
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,48 @@ | ||
using System.Net; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Refit.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class PerformanceBenchmark | ||
{ | ||
private IPerformanceService? service; | ||
|
||
private const string Host = "https://github.com"; | ||
private SystemTextJsonContentSerializer systemTextJsonContentSerializer; | ||
|
||
[GlobalSetup] | ||
public Task SetupAsync() | ||
{ | ||
systemTextJsonContentSerializer = new SystemTextJsonContentSerializer(); | ||
service = | ||
RestService.For<IPerformanceService>( | ||
Host, | ||
new RefitSettings(systemTextJsonContentSerializer) | ||
{ | ||
HttpMessageHandlerFactory = () => | ||
new StaticValueHttpResponseHandler( | ||
"Ok", | ||
HttpStatusCode.OK | ||
) | ||
} | ||
); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
[Benchmark] | ||
public async Task<string> ConstantRouteAsync() => await service.ConstantRoute(); | ||
|
||
[Benchmark] | ||
public async Task<string> DynamicRouteAsync() => await service.DynamicRoute(101); | ||
|
||
[Benchmark] | ||
public async Task<string> ComplexDynamicRouteAsync() => await service.ComplexDynamicRoute(101, "tom", "yCxv"); | ||
|
||
[Benchmark] | ||
public async Task<string> ObjectRequestAsync() => await service.ObjectRequest(new PathBoundObject(){SomeProperty = "myProperty", SomeQuery = "myQuery"}); | ||
|
||
[Benchmark] | ||
public async Task<string> ComplexRequestAsync() => await service.ComplexRequest(101, new PathBoundObject(){SomeProperty = "myProperty", SomeQuery = "myQuery"}, [1,2,3,4,5,6]); | ||
} |
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 |
---|---|---|
@@ -1,19 +1,13 @@ | ||
using BenchmarkDotNet.Running; | ||
using Refit.Benchmarks; | ||
|
||
namespace Refit.Benchmarks | ||
if (args is { Length: > 0 }) | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
if (args != null && args.Length > 0) | ||
{ | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | ||
} | ||
else | ||
{ | ||
BenchmarkRunner.Run<EndToEndBenchmark>(); | ||
} | ||
} | ||
} | ||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); | ||
} | ||
else | ||
{ | ||
BenchmarkRunner.Run<EndToEndBenchmark>(); | ||
// BenchmarkRunner.Run<StartupBenchmark>(); | ||
// BenchmarkRunner.Run<PerformanceBenchmarks>(); | ||
} |
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,49 @@ | ||
using System.Net; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Refit.Benchmarks; | ||
|
||
[MemoryDiagnoser] | ||
public class StartupBenchmark | ||
{ | ||
private IPerformanceService initialisedService; | ||
private const string Host = "https://github.com"; | ||
private readonly RefitSettings settings = new RefitSettings() | ||
{ | ||
HttpMessageHandlerFactory = () => | ||
new StaticValueHttpResponseHandler( | ||
"Ok", | ||
HttpStatusCode.OK | ||
) | ||
}; | ||
|
||
|
||
[IterationSetup(Targets = [nameof(FirstCallConstantRouteAsync), nameof(FirstCallComplexRequestAsync)])] | ||
public void Setup() | ||
{ | ||
initialisedService = RestService.For<IPerformanceService>(Host, settings); | ||
} | ||
|
||
[Benchmark] | ||
public IPerformanceService CreateService() => RestService.For<IPerformanceService>(Host, settings); | ||
|
||
[Benchmark] | ||
public async Task<string> FirstCallConstantRouteAsync() => await initialisedService.ConstantRoute(); | ||
|
||
[Benchmark] | ||
public async Task<string> ConstantRouteAsync() | ||
{ | ||
var service = RestService.For<IPerformanceService>(Host, settings); | ||
return await service.ConstantRoute(); | ||
} | ||
|
||
[Benchmark] | ||
public async Task<string> FirstCallComplexRequestAsync() => await initialisedService.ObjectRequest(new PathBoundObject(){SomeProperty = "myProperty", SomeQuery = "myQuery"}); | ||
|
||
[Benchmark] | ||
public async Task<string> ComplexRequestAsync() | ||
{ | ||
var service = RestService.For<IPerformanceService>(Host, settings); | ||
return await service.ObjectRequest(new PathBoundObject(){SomeProperty = "myProperty", SomeQuery = "myQuery"}); | ||
} | ||
} |
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
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,20 @@ | ||
using System.Net; | ||
|
||
namespace Refit.Benchmarks; | ||
|
||
public class StaticValueHttpResponseHandler (string response, HttpStatusCode code) : HttpMessageHandler | ||
{ | ||
protected override Task<HttpResponseMessage> SendAsync( | ||
HttpRequestMessage request, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
return Task.FromResult( | ||
new HttpResponseMessage(code) | ||
{ | ||
RequestMessage = request, | ||
Content = new StringContent(response) | ||
} | ||
); | ||
} | ||
} |