Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.

Documentationbreadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/x/icmpbreadcrumb arrow pingAsync( target, [optsOrCallback], [callback] )
Open source

pingAsync( target, [optsOrCallback], [callback] )

Sends ICMP echo requests (pings) to the specified target asynchronously.

Signature

JavaScript
await pingAsync(target, optsOrCallback, callback)

Parameters

ParameterTypeDescription
targetstringHostname or IP address to ping.
optsOrCallbackPingOptions | PingCallbackOptional ping options or callback function.
callbackPingCallbackOptional callback function for ping results.

Returns

TypeDescription
Promise<boolean>Promise that resolves to true if the number of successful pings is greater than or equal to the threshold, otherwise false.

Example

JavaScript
import { pingAsync } from "k6/x/icmp"

export default async function () {
  const host = "8.8.8.8"

  console.log(`Pinging ${host} with callback:`);

  const opts = {
    timeout: 1000,
    retries: 2,
    count: 5
  };

  const result = await pingAsync(host, opts, (err, { target, sent_at, received_at, seq, ttl, size, options }) => {
    if (err) {
      console.error(`${target}: ${err}`);

      return
    }

    const rtt = received_at - sent_at;

    console.log(`${size} bytes from ${target}: icmp_seq=${seq} ttl=${ttl} time=${rtt} ms`);
  });

  if (result) {
    console.log(`Host ${host} is reachable`);
  } else {
    console.error(`Host ${host} is unreachable`);
  }
}