-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Benchmark for strategy creation #1426
Conversation
| Method | Mean | Error | StdDev | Gen0 | Allocated | | ||
|------------ |-----------:|---------:|---------:|-------:|----------:| | ||
| Fallback_V7 | 114.8 ns | 1.84 ns | 2.70 ns | 0.0191 | 480 B | | ||
| Fallback_V8 | 4,324.7 ns | 21.92 ns | 31.43 ns | 0.2518 | 6504 B | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The creation in V8 is indeed more expensive as it involves a lot more infra. For this reason the recreation of strategies on hot path should be discouraged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would your generic recommendation be?
In the sandbox app the fallback behaviour is trivial and could be cached, but in the code on which it was based there's dynamic per-invocation behaviour going on to select the fallback.
It feels that to support the same behaviour the more performant option would be to drop Polly fallbacks entirely and deal with it at the call site like any "normal" code, which if true doesn't feel great that the performance for this scenario has actually gone backwards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I have seen a scenario where you are "forced" to recreate a strategy on a hot path.
At worst you need to fallback to some dynamic value, but that can be archived by passing such value to the fallback action using ResilienceContext
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you paste the code rather than a picture of it? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I tried to create a draft PR to your repo and got rejected.
public ResilienceStrategy<TResult> GetStrategy<TResult>(ApiEndpointOption endpoint, bool handleExecutionFaults, string resource)
{
return _registry.GetOrAddStrategy<TResult>($"fallback/{resource}/{handleExecutionFaults}", builder =>
{
var shouldHandle = new PredicateBuilder<TResult>()
.Handle<ApiException>()
.HandleHttpRequestFault()
.Handle<TaskCanceledException>();
if (handleExecutionFaults)
{
shouldHandle = shouldHandle
.Handle<BrokenCircuitException>()
.Handle<IsolatedCircuitException>()
.Handle<TimeoutRejectedException>();
}
builder
.AddStrategy(GetStrategy(endpoint, resource))
.AddFallback(new FallbackStrategyOptions<TResult>()
{
FallbackAction = context =>
{
if (context.Context.Properties.TryGetValue(FallbackKeys<TResult>.FallbackValue, out var value) && value != null)
{
return Outcome.FromResultAsTask(value());
}
return Outcome.FromResultAsTask<TResult>(default);
},
ShouldHandle = shouldHandle,
StrategyName = $"{endpoint.Name} Fallback",
});
});
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have the code for FallbackKeys
?
Nevermind, I can see it in the screenshot.
| Fallback_V7 | 114.8 ns | 1.84 ns | 2.70 ns | 0.0191 | 480 B | | ||
| Fallback_V8 | 4,324.7 ns | 21.92 ns | 31.43 ns | 0.2518 | 6504 B | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow that's a lot more 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think validation eats a lot of that as it involves reflection.
Codecov Report
@@ Coverage Diff @@
## main #1426 +/- ##
=======================================
Coverage 83.92% 83.92%
=======================================
Files 275 275
Lines 6526 6526
Branches 1007 1007
=======================================
Hits 5477 5477
Misses 840 840
Partials 209 209
Flags with carried forward coverage won't be shown. Click here to find out more. |
Details on the issue fix or feature implementation
Benchmark that demonstrates the creation of strategies (V7 vs V8).
Confirm the following