[backend] Switch to more efficient TimeSpan/DateTime/-Offset handling where applicable

This commit is contained in:
Laura Hausmann 2024-10-30 17:55:41 +01:00
parent a5f292cdb3
commit fbdab96f9d
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
5 changed files with 17 additions and 15 deletions

View file

@ -93,7 +93,7 @@ public static class HttpSignature
{
ArgumentNullException.ThrowIfNull(request.RequestUri);
request.Headers.Date = DateTime.Now;
request.Headers.Date = DateTimeOffset.UtcNow;
request.Headers.Host = request.RequestUri.Host;
var requiredHeadersEnum = requiredHeaders.ToList();

View file

@ -57,7 +57,7 @@ public static class LdSignature
{
var options = new SignatureOptions
{
Created = DateTime.Now,
Created = DateTime.UtcNow,
Creator = new ASObjectBase(creator),
Nonce = CryptographyHelpers.GenerateRandomHexString(16),
Type = ["_:RsaSignature2017"],

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using System.Globalization;
namespace Iceshrimp.Backend.Core.Middleware;
@ -8,10 +9,10 @@ public class RequestDurationMiddleware : IMiddleware
{
if (ctx.GetEndpoint()?.Metadata.GetMetadata<HideRequestDuration>() == null)
{
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
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",
duration.ToString(CultureInfo.InvariantCulture) + " ms");
return Task.CompletedTask;

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using Iceshrimp.Backend.Core.Helpers;
namespace Iceshrimp.Tests.Concurrency;
@ -9,7 +10,7 @@ public class EventTests
public async Task TestAsyncAutoResetEvent()
{
var autoResetEvent = new AsyncAutoResetEvent();
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
var task = autoResetEvent.WaitAsync();
_ = Task.Run(async () =>
{
@ -17,7 +18,7 @@ public class EventTests
autoResetEvent.Set();
});
await task;
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
autoResetEvent.Signaled.Should().BeFalse();
}
@ -25,7 +26,7 @@ public class EventTests
public async Task TestAsyncAutoResetEventWithoutReset()
{
var autoResetEvent = new AsyncAutoResetEvent();
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
var task = autoResetEvent.WaitWithoutResetAsync();
_ = Task.Run(async () =>
{
@ -33,7 +34,7 @@ public class EventTests
autoResetEvent.Set();
});
await task;
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
autoResetEvent.Signaled.Should().BeTrue();
}
@ -41,7 +42,7 @@ public class EventTests
public async Task TestAsyncAutoResetEventMulti()
{
var autoResetEvent = new AsyncAutoResetEvent();
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
Task[] tasks = [autoResetEvent.WaitAsync(), autoResetEvent.WaitAsync()];
_ = Task.Run(async () =>
{
@ -49,7 +50,7 @@ public class EventTests
autoResetEvent.Set();
});
await Task.WhenAll(tasks);
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
autoResetEvent.Signaled.Should().BeFalse();
}
@ -57,7 +58,7 @@ public class EventTests
public async Task TestAsyncAutoResetEventWithoutResetMulti()
{
var autoResetEvent = new AsyncAutoResetEvent();
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
Task[] tasks = [autoResetEvent.WaitWithoutResetAsync(), autoResetEvent.WaitWithoutResetAsync()];
_ = Task.Run(async () =>
{
@ -65,7 +66,7 @@ public class EventTests
autoResetEvent.Set();
});
await Task.WhenAll(tasks);
(DateTime.Now - pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
Stopwatch.GetElapsedTime(pre).Should().BeGreaterThan(TimeSpan.FromMilliseconds(45));
autoResetEvent.Signaled.Should().BeTrue();
}

View file

@ -1,3 +1,4 @@
using System.Diagnostics;
using Iceshrimp.Backend.Core.Helpers.LibMfm.Serialization;
using Iceshrimp.Parsing;
using Microsoft.FSharp.Collections;
@ -376,10 +377,9 @@ public class MfmTests
double RunBenchmark()
{
var pre = DateTime.Now;
var pre = Stopwatch.GetTimestamp();
Mfm.parse(mfm);
var post = DateTime.Now;
var ms = (post - pre).TotalMilliseconds;
var ms = Stopwatch.GetElapsedTime(pre).TotalMilliseconds;
Console.WriteLine($@"Took {ms} ms");
return ms;
}