Skip to content

Commit

Permalink
test: dns to check if cache refreshes and concurrent cache miss
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitjj committed Sep 11, 2023
1 parent c32745b commit 5b28d66
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/parallel/test-dns-concurrent.js
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);
});
61 changes: 61 additions & 0 deletions test/parallel/test-dns-refresh-cache.js
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);

}));
}));

0 comments on commit 5b28d66

Please sign in to comment.