Skip to content

Commit

Permalink
More output
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Oct 28, 2024
1 parent 4f19bfc commit dee7c43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions GlobalRankLookupCache/Controllers/BeatmapItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public BeatmapItem(int beatmapId, string highScoresTable)
using (var db = await Program.GetDatabaseConnection())
using (var cmd = db.CreateCommand())
{
Interlocked.Increment(ref BeatmapRankCacheCollection.Misses);
Console.Write("q");

cmd.CommandTimeout = 10;
Expand All @@ -65,7 +66,7 @@ public BeatmapItem(int beatmapId, string highScoresTable)
}
else
{
Console.Write($".");
Console.Write(".");
lastPopulation = DateTimeOffset.Now;
requestsSinceLastPopulation--;
}
Expand Down Expand Up @@ -101,7 +102,7 @@ private async Task repopulateScores()
{
var users = new HashSet<int>();

cmd.CommandTimeout = 120;
cmd.CommandTimeout = 180;
cmd.CommandText = $"SELECT user_id, score FROM {highScoresTable} WHERE beatmap_id = {beatmapId} AND hidden = 0";

Console.Write(isRepopulate ? "r" : "p");
Expand Down
29 changes: 29 additions & 0 deletions GlobalRankLookupCache/Controllers/BeatmapRankCacheCollection.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Extensions;

namespace GlobalRankLookupCache.Controllers
{
internal class BeatmapRankCacheCollection
{
public static int Hits;
public static int Misses;
public static int Populations;

private readonly string highScoresTable;

private readonly ConcurrentDictionary<int, BeatmapItem> beatmapScoresLookup = new ConcurrentDictionary<int, BeatmapItem>();
Expand All @@ -29,9 +35,32 @@ public void Update(int beatmapId, in int oldScore, in int newScore)

public bool Clear(in int beatmapId) => beatmapScoresLookup.TryRemove(beatmapId, out var _);

private long lastReport = DateTimeOffset.Now.ToUnixTimeSeconds();

public Task<(int position, int total)> Lookup(int beatmapId, in int score)
{
long unixSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();

if (unixSeconds - lastReport >= 10)
{
if (unixSeconds - Interlocked.Exchange(ref lastReport, unixSeconds) > 10)
output();
}

return beatmapScoresLookup.GetOrAdd(beatmapId, new BeatmapItem(beatmapId, highScoresTable)).Lookup(score);
}

private void output()
{
double hits = Interlocked.Exchange(ref Hits, 0);
double misses = Interlocked.Exchange(ref Misses, 0);
double populations = Interlocked.Exchange(ref Populations, 0);

double hitRate = hits / (hits + misses);

long memory = GC.GetTotalMemory(false);

Console.WriteLine($"mem: {memory / 1048576:N0} MB beatmaps: {beatmapScoresLookup.Count} hr:{hitRate:P0} h:{hits:N0} m:{misses:N0} p:{populations:N0}");
}
}
}

0 comments on commit dee7c43

Please sign in to comment.