AsyncMysqlConnectionPool::getPoolStats

Returns statistical information for the current pool.

Description

public function getPoolStats(): array

Returns statistical information for the current pool.

Information provided includes the number of pool connections that were created and destroyed, how many connections were requested, and how many times the pool was hit or missed when creating the connection. The returned array keys are:

  • created_pool_connections
  • destroyed_pool_connections
  • connections_requested
  • pool_hits
  • pool_misses

Return Values

  • array - A string-keyed array with the statistical information above.

Examples

The following example shows how to gather AsyncMySqlConnectionPool statistics using its getStats() method. The statistics that are gathered are connection statistics.

<?hh

namespace Hack\UserDocumentation\API\Examples\AsyncMysql\ConnPool\Stats;

use \Hack\UserDocumentation\API\Examples\AsyncMysql\ConnectionInfo as CI;


function set_connection_pool(array<string, mixed> $options = array()):
  \AsyncMysqlConnectionPool {
  return new \AsyncMysqlConnectionPool($options);
}

async function connect_with_pool(\AsyncMysqlConnectionPool $pool):
  Awaitable<\AsyncMysqlConnection> {
  return await $pool->connect(
    CI::$host,
    CI::$port,
    CI::$db,
    CI::$user,
    CI::$passwd
  );
}

function get_stats(\AsyncMysqlConnectionPool $pool): array<mixed> {
  return $pool->getPoolStats();
}

function run_it(): void {
  $pool = set_connection_pool();
  $conn_awaitables = Vector {};
  $conn_awaitables[] = connect_with_pool($pool);
  $conn_awaitables[] = connect_with_pool($pool);
  $conn_awaitables[] = connect_with_pool($pool);
  $conns = \HH\Asio\join(\HH\Asio\v($conn_awaitables));
  // Get pool connection stats, like pool connections created, how many
  // connections were requested, etc.
  var_dump(get_stats($pool));
}

run_it();
Output
array(5) {
  ["created_pool_connections"]=>
  int(3)
  ["destroyed_pool_connections"]=>
  int(0)
  ["connections_requested"]=>
  int(3)
  ["pool_hits"]=>
  int(0)
  ["pool_misses"]=>
  int(3)
}