From 5b28d66ce8d8713f3e374ed7d14946c8b59a08f7 Mon Sep 17 00:00:00 2001 From: kshitjj Date: Mon, 11 Sep 2023 22:27:25 +0530 Subject: [PATCH] test: dns to check if cache refreshes and concurrent cache miss --- test/parallel/test-dns-concurrent.js | 54 ++++++++++++++++++++++ test/parallel/test-dns-refresh-cache.js | 61 +++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 test/parallel/test-dns-concurrent.js create mode 100644 test/parallel/test-dns-refresh-cache.js diff --git a/test/parallel/test-dns-concurrent.js b/test/parallel/test-dns-concurrent.js new file mode 100644 index 00000000000000..93edd834a85581 --- /dev/null +++ b/test/parallel/test-dns-concurrent.js @@ -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); + }); diff --git a/test/parallel/test-dns-refresh-cache.js b/test/parallel/test-dns-refresh-cache.js new file mode 100644 index 00000000000000..b486a327b90087 --- /dev/null +++ b/test/parallel/test-dns-refresh-cache.js @@ -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); + + })); +})); \ No newline at end of file