|
| 1 | +--- |
| 2 | +title: 'lookup( hostname )' |
| 3 | +description: 'Lookup the IP addresses a DNS name is bound to using system configured DNS servers' |
| 4 | +weight: 30 |
| 5 | +--- |
| 6 | + |
| 7 | +# lookup( hostname ) |
| 8 | + |
| 9 | +The `dns.lookup` function performs DNS resolution using the system's default DNS configuration and returns a promise that resolves to an array of IP addresses. This function is useful, for instance, for testing standard DNS resolution behavior and comparing it with custom DNS server results. |
| 10 | + |
| 11 | +## Parameters |
| 12 | + |
| 13 | +| Parameter | Type | Description | |
| 14 | +| :-------- | :----- | :------------------------------------------------------------------------ | |
| 15 | +| hostname | string | The domain name to resolve (e.g., "example.com", "k6.io") | |
| 16 | + |
| 17 | +## Returns |
| 18 | + |
| 19 | +A promise resolving to an array of strings, where each string is an IP address that the domain name resolves to. The function returns the same IP addresses that would be returned by the system's standard DNS resolution mechanism. |
| 20 | + |
| 21 | +## Emitted metrics |
| 22 | + |
| 23 | +When using `dns.lookup`, the following metrics are automatically generated: |
| 24 | + |
| 25 | +- `dns_lookups`: Counter incremented for each lookup attempt |
| 26 | +- `dns_lookup_duration`: Trend measuring the time taken for DNS lookup |
| 27 | + |
| 28 | +These metrics help you monitor DNS performance using your system's DNS configuration. |
| 29 | + |
| 30 | +## Examples |
| 31 | + |
| 32 | +### Basic lookup |
| 33 | + |
| 34 | +{{< code >}} |
| 35 | + |
| 36 | +<!-- md-k6:skip --> |
| 37 | + |
| 38 | +```javascript |
| 39 | +import dns from 'k6/x/dns'; |
| 40 | + |
| 41 | +export default async function () { |
| 42 | + // Resolve using system DNS servers |
| 43 | + const addresses = await dns.lookup('k6.io'); |
| 44 | + console.log('k6.io resolves to:', addresses); |
| 45 | + // Output: k6.io resolves to: ["104.21.7.127", "172.67.154.74"] |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +{{< /code >}} |
| 50 | + |
| 51 | +### Comparing system vs custom DNS |
| 52 | + |
| 53 | +{{< code >}} |
| 54 | + |
| 55 | +<!-- md-k6:skip --> |
| 56 | + |
| 57 | +```javascript |
| 58 | +import dns from 'k6/x/dns'; |
| 59 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js'; |
| 60 | + |
| 61 | +export default async function () { |
| 62 | + const domain = 'example.com'; |
| 63 | + |
| 64 | + // Get results from system DNS |
| 65 | + const systemResults = await dns.lookup(domain); |
| 66 | + |
| 67 | + // Get results from Google's DNS |
| 68 | + const googleResults = await dns.resolve(domain, 'A', '8.8.8.8:53'); |
| 69 | + |
| 70 | + console.log('System DNS results:', systemResults); |
| 71 | + console.log('Google DNS results:', googleResults); |
| 72 | + |
| 73 | + // Check if both methods return results |
| 74 | + expect(systemResults.length).toBeGreaterThan(0); |
| 75 | + expect(googleResults.length).toBeGreaterThan(0); |
| 76 | + |
| 77 | + // Compare results (they might differ due to different DNS configurations) |
| 78 | + const hasCommonAddress = systemResults.some(ip => googleResults.includes(ip)); |
| 79 | + expect(hasCommonAddress).toBeTruthy(); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +{{< /code >}} |
| 84 | + |
| 85 | +### Testing DNS consistency |
| 86 | + |
| 87 | +{{< code >}} |
| 88 | + |
| 89 | +<!-- md-k6:skip --> |
| 90 | + |
| 91 | +```javascript |
| 92 | +import dns from 'k6/x/dns'; |
| 93 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js'; |
| 94 | + |
| 95 | +export const options = { |
| 96 | + vus: 1, |
| 97 | + iterations: 10, |
| 98 | +}; |
| 99 | + |
| 100 | +export default async function () { |
| 101 | + const domain = 'k6.io'; |
| 102 | + |
| 103 | + try { |
| 104 | + const results = await dns.lookup(domain); |
| 105 | + |
| 106 | + expect(results.length).toBeGreaterThan(0); |
| 107 | + expect(results.every(ip => |
| 108 | + /^\d+\.\d+\.\d+\.\d+$/.test(ip) || /^[0-9a-fA-F:]+$/.test(ip) |
| 109 | + )).toBeTruthy(); |
| 110 | + |
| 111 | + console.log(`Iteration ${__ITER}: ${domain} -> ${results.join(', ')}`); |
| 112 | + } catch (error) { |
| 113 | + console.error('DNS lookup failed:', error); |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +{{< /code >}} |
| 119 | + |
| 120 | +### Load testing with system DNS |
| 121 | + |
| 122 | +{{< code >}} |
| 123 | + |
| 124 | +<!-- md-k6:skip --> |
| 125 | + |
| 126 | +```javascript |
| 127 | +import dns from 'k6/x/dns'; |
| 128 | +import { sleep } from 'k6'; |
| 129 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js'; |
| 130 | +import { Trend, Rate } from 'k6/metrics'; |
| 131 | + |
| 132 | +const lookupDuration = new Trend('dns_lookup_duration_custom'); |
| 133 | +const successRate = new Rate('dns_lookup_success_rate'); |
| 134 | + |
| 135 | +export const options = { |
| 136 | + vus: 10, |
| 137 | + duration: '60s', |
| 138 | +}; |
| 139 | + |
| 140 | +const domains = [ |
| 141 | + 'k6.io', |
| 142 | + 'example.com', |
| 143 | + 'google.com', |
| 144 | + 'github.com', |
| 145 | + 'stackoverflow.com', |
| 146 | +]; |
| 147 | + |
| 148 | +export default async function () { |
| 149 | + const domain = domains[Math.floor(Math.random() * domains.length)]; |
| 150 | + const startTime = Date.now(); |
| 151 | + |
| 152 | + try { |
| 153 | + const results = await dns.lookup(domain); |
| 154 | + const duration = Date.now() - startTime; |
| 155 | + |
| 156 | + lookupDuration.add(duration); |
| 157 | + successRate.add(true); |
| 158 | + |
| 159 | + expect(results.length).toBeGreaterThan(0); |
| 160 | + |
| 161 | + console.log(`${domain} resolved in ${duration}ms to ${results.length} addresses`); |
| 162 | + } catch (error) { |
| 163 | + const duration = Date.now() - startTime; |
| 164 | + lookupDuration.add(duration); |
| 165 | + successRate.add(false); |
| 166 | + |
| 167 | + console.error(`Failed to resolve ${domain}: ${error.message}`); |
| 168 | + } |
| 169 | + |
| 170 | + sleep(1); |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +{{< /code >}} |
| 175 | + |
| 176 | +### Validating DNS configuration |
| 177 | + |
| 178 | +{{< code >}} |
| 179 | + |
| 180 | +<!-- md-k6:skip --> |
| 181 | + |
| 182 | +```javascript |
| 183 | +import dns from 'k6/x/dns'; |
| 184 | +import { expect } from 'https://jslib.k6.io/k6-testing/{{< param "JSLIB_TESTING_VERSION" >}}/index.js'; |
| 185 | + |
| 186 | +export default async function () { |
| 187 | + const testDomains = [ |
| 188 | + 'k6.io', |
| 189 | + 'grafana.com', |
| 190 | + 'example.com', |
| 191 | + ]; |
| 192 | + |
| 193 | + for (const domain of testDomains) { |
| 194 | + try { |
| 195 | + const results = await dns.lookup(domain); |
| 196 | + |
| 197 | + expect(results.length).toBeGreaterThan(0); |
| 198 | + expect(results.every(ip => { |
| 199 | + // Basic IPv4/IPv6 validation |
| 200 | + return /^\d+\.\d+\.\d+\.\d+$/.test(ip) || /^[0-9a-fA-F:]+$/.test(ip); |
| 201 | + })).toBeTruthy(); |
| 202 | + |
| 203 | + console.log(`✓ ${domain}: ${results.join(', ')}`); |
| 204 | + } catch (error) { |
| 205 | + console.error(`✗ ${domain}: ${error.message}`); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
| 209 | +``` |
| 210 | + |
| 211 | +{{< /code >}} |
| 212 | + |
| 213 | +## Error handling |
| 214 | + |
| 215 | +The `lookup` function may throw errors in the following cases: |
| 216 | + |
| 217 | +- Invalid hostname format |
| 218 | +- DNS resolution timeout |
| 219 | +- No DNS servers configured on the system |
| 220 | +- Network connectivity issues |
| 221 | + |
| 222 | +{{< code >}} |
| 223 | + |
| 224 | +<!-- md-k6:skip --> |
| 225 | + |
| 226 | +```javascript |
| 227 | +import dns from 'k6/x/dns'; |
| 228 | + |
| 229 | +export default async function () { |
| 230 | + try { |
| 231 | + const results = await dns.lookup('nonexistent.invalid.domain.test'); |
| 232 | + console.log('Unexpected success:', results); |
| 233 | + } catch (error) { |
| 234 | + console.log('Expected DNS lookup error:', error.message); |
| 235 | + } |
| 236 | + |
| 237 | + // Test with invalid hostname format |
| 238 | + try { |
| 239 | + const results = await dns.lookup(''); |
| 240 | + console.log('Unexpected success with empty hostname:', results); |
| 241 | + } catch (error) { |
| 242 | + console.log('Expected error for empty hostname:', error.message); |
| 243 | + } |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +{{< /code >}} |
0 commit comments