SimpleDebuggableConnection.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require 'SharedConfigurations.php';
  11. use Predis\Command\CommandInterface;
  12. use Predis\Connection\StreamConnection;
  13. class SimpleDebuggableConnection extends StreamConnection {
  14. private $tstart = 0;
  15. private $debugBuffer = array();
  16. public function connect() {
  17. $this->tstart = microtime(true);
  18. parent::connect();
  19. }
  20. private function storeDebug(CommandInterface $command, $direction) {
  21. $firtsArg = $command->getArgument(0);
  22. $timestamp = round(microtime(true) - $this->tstart, 4);
  23. $debug = $command->getId();
  24. $debug .= isset($firtsArg) ? " $firtsArg " : ' ';
  25. $debug .= "$direction $this";
  26. $debug .= " [{$timestamp}s]";
  27. $this->debugBuffer[] = $debug;
  28. }
  29. public function writeRequest(CommandInterface $command) {
  30. parent::writeRequest($command);
  31. $this->storeDebug($command, '->');
  32. }
  33. public function readResponse(CommandInterface $command) {
  34. $response = parent::readResponse($command);
  35. $this->storeDebug($command, '<-');
  36. return $response;
  37. }
  38. public function getDebugBuffer() {
  39. return $this->debugBuffer;
  40. }
  41. }
  42. $options = array(
  43. 'connections' => array(
  44. 'tcp' => 'SimpleDebuggableConnection',
  45. ),
  46. );
  47. $client = new Predis\Client($single_server, $options);
  48. $client->set('foo', 'bar');
  49. $client->get('foo');
  50. $client->info();
  51. var_export($client->getConnection()->getDebugBuffer());
  52. /* OUTPUT:
  53. array (
  54. 0 => 'SELECT 15 -> 127.0.0.1:6379 [0.0008s]',
  55. 1 => 'SELECT 15 <- 127.0.0.1:6379 [0.001s]',
  56. 2 => 'SET foo -> 127.0.0.1:6379 [0.001s]',
  57. 3 => 'SET foo <- 127.0.0.1:6379 [0.0011s]',
  58. 4 => 'GET foo -> 127.0.0.1:6379 [0.0013s]',
  59. 5 => 'GET foo <- 127.0.0.1:6379 [0.0015s]',
  60. 6 => 'INFO -> 127.0.0.1:6379 [0.0019s]',
  61. 7 => 'INFO <- 127.0.0.1:6379 [0.0022s]',
  62. )
  63. */