[backend] Switch to more efficient TimeSpan/DateTime/-Offset handling where applicable
This commit is contained in:
parent
a5f292cdb3
commit
fbdab96f9d
5 changed files with 17 additions and 15 deletions
|
@ -93,7 +93,7 @@ public static class HttpSignature
|
||||||
{
|
{
|
||||||
ArgumentNullException.ThrowIfNull(request.RequestUri);
|
ArgumentNullException.ThrowIfNull(request.RequestUri);
|
||||||
|
|
||||||
request.Headers.Date = DateTime.Now;
|
request.Headers.Date = DateTimeOffset.UtcNow;
|
||||||
request.Headers.Host = request.RequestUri.Host;
|
request.Headers.Host = request.RequestUri.Host;
|
||||||
|
|
||||||
var requiredHeadersEnum = requiredHeaders.ToList();
|
var requiredHeadersEnum = requiredHeaders.ToList();
|
||||||
|
|
|
@ -57,7 +57,7 @@ public static class LdSignature
|
||||||
{
|
{
|
||||||
var options = new SignatureOptions
|
var options = new SignatureOptions
|
||||||
{
|
{
|
||||||
Created = DateTime.Now,
|
Created = DateTime.UtcNow,
|
||||||
Creator = new ASObjectBase(creator),
|
Creator = new ASObjectBase(creator),
|
||||||
Nonce = CryptographyHelpers.GenerateRandomHexString(16),
|
Nonce = CryptographyHelpers.GenerateRandomHexString(16),
|
||||||
Type = ["_:RsaSignature2017"],
|
Type = ["_:RsaSignature2017"],
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
|
||||||
namespace Iceshrimp.Backend.Core.Middleware;
|
namespace Iceshrimp.Backend.Core.Middleware;
|
||||||
|
@ -8,10 +9,10 @@ public class RequestDurationMiddleware : IMiddleware
|
||||||
{
|
{
|
||||||
if (ctx.GetEndpoint()?.Metadata.GetMetadata<HideRequestDuration>() == null)
|
if (ctx.GetEndpoint()?.Metadata.GetMetadata<HideRequestDuration>() == null)
|
||||||
{
|
{
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
ctx.Response.OnStarting(() =>
|
ctx.Response.OnStarting(() =>
|
||||||
{
|
{
|
||||||
var duration = (int)(DateTime.Now - pre).TotalMilliseconds;
|
var duration = Math.Truncate(Stopwatch.GetElapsedTime(pre).TotalMilliseconds);
|
||||||
ctx.Response.Headers.Append("X-Request-Duration",
|
ctx.Response.Headers.Append("X-Request-Duration",
|
||||||
duration.ToString(CultureInfo.InvariantCulture) + " ms");
|
duration.ToString(CultureInfo.InvariantCulture) + " ms");
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Diagnostics;
|
||||||
using Iceshrimp.Backend.Core.Helpers;
|
using Iceshrimp.Backend.Core.Helpers;
|
||||||
|
|
||||||
namespace Iceshrimp.Tests.Concurrency;
|
namespace Iceshrimp.Tests.Concurrency;
|
||||||
|
@ -9,7 +10,7 @@ public class EventTests
|
||||||
public async Task TestAsyncAutoResetEvent()
|
public async Task TestAsyncAutoResetEvent()
|
||||||
{
|
{
|
||||||
var autoResetEvent = new AsyncAutoResetEvent();
|
var autoResetEvent = new AsyncAutoResetEvent();
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
var task = autoResetEvent.WaitAsync();
|
var task = autoResetEvent.WaitAsync();
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
|
@ -17,7 +18,7 @@ public class EventTests
|
||||||
autoResetEvent.Set();
|
autoResetEvent.Set();
|
||||||
});
|
});
|
||||||
await task;
|
await task;
|
||||||
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
||||||
autoResetEvent.Signaled.Should().BeFalse();
|
autoResetEvent.Signaled.Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ public class EventTests
|
||||||
public async Task TestAsyncAutoResetEventWithoutReset()
|
public async Task TestAsyncAutoResetEventWithoutReset()
|
||||||
{
|
{
|
||||||
var autoResetEvent = new AsyncAutoResetEvent();
|
var autoResetEvent = new AsyncAutoResetEvent();
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
var task = autoResetEvent.WaitWithoutResetAsync();
|
var task = autoResetEvent.WaitWithoutResetAsync();
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
|
@ -33,7 +34,7 @@ public class EventTests
|
||||||
autoResetEvent.Set();
|
autoResetEvent.Set();
|
||||||
});
|
});
|
||||||
await task;
|
await task;
|
||||||
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
||||||
autoResetEvent.Signaled.Should().BeTrue();
|
autoResetEvent.Signaled.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +42,7 @@ public class EventTests
|
||||||
public async Task TestAsyncAutoResetEventMulti()
|
public async Task TestAsyncAutoResetEventMulti()
|
||||||
{
|
{
|
||||||
var autoResetEvent = new AsyncAutoResetEvent();
|
var autoResetEvent = new AsyncAutoResetEvent();
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
Task[] tasks = [autoResetEvent.WaitAsync(), autoResetEvent.WaitAsync()];
|
Task[] tasks = [autoResetEvent.WaitAsync(), autoResetEvent.WaitAsync()];
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
|
@ -49,7 +50,7 @@ public class EventTests
|
||||||
autoResetEvent.Set();
|
autoResetEvent.Set();
|
||||||
});
|
});
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
||||||
autoResetEvent.Signaled.Should().BeFalse();
|
autoResetEvent.Signaled.Should().BeFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +58,7 @@ public class EventTests
|
||||||
public async Task TestAsyncAutoResetEventWithoutResetMulti()
|
public async Task TestAsyncAutoResetEventWithoutResetMulti()
|
||||||
{
|
{
|
||||||
var autoResetEvent = new AsyncAutoResetEvent();
|
var autoResetEvent = new AsyncAutoResetEvent();
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
Task[] tasks = [autoResetEvent.WaitWithoutResetAsync(), autoResetEvent.WaitWithoutResetAsync()];
|
Task[] tasks = [autoResetEvent.WaitWithoutResetAsync(), autoResetEvent.WaitWithoutResetAsync()];
|
||||||
_ = Task.Run(async () =>
|
_ = Task.Run(async () =>
|
||||||
{
|
{
|
||||||
|
@ -65,7 +66,7 @@ public class EventTests
|
||||||
autoResetEvent.Set();
|
autoResetEvent.Set();
|
||||||
});
|
});
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
|
||||||
autoResetEvent.Signaled.Should().BeTrue();
|
autoResetEvent.Signaled.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Diagnostics;
|
||||||
using Iceshrimp.Backend.Core.Helpers.LibMfm.Serialization;
|
using Iceshrimp.Backend.Core.Helpers.LibMfm.Serialization;
|
||||||
using Iceshrimp.Parsing;
|
using Iceshrimp.Parsing;
|
||||||
using Microsoft.FSharp.Collections;
|
using Microsoft.FSharp.Collections;
|
||||||
|
@ -376,10 +377,9 @@ public class MfmTests
|
||||||
|
|
||||||
double RunBenchmark()
|
double RunBenchmark()
|
||||||
{
|
{
|
||||||
var pre = DateTime.Now;
|
var pre = Stopwatch.GetTimestamp();
|
||||||
Mfm.parse(mfm);
|
Mfm.parse(mfm);
|
||||||
var post = DateTime.Now;
|
var ms = Stopwatch.GetElapsedTime(pre).TotalMilliseconds;
|
||||||
var ms = (post - pre).TotalMilliseconds;
|
|
||||||
Console.WriteLine($@"Took {ms} ms");
|
Console.WriteLine($@"Took {ms} ms");
|
||||||
return ms;
|
return ms;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue