spanner_probes.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. require('../vendor/autoload.php');
  3. $_DATABASE = 'projects/grpc-prober-testing/instances/test-instance/databases/test-db';
  4. $_TEST_USERNAME = 'test_username';
  5. function hardAssert($value, $error_message)
  6. {
  7. if (!$value) {
  8. echo $error_message."\n";
  9. exit(1);
  10. }
  11. }
  12. function hardAssertIfStatusOk($status)
  13. {
  14. if ($status->code !== Grpc\STATUS_OK) {
  15. echo "Call did not complete successfully. Status object:\n";
  16. var_dump($status);
  17. exit(1);
  18. }
  19. }
  20. function microtime_float()
  21. {
  22. list($usec, $sec) = explode(" ", microtime());
  23. return ((float)$usec + (float)$sec);
  24. }
  25. /*
  26. Probes to test session related grpc call from Spanner stub.
  27. Includes tests against CreateSession, GetSession, ListSessions, and
  28. DeleteSession of Spanner stub.
  29. Args:
  30. stub: An object of SpannerStub.
  31. metrics: A list of metrics.
  32. */
  33. function sessionManagement($client, &$metrics){
  34. global $_DATABASE;
  35. $createSessionRequest = new Google\Cloud\Spanner\V1\CreateSessionRequest();
  36. $createSessionRequest->setDatabase($_DATABASE);
  37. #Create Session test
  38. #Create
  39. $time_start = microtime_float();
  40. list($session, $status) = $client->CreateSession($createSessionRequest)->wait();
  41. hardAssertIfStatusOk($status);
  42. hardAssert($session !== null, 'Call completed with a null response');
  43. $lantency = (microtime_float()- $time_start) * 1000;
  44. $metrics['create_session_latency_ms'] = $lantency;
  45. #Get Session
  46. $getSessionRequest = new Google\Cloud\Spanner\V1\GetSessionRequest();
  47. $getSessionRequest->setName($session->getName());
  48. $time_start = microtime_float();
  49. $response = $client->GetSession($getSessionRequest);
  50. $response->wait();
  51. $lantency = (microtime_float() - $time_start) * 1000;
  52. $metrics['get_session_latency_ms'] = $lantency;
  53. #List session
  54. $listSessionsRequest = new Google\Cloud\Spanner\V1\ListSessionsRequest();
  55. $listSessionsRequest->setDatabase($_DATABASE);
  56. $time_start = microtime_float();
  57. $response = $client->ListSessions($listSessionsRequest);
  58. $lantency = (microtime_float() - $time_start) * 1000;
  59. $metrics['list_sessions_latency_ms'] = $lantency;
  60. #Delete session
  61. $deleteSessionRequest = new Google\Cloud\Spanner\V1\DeleteSessionRequest();
  62. $deleteSessionRequest->setName($session->getName());
  63. $time_start = microtime_float();
  64. $client->deleteSession($deleteSessionRequest);
  65. $lantency = (microtime_float() - $time_start) * 1000;
  66. $metrics['delete_session_latency_ms'] = $lantency;
  67. }
  68. /*
  69. Probes to test ExecuteSql and ExecuteStreamingSql call from Spanner stub.
  70. Args:
  71. stub: An object of SpannerStub.
  72. metrics: A list of metrics.
  73. */
  74. function executeSql($client, &$metrics){
  75. global $_DATABASE;
  76. $createSessionRequest = new Google\Cloud\Spanner\V1\CreateSessionRequest();
  77. $createSessionRequest->setDatabase($_DATABASE);
  78. list($session, $status) = $client->CreateSession($createSessionRequest)->wait();
  79. hardAssertIfStatusOk($status);
  80. hardAssert($session !== null, 'Call completed with a null response');
  81. # Probing ExecuteSql call
  82. $time_start = microtime_float();
  83. $executeSqlRequest = new Google\Cloud\Spanner\V1\ExecuteSqlRequest();
  84. $executeSqlRequest->setSession($session->getName());
  85. $executeSqlRequest->setSql('select * FROM users');
  86. $result_set = $client->ExecuteSql($executeSqlRequest);
  87. $lantency = (microtime_float() - $time_start) * 1000;
  88. $metrics['execute_sql_latency_ms'] = $lantency;
  89. // TODO: Error check result_set
  90. # Probing ExecuteStreamingSql call
  91. $partial_result_set = $client->ExecuteStreamingSql($executeSqlRequest);
  92. $time_start = microtime_float();
  93. $first_result = array_values($partial_result_set->getMetadata())[0];
  94. $lantency = (microtime_float() - $time_start) * 1000;
  95. $metrics['execute_streaming_sql_latency_ms'] = $lantency;
  96. // TODO: Error Check for sreaming sql first result
  97. $deleteSessionRequest = new Google\Cloud\Spanner\V1\DeleteSessionRequest();
  98. $deleteSessionRequest->setName($session->getName());
  99. $client->deleteSession($deleteSessionRequest);
  100. }
  101. /*
  102. Probe to test Read and StreamingRead grpc call from Spanner stub.
  103. Args:
  104. stub: An object of SpannerStub.
  105. metrics: A list of metrics.
  106. */
  107. function read($client, &$metrics){
  108. global $_DATABASE;
  109. $createSessionRequest = new Google\Cloud\Spanner\V1\CreateSessionRequest();
  110. $createSessionRequest->setDatabase($_DATABASE);
  111. list($session, $status) = $client->CreateSession($createSessionRequest)->wait();
  112. hardAssertIfStatusOk($status);
  113. hardAssert($session !== null, 'Call completed with a null response');
  114. # Probing Read call
  115. $time_start = microtime_float();
  116. $readRequest = new Google\Cloud\Spanner\V1\ReadRequest();
  117. $readRequest->setSession($session->getName());
  118. $readRequest->setTable('users');
  119. $readRequest->setColumns(['username', 'firstname', 'lastname']);
  120. $keyset = new Google\Cloud\Spanner\V1\KeySet();
  121. $keyset->setAll(True);
  122. $readRequest->setKeySet($keyset);
  123. $result_set = $client->Read($readRequest);
  124. $lantency = (microtime_float() - $time_start) * 1000;
  125. $metrics['read_latency_ms'] = $lantency;
  126. // TODO: Error Check for result_set
  127. # Probing StreamingRead call
  128. $partial_result_set = $client->StreamingRead($readRequest);
  129. $time_start = microtime_float();
  130. $first_result = array_values($partial_result_set->getMetadata())[0];
  131. $lantency = (microtime_float() - $time_start) * 1000;
  132. $metrics['streaming_read_latency_ms'] = $lantency;
  133. //TODO: Error Check for streaming read first result
  134. $deleteSessionRequest = new Google\Cloud\Spanner\V1\DeleteSessionRequest();
  135. $deleteSessionRequest->setName($session->getName());
  136. $client->deleteSession($deleteSessionRequest);
  137. }
  138. /*
  139. Probe to test BeginTransaction, Commit and Rollback grpc from Spanner stub.
  140. Args:
  141. stub: An object of SpannerStub.
  142. metrics: A list of metrics.
  143. */
  144. function transaction($client, &$metrics){
  145. global $_DATABASE;
  146. $createSessionRequest = new Google\Cloud\Spanner\V1\CreateSessionRequest();
  147. $createSessionRequest->setDatabase($_DATABASE);
  148. list($session, $status) = $client->CreateSession($createSessionRequest)->wait();
  149. hardAssertIfStatusOk($status);
  150. hardAssert($session !== null, 'Call completed with a null response');
  151. $txn_options = new Google\Cloud\Spanner\V1\TransactionOptions();
  152. $rw = new Google\Cloud\Spanner\V1\TransactionOptions\ReadWrite();
  153. $txn_options->setReadWrite($rw);
  154. $txn_request = new Google\Cloud\Spanner\V1\BeginTransactionRequest();
  155. $txn_request->setSession($session->getName());
  156. $txn_request->setOptions($txn_options);
  157. # Probing BeginTransaction call
  158. $time_start = microtime_float();
  159. list($txn, $status) = $client->BeginTransaction($txn_request)->wait();
  160. $lantency = (microtime_float() - $time_start) * 1000;
  161. $metrics['begin_transaction_latency_ms'] = $lantency;
  162. hardAssertIfStatusOk($status);
  163. hardAssert($txn !== null, 'Call completed with a null response');
  164. # Probing Commit Call
  165. $commit_request = new Google\Cloud\Spanner\V1\CommitRequest();
  166. $commit_request->setSession($session->getName());
  167. $commit_request->setTransactionId($txn->getId());
  168. $time_start = microtime_float();
  169. $client->Commit($commit_request);
  170. $latency = (microtime_float() - $time_start) * 1000;
  171. $metrics['commit_latency_ms'] = $lantency;
  172. # Probing Rollback call
  173. list($txn, $status) = $client->BeginTransaction($txn_request)->wait();
  174. $rollback_request = new Google\Cloud\Spanner\V1\RollbackRequest();
  175. $rollback_request->setSession($session->getName());
  176. $rollback_request->setTransactionId($txn->getId());
  177. hardAssertIfStatusOk($status);
  178. hardAssert($txn !== null, 'Call completed with a null response');
  179. $time_start = microtime_float();
  180. $client->Rollback($rollback_request);
  181. $latency = (microtime_float() - $time_start) * 1000;
  182. $metrics['rollback_latency_ms'] = $latency;
  183. $deleteSessionRequest = new Google\Cloud\Spanner\V1\DeleteSessionRequest();
  184. $deleteSessionRequest->setName($session->getName());
  185. $client->deleteSession($deleteSessionRequest);
  186. }
  187. /*
  188. Probe to test PartitionQuery and PartitionRead grpc call from Spanner stub.
  189. Args:
  190. stub: An object of SpannerStub.
  191. metrics: A list of metrics.
  192. */
  193. function partition($client, &$metrics){
  194. global $_DATABASE;
  195. global $_TEST_USERNAME;
  196. $createSessionRequest = new Google\Cloud\Spanner\V1\CreateSessionRequest();
  197. $createSessionRequest->setDatabase($_DATABASE);
  198. list($session, $status) = $client->CreateSession($createSessionRequest)->wait();
  199. hardAssertIfStatusOk($status);
  200. hardAssert($session !== null, 'Call completed with a null response');
  201. $txn_options = new Google\Cloud\Spanner\V1\TransactionOptions();
  202. $ro = new Google\Cloud\Spanner\V1\TransactionOptions\ReadOnly();
  203. $txn_options->setReadOnly($ro);
  204. $txn_selector = new Google\Cloud\Spanner\V1\TransactionSelector();
  205. $txn_selector->setBegin($txn_options);
  206. #Probing PartitionQuery call
  207. $ptn_query_request = new Google\Cloud\Spanner\V1\PartitionQueryRequest();
  208. $ptn_query_request->setSession($session->getName());
  209. $ptn_query_request->setSql('select * FROM users');
  210. $ptn_query_request->setTransaction($txn_selector);
  211. $time_start = microtime_float();
  212. $client->PartitionQuery($ptn_query_request);
  213. $lantency = (microtime_float() - $time_start) * 1000;
  214. $metrics['partition_query_latency_ms'] = $lantency;
  215. #Probing PartitionRead call
  216. $ptn_read_request = new Google\Cloud\Spanner\V1\PartitionReadRequest();
  217. $ptn_read_request->setSession($session->getName());
  218. $ptn_read_request->setTable('users');
  219. $ptn_read_request->setTransaction($txn_selector);
  220. $keyset = new Google\Cloud\Spanner\V1\KeySet();
  221. $keyset->setAll(True);
  222. $ptn_read_request->setKeySet($keyset);
  223. $ptn_read_request->setColumns(['username', 'firstname', 'lastname']);
  224. $time_start = microtime_float();
  225. $client->PartitionRead($ptn_read_request);
  226. $latency = (microtime_float() - $time_start) * 1000;
  227. $metrics['partition_read_latency_ms'] = $latency;
  228. # Delete Session
  229. $deleteSessionRequest = new Google\Cloud\Spanner\V1\DeleteSessionRequest();
  230. $deleteSessionRequest->setName($session->getName());
  231. $client->deleteSession($deleteSessionRequest);
  232. }
  233. $PROBE_FUNCTIONS = [
  234. 'session_management' => 'sessionManagement',
  235. 'execute_sql' => 'executeSql',
  236. 'read' => 'read',
  237. 'transaction' => 'transaction',
  238. 'partition' => 'partition'
  239. ];
  240. return $PROBE_FUNCTIONS;