benchmark.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. require_once(__DIR__.'/../vendor/autoload.php');
  3. use Neoxygen\NeoClient\ClientBuilder;
  4. function convert($size)
  5. {
  6. $unit=array('b','kb','mb','gb','tb','pb');
  7. return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
  8. }
  9. $outputfile = __DIR__.'/results.txt';
  10. $output = '';
  11. $output .= 'Benchmarking client instantation without cache, with result formatter enabled, 1000 runs'."\n";
  12. $start = microtime(true);
  13. $i = 0;
  14. while ($i < 1000) {
  15. $client = ClientBuilder::create()
  16. ->setAutoFormatResponse(true)
  17. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  18. ->build();
  19. $i++;
  20. }
  21. $usage = convert(memory_get_peak_usage(true));
  22. $end = microtime(true);
  23. $diff = $end - $start;
  24. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage);
  25. $output .= PHP_EOL.'--------------------------'.PHP_EOL;
  26. $client = null;
  27. $i = null;
  28. // ----- Client Instantation with DI cache
  29. $output .= 'Benchmarking client instatation with cache enabled, 1000 runs'.PHP_EOL;
  30. $start = microtime(true);
  31. $i = 0;
  32. while ($i < 1000) {
  33. $client = ClientBuilder::create()
  34. ->enableCache(__DIR__.'/../cache/')
  35. ->setAutoFormatResponse(true)
  36. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  37. ->build();
  38. $i++;
  39. }
  40. $usage = convert(memory_get_peak_usage(true));
  41. $end = microtime(true);
  42. $diff = $end - $start;
  43. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage);
  44. $output .= PHP_EOL.'--------------------------'.PHP_EOL;
  45. $client = null;
  46. $i = null;
  47. // ---------- Running 1000 statements with immediate tx commits
  48. $output .= 'Running 1000 statements with immediate tx commits'.PHP_EOL;
  49. $client = ClientBuilder::create()
  50. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  51. ->setAutoFormatResponse(true)
  52. ->build();
  53. $start = microtime(true);
  54. for ($i=0; $i < 1000; $i++) {
  55. $q = 'CREATE (n:Benchmark {tx_id:{id}})';
  56. $p = ['id' => $i];
  57. $client->sendCypherQuery($q, $p);
  58. }
  59. $end = microtime(true);
  60. $usage = convert(memory_get_peak_usage(true));
  61. $diff = $end - $start;
  62. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage).PHP_EOL;
  63. $output .= '--------------------------'.PHP_EOL;
  64. $client = null;
  65. $i = null;
  66. $p = null;
  67. $q = null;
  68. // ---------- Running 1000 statements with immediate tx commits
  69. $output .= 'Running 1000 statements in one transaction commit, separate requests using the TransactionManager'.PHP_EOL;
  70. $client = ClientBuilder::create()
  71. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  72. ->setAutoFormatResponse(true)
  73. ->build();
  74. $start = microtime(true);
  75. $tx = $client->createTransaction();
  76. for ($i=0; $i < 1000; $i++) {
  77. $q = 'CREATE (n:Benchmark {tx_id:{id}})';
  78. $p = ['id' => $i];
  79. $tx->pushQuery($q, $p);
  80. }
  81. $tx->commit();
  82. $end = microtime(true);
  83. $usage = convert(memory_get_peak_usage(true));
  84. $diff = $end - $start;
  85. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage).PHP_EOL;
  86. $output .= '--------------------------'.PHP_EOL;
  87. $client = null;
  88. $i = null;
  89. $p = null;
  90. $q = null;
  91. // ---------- Running 1000 statements with immediate tx commits
  92. $output .= 'Running 1000 statements in one transaction commit, same request'.PHP_EOL;
  93. $client = ClientBuilder::create()
  94. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  95. //->setAutoFormatResponse(true)
  96. ->build();
  97. $start = microtime(true);
  98. $statements = [];
  99. for ($i=0; $i < 1000; $i++) {
  100. $q = 'CREATE (n {tx_id:{id}})';
  101. $p = ['id' => $i];
  102. $st = [
  103. 'statement' => $q,
  104. 'parameters' => $p
  105. ];
  106. $statements[] = $st;
  107. }
  108. $client->sendMultiple($statements);
  109. $end = microtime(true);
  110. $usage = convert(memory_get_peak_usage(true));
  111. $diff = $end - $start;
  112. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage).PHP_EOL;
  113. $output .= '--------------------------'.PHP_EOL;
  114. $client = null;
  115. $i = null;
  116. $p = null;
  117. $q = null;
  118. // ----- Handling big graph response format
  119. $output .= 'Handling big graph Response format, 1000 nodes with more edges'.PHP_EOL;
  120. $client = ClientBuilder::create()
  121. ->addConnection('default', 'http', 'localhost', 7474, true, 'neo4j', 'veryCoolMax')
  122. ->setAutoFormatResponse(true)
  123. ->build();
  124. $client->sendCypherQuery('MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE r,n');
  125. $start = microtime(true);
  126. $q = 'MATCH (n) OPTIONAL MATCH (n)-[r]-() RETURN r,n LIMIT 1000';
  127. $r = $client->sendCypherQuery($q)->getResult();
  128. $end = microtime(true);
  129. $usage = convert(memory_get_peak_usage(true));
  130. $diff = $end - $start;
  131. $output .= sprintf('Runned in %s seconds, using %s memory', $diff, $usage).PHP_EOL;
  132. $output .= '--------------------------'.PHP_EOL;
  133. file_put_contents($outputfile, $output);