Using new stopwatch, some cleanup. No functional change

This commit is contained in:
Toastie (DCS Team) 2024-06-29 01:33:36 +12:00
parent a6e891fc92
commit a2890195a9
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
6 changed files with 16 additions and 21 deletions

View file

@ -93,7 +93,7 @@ public sealed class Bot : IBot
private void AddServices()
{
var startingGuildIdList = GetCurrentGuildIds();
var sw = Stopwatch.StartNew();
var startTime = Stopwatch.GetTimestamp();
var bot = Client.CurrentUser;
using (var uow = _db.GetDbContext())
@ -161,8 +161,7 @@ public sealed class Bot : IBot
LoadTypeReaders(a);
}
sw.Stop();
Log.Information("All services loaded in {ServiceLoadTime:F2}s", sw.Elapsed.TotalSeconds);
Log.Information("All services loaded in {ServiceLoadTime:F2}s", Stopwatch.GetElapsedTime(startTime).TotalSeconds);
}
private void LoadTypeReaders(Assembly assembly)
@ -259,7 +258,7 @@ public sealed class Bot : IBot
if (ShardId == 0)
await _db.SetupAsync();
var sw = Stopwatch.StartNew();
var startTime = Stopwatch.GetTimestamp();
await LoginAsync(_creds.Token);
@ -274,8 +273,7 @@ public sealed class Bot : IBot
Helpers.ReadErrorAndExit(9);
}
sw.Stop();
Log.Information("Shard {ShardId} connected in {Elapsed:F2}s", Client.ShardId, sw.Elapsed.TotalSeconds);
Log.Information("Shard {ShardId} connected in {Elapsed:F2}s", Client.ShardId, Stopwatch.GetElapsedTime(startTime).TotalSeconds);
var commandHandler = Services.GetRequiredService<CommandHandler>();
// start handling messages received in commandhandler

View file

@ -165,7 +165,7 @@ public class ChatterBotService : IExecOnMessage
}
else
{
Log.Warning("Error in chatterbot: {Error}", error);
Log.Warning("Error in chatterbot: {Error}", error.Value);
}
Log.Information("""

View file

@ -103,6 +103,8 @@ public class OfficialGptSession : IChatterBotSession
try
{
var response = JsonConvert.DeserializeObject<OpenAiCompletionResponse>(dataString);
Log.Information("Received response: {response}", dataString);
var res = response?.Choices?[0];
var message = res?.Message?.Content;

View file

@ -32,20 +32,21 @@ public sealed class SearxSearchService : SearchServiceBase, IEService
var instanceUrl = GetRandomInstance();
Log.Information("Using {Instance} instance for web search...", instanceUrl);
var sw = Stopwatch.StartNew();
var startTime = Stopwatch.GetTimestamp();
using var http = _http.CreateClient();
await using var res = await http.GetStreamAsync($"{instanceUrl}"
+ $"?q={Uri.EscapeDataString(query)}"
+ $"&format=json"
+ $"&strict=2");
sw.Stop();
var elapsed = Stopwatch.GetElapsedTime(startTime);
var dat = await JsonSerializer.DeserializeAsync<SearxSearchResult>(res);
if (dat is null)
return new SearxSearchResult();
dat.SearchTime = sw.Elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture);
dat.SearchTime = elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture);
return dat;
}
@ -56,7 +57,7 @@ public sealed class SearxSearchService : SearchServiceBase, IEService
var instanceUrl = GetRandomInstance();
Log.Information("Using {Instance} instance for img search...", instanceUrl);
var sw = Stopwatch.StartNew();
var startTime = Stopwatch.GetTimestamp();
using var http = _http.CreateClient();
await using var res = await http.GetStreamAsync($"{instanceUrl}"
+ $"?q={Uri.EscapeDataString(query)}"
@ -64,13 +65,13 @@ public sealed class SearxSearchService : SearchServiceBase, IEService
+ $"&category_images=on"
+ $"&strict=2");
sw.Stop();
var elapsed = Stopwatch.GetElapsedTime(startTime);
var dat = await JsonSerializer.DeserializeAsync<SearxImageSearchResult>(res);
if (dat is null)
return new SearxImageSearchResult();
dat.SearchTime = sw.Elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture);
dat.SearchTime = elapsed.TotalSeconds.ToString("N2", CultureInfo.InvariantCulture);
return dat;
}
}

View file

@ -8,9 +8,9 @@ public sealed class CommandPromptResultModel
public required string Name { get; set; }
[JsonPropertyName("arguments")]
public required Dictionary<string, string> Arguments { get; set; }
public Dictionary<string, string> Arguments { get; set; } = new();
[JsonPropertyName("remaining")]
[JsonConverter(typeof(NumberToStringConverter))]
public required string Remaining { get; set; }
public string Remaining { get; set; } = string.Empty;
}

View file

@ -229,10 +229,4 @@ public static class Extensions
public static IEnumerable<IRole> GetRoles(this IGuildUser user)
=> user.RoleIds.Select(r => user.Guild.GetRole(r)).Where(r => r is not null);
// todo remove
public static void Lap(this Stopwatch sw, string checkpoint)
{
Log.Information("Checkpoint {CheckPoint}: {Time}ms", checkpoint, sw.Elapsed.TotalMilliseconds);
sw.Restart();
}
}