-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: dns to check if cache refreshes and concurrent cache miss
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const dns = require('dns'); | ||
|
||
// Define an array of website hostnames to perform DNS lookups on | ||
const websites = ['google.com', 'example.com', 'github.com']; | ||
|
||
// Create an array to store the promises | ||
const lookupPromises = []; | ||
|
||
async function performLookup(hostname) { | ||
return new Promise((resolve, reject) => { | ||
const startTime = process.hrtime.bigint(); | ||
|
||
dns.lookup(hostname, (err, address, family) => { | ||
if (err) { | ||
console.error(`Error during DNS lookup for ${hostname}:`, err); | ||
return reject(err); | ||
} | ||
|
||
const endTime = process.hrtime.bigint(); | ||
const time = Number(endTime - startTime) / 1e6; // Convert to milliseconds | ||
|
||
const result = { | ||
Hostname: hostname, | ||
Address: address, | ||
Family: family, | ||
TimeMs: time.toFixed(2), | ||
}; | ||
|
||
console.log(`DNS lookup result for ${hostname}: ${address}, family ${family}`); | ||
resolve(result); | ||
}); | ||
}); | ||
} | ||
|
||
console.log('Starting concurrent DNS cache misses for multiple websites (100 times each)'); | ||
|
||
for (let i = 0; i < 100; i++) { | ||
for (const hostname of websites) { | ||
lookupPromises.push(performLookup(hostname)); | ||
} | ||
} | ||
|
||
// Wait for all DNS lookups to complete | ||
Promise.all(lookupPromises) | ||
.then((results) => { | ||
console.log('\nDNS Lookup Results:'); | ||
console.table(results); | ||
}) | ||
.catch((error) => { | ||
console.error('An error occurred:', error); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Test written to check if the refreshing of | ||
// the dns cache implementation works | ||
// The test checks if lookup1(withoutCache) is bigger than lookup2(usingCache) | ||
// then checks if lookup3(cacheExpires) is bigger than lookup2(usingCache) | ||
|
||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const dns = require('node:dns'); | ||
const common = require('../common'); | ||
|
||
const hostname = 'google.com'; | ||
|
||
const startTimeWithoutCache = process.hrtime.bigint(); | ||
console.log('Starting DNS lookup without cache'); | ||
dns.lookup(hostname, common.mustCall((err, address, family) => { | ||
if (err) { | ||
console.error('Error during DNS lookup without cache:', err); | ||
return; | ||
} | ||
assert.strictEqual(typeof address, 'string'); | ||
assert.strictEqual(family, 6); | ||
|
||
const endTimeWithoutCache = process.hrtime.bigint(); | ||
const timeWithoutCache = Number(endTimeWithoutCache - startTimeWithoutCache) / 1e6; | ||
console.log(`Time without cache: ${timeWithoutCache.toFixed(2)} ms`); | ||
|
||
const startTimeWithCache = process.hrtime.bigint(); | ||
dns.lookup(hostname, common.mustCall((err, cachedAddress, cachedFamily) => { | ||
if (err) { | ||
console.error(err); | ||
return; | ||
} | ||
assert.strictEqual(address, cachedAddress); | ||
assert.strictEqual(family, cachedFamily); | ||
|
||
const endTimeWithCache = process.hrtime.bigint(); | ||
const timeWithCache = Number(endTimeWithCache - startTimeWithCache) / 1e6; | ||
|
||
console.log(`Time with cache: ${timeWithCache.toFixed(2)} ms`); | ||
|
||
assert.ok(timeWithCache < timeWithoutCache); | ||
|
||
// 2 second delay (cache clears) | ||
setTimeout(() => { | ||
const startTimeThirdLookup = process.hrtime.bigint(); | ||
console.log('Starting DNS lookup after 2-second delay'); | ||
dns.lookup(hostname, common.mustCall((err, delayedAddress, delayedFamily) => { | ||
if (err) { | ||
console.error('Error during DNS lookup after delay:', err); | ||
return; | ||
} | ||
const endTimeThirdLookup = process.hrtime.bigint(); | ||
const timeThirdLookup = Number(endTimeThirdLookup - startTimeThirdLookup) / 1e6; | ||
console.log(`Time for third lookup: ${timeThirdLookup.toFixed(2)} ms`); | ||
assert.ok(timeThirdLookup > timeWithoutCache); | ||
})); | ||
}, 2000); | ||
|
||
})); | ||
})); |