Skip to content

Commit

Permalink
Add a buffer for the top/bottom 10 downloaders for smoother transitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
thebracket committed Nov 13, 2024
1 parent 1a83a75 commit 61d3c42
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {RttHistogram} from "../graphs/rtt_histo";
import {clearDashDiv, theading, TopNTableFromMsgData, topNTableHeader, topNTableRow} from "../helpers/builders";
import {scaleNumber, rttCircleSpan, formatThroughput, formatRtt, formatRetransmit} from "../helpers/scaling";
import {redactCell} from "../helpers/redact";
import {TimedLRUCache} from "../helpers/timed_lru_cache";

export class Top10Downloaders extends BaseDashlet {
constructor(slot) {
super(slot);
this.buffer = new TimedLRUCache(10, 300);
}

title() {
Expand Down Expand Up @@ -39,12 +41,19 @@ export class Top10Downloaders extends BaseDashlet {
onMessage(msg) {
if (msg.event === "TopDownloads") {
let target = document.getElementById(this.id);
msg.data.forEach((r) => {
this.buffer.set(r.circuit_id, r);
});
this.buffer.tick();

let t = TopNTableFromMsgData(msg);
let results = this.buffer.toArray();
results.sort((a, b) => a.bits_per_second.down - b.bits_per_second.down);

let t = TopNTableFromMsgData(results);

// Display it
clearDashDiv(this.id, target);
target.appendChild(t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {RttHistogram} from "../graphs/rtt_histo";
import {clearDashDiv, theading, TopNTableFromMsgData, topNTableHeader, topNTableRow} from "../helpers/builders";
import {scaleNumber, rttCircleSpan, formatThroughput, formatRtt} from "../helpers/scaling";
import {redactCell} from "../helpers/redact";
import {TimedLRUCache} from "../helpers/timed_lru_cache";

export class Worst10Downloaders extends BaseDashlet {
constructor(slot) {
super(slot);
this.buffer = new TimedLRUCache(10, 300);
}

canBeSlowedDown() {
Expand Down Expand Up @@ -39,6 +41,13 @@ export class Worst10Downloaders extends BaseDashlet {
onMessage(msg) {
if (msg.event === "WorstRTT") {
let target = document.getElementById(this.id);
msg.data.forEach((r) => {
this.buffer.set(r.circuit_id, r);
});
this.buffer.tick();

let results = this.buffer.toArray();
results.sort((a, b) => b.median_tcp_rtt - a.median_tcp_rtt);

let t = TopNTableFromMsgData(msg);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export class TimedLRUCache {
constructor(maxCapacity = 10, timeToLive = 300) { // default TTL is 5 minutes (300 seconds)
this.cache = new Map();
this.maxCapacity = maxCapacity;
this.timeToLive = timeToLive; // in seconds
}

// Add or update an object in the cache
set(key, value) {
if (this.cache.has(key)) {
this.cache.delete(key); // Reset order
}
this.cache.set(key, { value: value, lastAccessed: Date.now() });

// If capacity exceeds max, remove the oldest item
if (this.cache.size > this.maxCapacity) {
const oldestKey = this.cache.keys().next().value;
this.cache.delete(oldestKey);
}
}

// Get an object from the cache (resets time)
get(key) {
if (!this.cache.has(key)) {
return null;
}
const entry = this.cache.get(key);
entry.lastAccessed = Date.now(); // Update access time
this.cache.delete(key);
this.cache.set(key, entry); // Refresh order in the Map
return entry.value;
}

// Tick function to be called externally every second
tick() {
const now = Date.now();
for (const [key, entry] of this.cache) {
const elapsedTime = (now - entry.lastAccessed) / 1000; // convert ms to seconds
if (elapsedTime > this.timeToLive) {
this.cache.delete(key); // Remove expired items
}
}
}

toArray() {
return Array.from(this.cache.values());
}
}

0 comments on commit 61d3c42

Please sign in to comment.