Skip to content

Commit

Permalink
Add more accurate timekeeping and ETA
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Dec 11, 2023
1 parent e3a698d commit f4fb8cb
Showing 1 changed file with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ public class ImportHighScoresCommand

public async Task<int> OnExecuteAsync(CancellationToken cancellationToken)
{
startupTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

if (!CheckSlaveLatency)
scoresPerQuery = maximum_scores_per_query;

Expand Down Expand Up @@ -238,19 +236,34 @@ public async Task<int> OnExecuteAsync(CancellationToken cancellationToken)

while (!runningBatches.All(t => t.Task.IsCompleted))
{
long currentTimestamp = DateTimeOffset.Now.ToUnixTimeSeconds();
long currentTimestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();

if (currentTimestamp - lastCommitTimestamp >= seconds_between_report)
if (currentTimestamp - lastCommitTimestamp >= seconds_between_report / 1000)
{
int inserted = Interlocked.Exchange(ref currentReportInsertCount, 0);
int updated = Interlocked.Exchange(ref currentReportUpdateCount, 0);

double secondsPassed = (double)(currentTimestamp - lastCommitTimestamp) / 1000;
// Only set startup timestamp after first insert actual insert/update run to avoid weighting during catch-up.
if (inserted + updated > 0 && startupTimestamp == 0)
startupTimestamp = lastCommitTimestamp;

double secondsSinceStart = (double)(currentTimestamp - startupTimestamp) / 1000;
double processingRate = (totalInsertCount + totalUpdateCount) / secondsSinceStart;

string eta = string.Empty;

if (singleRun)
{
double secondsLeft = (maxProcessableId - lastId) / processingRate;

int etaHours = (int)(secondsLeft / 3600);
int etaMins = (int)(secondsLeft - etaHours * 3600) / 60;
eta = $"{etaHours}h{etaMins}m";
}

Console.WriteLine($"Inserting up to {lastId:N0} "
+ $"[{runningBatches.Count(t => t.Task.IsCompleted),-2}/{runningBatches.Count}] "
+ $"{totalInsertCount:N0} inserted {totalUpdateCount:N0} updated {totalSkipCount:N0} skipped (+{inserted:N0} new +{updated:N0} upd {(inserted + updated) / secondsPassed:N0}/s {(totalInsertCount + totalUpdateCount) / secondsSinceStart})");
+ $"{totalInsertCount:N0} inserted {totalUpdateCount:N0} updated {totalSkipCount:N0} skipped (+{inserted:N0} new +{updated:N0} upd) {processingRate:N0}/s {eta}");

lastCommitTimestamp = currentTimestamp;
}
Expand Down

0 comments on commit f4fb8cb

Please sign in to comment.