Skip to content

Commit

Permalink
Performance logging & increased regex performance
Browse files Browse the repository at this point in the history
Thanks, Arimah.

This also fixes a stray comment
  • Loading branch information
Simyon264 committed Apr 29, 2024
1 parent be0684f commit 3405174
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 0 additions & 1 deletion Client/Components/Pages/Leaderboard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ else
@foreach (var player in LeaderboardData.MostSeenPlayers)
{
<tr>
// Player name with link to their profile
<td><a href="/player/@player.Value.Player.PlayerGuid">@player.Value.Player.Username</a></td>
<td>@player.Value.Count</td>
</tr>
Expand Down
20 changes: 17 additions & 3 deletions Server/Api/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Server.Api;
[Route("api/[controller]")]
public class DataController : ControllerBase
{
public static readonly Regex HuntedRegex = new Regex(@"(?<=Kill(?:\sor\smaroon)\s)([^,]+)");
public static readonly Regex HuntedRegex = new Regex(@"Kill(?: or maroon? ([^,]+))");

private readonly ReplayDbContext _context;
private readonly IMemoryCache _cache;
Expand Down Expand Up @@ -171,11 +171,18 @@ public async Task<LeaderboardData> GetLeaderboard(
return leaderboardData;
}

var stopwatch = new Stopwatch();
stopwatch.Start();

var rangeTimespan = rangeOption.GetTimeSpan();
var dataReplays = await _context.Replays
.Where(r => r.Date > DateTime.UtcNow - rangeTimespan)
.Include(r => r.RoundEndPlayers)
.ToListAsync();

stopwatch.Stop();
Log.Information("Fetching replays took {Time}ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();

var leaderboardResult = new LeaderboardData()
{
Expand Down Expand Up @@ -249,7 +256,7 @@ public async Task<LeaderboardData> GetLeaderboard(
var matches = HuntedRegex.Matches(dataReplay.RoundEndText);
foreach (Match match in matches)
{
var playerName = match.Value.Trim();
var playerName = match.Groups[1].Value.Trim();
var player = dataReplay.RoundEndPlayers.FirstOrDefault(p => p.PlayerIcName == playerName);
if (player == null)
continue;
Expand Down Expand Up @@ -287,6 +294,10 @@ public async Task<LeaderboardData> GetLeaderboard(
.Take(10)
.ToDictionary(p => p.Key, p => p.Value);

stopwatch.Stop();
Log.Information("Calculating leaderboard took {Time}ms", stopwatch.ElapsedMilliseconds);
stopwatch.Restart();

// Now we need to fetch the usernames for the players
foreach (var player in leaderboardResult.MostSeenPlayers)
{
Expand All @@ -309,9 +320,12 @@ public async Task<LeaderboardData> GetLeaderboard(
await Task.Delay(50); // Rate limit the API
}

stopwatch.Stop();
Log.Information("Fetching usernames took {Time}ms", stopwatch.ElapsedMilliseconds);

// Save leaderboard to cache (its expensive as fuck to calculate)
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetAbsoluteExpiration(TimeSpan.FromHours(3));
.SetAbsoluteExpiration(TimeSpan.FromHours(5));
var cacheLeaderboard = leaderboardResult;
cacheLeaderboard.IsCache = true;

Expand Down

0 comments on commit 3405174

Please sign in to comment.