Skip to content

Commit

Permalink
Use median instead of warmup requests
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahvdAa committed Jun 16, 2024
1 parent cf739ee commit 1cbf464
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ TARGET_URL=https://frumentum.nl
# The interval in seconds to check the URL, defaults to 60
# Set to 0 to only check once
INTERVAL=60
# The number of times to check the URL before conducting the final measurement, defaults to 5
# see the section below for further explanation
WARMUP_COUNT=5
# responsetimer uses the median response time of X requests
REQ_COUNT=5
```

> [!NOTE]
> responsetimer sends a few requests to the URL before conducting the final measurement.
> This is done to ensure actions like DNS lookups do not affect the actual response time.
> The number of requests is determined by the `WARMUP_COUNT` environment variable, and can be set to `0` to completely
> disable the warmup feature.
28 changes: 14 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ const PAGE_ID = process.env.PAGE_ID;
const METRIC_ID = process.env.METRIC_ID;
const TARGET_URL = process.env.TARGET_URL;
const INTERVAL = Number(process.env.INTERVAL ?? '60');
const WARMUP_COUNT = parseInt(process.env.WARMUP_COUNT ?? '5');
const REQ_COUNT = parseInt(process.env.REQ_COUNT ?? '5');

let msg = [];
if (!API_KEY) msg.push('- Missing api key. Please set the API_KEY env variable.');
if (!PAGE_ID) msg.push('- Missing page id. Please set the PAGE_ID env variable.');
if (!METRIC_ID) msg.push('- Missing metric id. Please set the METRIC_ID env variable.');
if (!TARGET_URL) msg.push('- Missing target url. Please set the TARGET_URL env variable.');
if (isNaN(INTERVAL) || INTERVAL < 0) msg.push('- Invalid interval. Please set the INTERVAL env variable to a positive integer.');
if (isNaN(REQ_COUNT) || REQ_COUNT < 1) msg.push('- Invalid request count. Please set the REQ_COUNT env variable to an integer greater than 0.');

if (msg.length !== 0) {
console.error('Encountered one or more configuration errors:')
Expand All @@ -24,20 +26,18 @@ let request = async () => await fetch(TARGET_URL, {
});

let run = async () => {
if (WARMUP_COUNT > 0) {
console.log(`Sending warmup requests to ${TARGET_URL}...`);
for (let i = 0; i < WARMUP_COUNT; i++) {
await request();
performance.now();
}
console.log('Warmed up!');
let times = [];
console.log(`Sending requests to ${TARGET_URL}...`);
for (let i = 1; i <= REQ_COUNT; i++) {
let start = performance.now();
await request();
let time = performance.now() - start;
times.push(time);
console.log(`Request ${i}/${REQ_COUNT} took ${time}ms`);
}
let median = times.sort((a, b) => a - b)[Math.floor(times.length / 2)];

console.log(`Sending requests to ${TARGET_URL}...`);
let start = performance.now();
await request();
let time = performance.now() - start;
console.log(`Done! Took ${time}ms`);
console.log(`Done! Median is ${median}ms`);

console.log('Posting times to statuspage...');
let resp = await fetch(`https://api.statuspage.io/v1/pages/${PAGE_ID}/metrics/${METRIC_ID}/data.json`, {
Expand All @@ -49,7 +49,7 @@ let run = async () => {
body: JSON.stringify({
data: {
timestamp: Math.floor(Date.now() / 1000),
value: time
value: median
}
})
});
Expand Down

0 comments on commit 1cbf464

Please sign in to comment.