PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/test/tests.php

https://github.com/iwonnor/scribe
PHP | 365 lines | 321 code | 28 blank | 16 comment | 5 complexity | d2d72bbe335e2426a6d95ea18837b6fb MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. // Copyright (c) 2007-2008 Facebook
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // See accompanying file LICENSE or visit the Scribe site at:
  17. // http://developers.facebook.com/scribe/
  18. $GLOBALS['THRIFT_ROOT'] = '/usr/local/thrift/php/thrift';
  19. $GLOBALS['SCRIBE_ROOT'] = '/usr/local/thrift/php/thrift/packages';
  20. include_once $GLOBALS['SCRIBE_ROOT'].'/scribe.php';
  21. include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
  22. include_once $GLOBALS['THRIFT_ROOT'].'/transport/TFramedTransport.php';
  23. include_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php';
  24. function simple_test() {
  25. $messages = array();
  26. $msg = new LogEntry;
  27. $msg->category = 'scribe_test';
  28. $msg->message = "this is a message\n";
  29. $messages []= $msg;
  30. $msg2 = new LogEntry;
  31. $msg2->category = 'scribe_test';
  32. $msg2->message = "and a binary" . chr(0) . chr(1) . " message\n";
  33. $messages []= $msg2;
  34. $msg3 = new LogEntry;
  35. $msg3->category = 'buckettest';
  36. $msg3->message = '99 a key-value message with a non-printable delimiter\n';
  37. $messages []= $msg3;
  38. $msg4 = new LogEntry;
  39. $msg4->category = 'buckettest';
  40. $msg4->message = '99 a different message in the same bucket\n';
  41. $messages []= $msg4;
  42. $msg5 = new LogEntry;
  43. $msg5->category = 'buckettest';
  44. $msg5->message = '98 a different bucket\n';
  45. $messages []= $msg5;
  46. $scribe_client = create_scribe_client();
  47. $ret = scribe_Log_test($messages, $scribe_client);
  48. print "Log returned: " . $ret . "\n";
  49. }
  50. function bucket_test() {
  51. $messages = array();
  52. $msg = new LogEntry;
  53. $msg->category = 'scribe_test';
  54. $msg->message = "this is a message\n";
  55. $messages []= $msg;
  56. $msg2 = new LogEntry;
  57. $msg2->category = 'scribe_test';
  58. $msg2->message = "and a binary" . chr(0) . chr(1) . " message\n";
  59. $messages []= $msg2;
  60. $msg3 = new LogEntry;
  61. $msg3->category = 'buckettest2';
  62. $msg3->message = '99' . chr(1) . 'a key-value message with a non-printable delimiter\n';
  63. $messages []= $msg3;
  64. $msg4 = new LogEntry;
  65. $msg4->category = 'buckettest2';
  66. $msg4->message = '99' . chr(1) . 'a different message in the same bucket\n';
  67. $messages []= $msg4;
  68. $msg5 = new LogEntry;
  69. $msg5->category = 'buckettest2';
  70. $msg5->message = '98' . chr(1) . 'a different bucket\n';
  71. $messages []= $msg5;
  72. $msg6 = new LogEntry;
  73. $msg6->category = 'buckettest2';
  74. $msg6->message = '97' . chr(1) . 'a different bucket\n';
  75. $messages []= $msg6;
  76. $msg7 = new LogEntry;
  77. $msg7->category = 'buckettest2';
  78. $msg7->message = '96' . chr(1) . 'a different bucket\n';
  79. $messages []= $msg7;
  80. $scribe_client = create_scribe_client();
  81. $ret = scribe_Log_test($messages, $scribe_client);
  82. print "Log returned: " . $ret . "\n";
  83. }
  84. function strange_input_test() {
  85. $messages = array();
  86. $msg = new LogEntry;
  87. $msg->category = '%xa\n\ndfsdfjfdsjlkasjlkjerl%slkjasdf%dlkjasdlkjf\\\\\\\\\\\\\adskljasdl;kjsg[pa;lksdkjmdkjfkjfkjkdjfslkkjlaasdfasdfasdfasdfasdfasdflkhjlaksjdlkfjalksjdflkjasdflkjsdaflkjsdflkjadsflkjjsadflkkjsdflkjjsdfalkjfdsakljfsdaljk';
  88. $msg->message = '%xlasdlkfjalskjlkjasdklg\\\\\\\/////\\\/\\//\\;klf;klds;klfsdaflsk;kl;lk;sfkl;sdf%d<><>><><<>>l;kadsl;kadsl;k;klkl;fsdal;ksdfa;klsaf;lsdfl;kfsdl;ksdflksdaf;lkfds;lkfsd;lksdafl;kf;klsflk;sdf;klsdfka;lskdl;fkls;dfalk;fsdl;ksfadkl;sfdlk;sfadlk;fsld;kasflkad;klfsad;lksdfal;ksfda;lksdaflk;sdfal;kl;sdfakl;sdaf;klsdfa;klsdafk;lsdfakl;sdafkl;sdfak;lasdfkl;sdaflk;sdaflk;sdafkl;sadf;lksdafl;ksdaf;klsdafl;ksdafl;ksdfak;lsdafkl;sdfl;kdsfakl;sdaf;lkdsafk;lsdfkl;sdfakl;fdsa;klfsdk;lasfdk;lfsdakl;sdfak;lfsdakl;sdfakl;sfdak;lsdfaklfdsakl;sdfak;lsfdak;lsfdakl;sfdakl;sdfak;lsdfak;lsdfak;lsdfak;lsfdakl;sdfak;lsdfakl;sdfak;lsdfakl;sdfk;lfds;alkadfsk;lsdfak;ldfsak;lsdfa;klsfdakl;sdfak;lsdfakl;sdfak;lsdfak;lsdf;klsdfa;klsdfak;lsdfak;lfsdakl;sdfakl;fdsak;lsdfak;lsdfakl;sdfak;lsdfak;lsdfa;klsdfa;klsfdk;alsfadkl;sdfakl;sdfkl;fdsakl;sfdal;ksdfak;las;lkfsda;lksdfak;lsdfak;lsdfakl;kfds;lk;l;sdf;klfdsl;ksdfal;ksdl;kds;lksdkl;dsfal;kdsfak;ldsfa;kldfasl;kdfasl;kdafsl;kadfskl;dfsa;kldfsak;ldfsakl;dfask;ldfsak;ldfsal;kdfsakl;dfsak;ldfsak;k;dlfsa;lkadsf;kladsf;lkdfsa;lkdsaf;k;lkdfsakl;dfsal;kdfsa;kldsaf;lkdfsa;kldasf;kldfas;kldsaf;klasdfk;lsadf;klsdafk;ldsaf;lkasdfk;ldfas;kladfs;kldfaskl;dfsa;kldfsakl;dfsalk;dfska;kladsfk;ladfs;kladsfkl;adssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n\n\nssssssssssssssssssssssssssssssf';
  89. $messages []= $msg;
  90. $msg2 = new LogEntry;
  91. $msg2->category = 'scribe_test';
  92. $msg2->message = '\n';
  93. $messages []= $msg2;
  94. $msg3 = new LogEntry;
  95. $msg3->category = 'scribe_test';
  96. $msg3->message = '';
  97. $messages [] = $msg3;
  98. $scribe_client = create_scribe_client();
  99. $ret = scribe_Log_test($messages, $scribe_client);
  100. print "Log returned: " . $ret . "\n";
  101. }
  102. function stress_test($category, $client_name, $rate, $total, $msg_per_call,
  103. $avg_size, $num_categories) {
  104. $send_interval = $msg_per_call/$rate;
  105. $random = generate_random($avg_size * 2);
  106. $msgs_since_send = 0;
  107. $messages = array();
  108. $last_send_time = microtime(true);
  109. $scribe_client = create_scribe_client();
  110. for($i = 0; $i < $total; ++$i) {
  111. $entry = new LogEntry;
  112. $entry->category = $category;
  113. if ($num_categories > 1) {
  114. $entry->category .= rand(1, $num_categories);
  115. }
  116. $entry->message = make_message($client_name, $avg_size, $i, $random);
  117. $messages []= $entry;
  118. ++$msgs_since_send;
  119. if ($msgs_since_send >= $msg_per_call) {
  120. $msgs_since_send = 0;
  121. $ret = scribe_Log_test($messages, $scribe_client);
  122. $messages = array();
  123. $now = microtime(true);
  124. $wait = $last_send_time + $send_interval - $now;
  125. $last_send_time = $now;
  126. if ($wait > 0) {
  127. usleep($wait * 1000000);
  128. }
  129. }
  130. }
  131. }
  132. function many_connections_test($category, $client_name, $num_connections, $rate,
  133. $total, $msg_per_call, $avg_size) {
  134. if ($num_connections < 1) {
  135. print("you can't run the test with $num_connections connections\n");
  136. }
  137. if ($rate % $num_connections != 0 ||
  138. $total % $num_connections != 0 ||
  139. ($total/$num_connections) % $msg_per_call != 0) {
  140. print("Arguments don't divide evenly, so number of messages won't be accurate\n");
  141. }
  142. $rate_per_conn = $rate/$num_connections;
  143. $send_interval = $msg_per_call/$rate_per_conn;
  144. // open all the connections. This bypasses the normal client library because
  145. // we want to open a lot of connections simultaneously, which is something
  146. // a real client would never need to do.
  147. //
  148. $server_ips = array('localhost');
  149. $port = 1463;
  150. $socks = array();
  151. $trans = array();
  152. $prots = array();
  153. $scribes = array();
  154. $opened = array();
  155. try {
  156. for ($i = 0; $i < $num_connections; ++$i) {
  157. $socks[$i] = new TSocketPool($server_ips, $port);
  158. $socks[$i]->setDebug(1);
  159. $socks[$i]->setSendTimeout(2500);
  160. $socks[$i]->setRecvTimeout(2500);
  161. $socks[$i]->setNumRetries(1);
  162. $socks[$i]->setRetryInterval(30);
  163. $socks[$i]->setRandomize(true);
  164. $socks[$i]->setMaxConsecutiveFailures(2);
  165. $socks[$i]->setAlwaysTryLast(false);
  166. $trans[$i] = new TFramedTransport($socks[$i], 1024, 1024);
  167. $prots[$i] = new TBinaryProtocol($trans[$i]);
  168. $scribes[$i] = new scribeClient($prots[$i]);
  169. $trans[$i]->open();
  170. $opened[$i] = true;
  171. }
  172. } catch (Exception $x) {
  173. print "exception opening connection $i";
  174. // Bummer too bad so sad
  175. return;
  176. }
  177. // Send the messages, a few from each connection every loop
  178. //
  179. $random = generate_random($avg_size * 2);
  180. $last_send_time = microtime(true);
  181. $i = 0;
  182. while ($i < $total) {
  183. $messages = array();
  184. for ($conn = 0; $conn < $num_connections; ++$conn) {
  185. for ($j = 0; $j < $msg_per_call; ++$j) {
  186. $entry = new LogEntry;
  187. $entry->category = $category;
  188. $entry->message = make_message($client_name, $avg_size, $i, $random);
  189. $messages []= $entry;
  190. ++$i;
  191. }
  192. $result = $scribes[$conn]->Log($messages);
  193. if ($result <> OK) {
  194. print "Warning: Log returned $result \n";
  195. }
  196. $messages = array();
  197. }
  198. $now = microtime(true);
  199. $wait = $last_send_time + $send_interval - $now;
  200. $last_send_time = $now;
  201. if ($wait > 0) {
  202. usleep($wait * 1000000);
  203. }
  204. }
  205. // Close the connections
  206. //
  207. for ($i = 0; $i < $num_connections; ++$i) {
  208. if ($opened[$i]) {
  209. try {
  210. $trans[$i]->close();
  211. } catch (Exception $x) {
  212. print "exception closing connection $i";
  213. // Ignore close errors
  214. }
  215. }
  216. }
  217. }
  218. function generate_random($size) {
  219. $random = 'qwertyiopui%sfmsg;lmad;lkh[pwermflvps/w]slkbb;k,mtjasdlkjfaslkjdfflkjasdfkljaslkdjgfacebookqweetewatab';
  220. while ($random_len = strlen($random) < $size) {
  221. $random .= $random;
  222. }
  223. return $random;
  224. }
  225. function make_message($client_name, $avg_size, $sequence, $random) {
  226. $padding_size = $avg_size - strlen($client_name) - 10;
  227. if ($padding_size > 0) {
  228. // TODO: we could make this fancier with pseudo-random sizes
  229. $padding_size = $sequence % ($padding_size * 2);
  230. } else {
  231. $padding_size = 0;
  232. }
  233. $message = $client_name . '-' . $sequence;
  234. if ($padding_size) {
  235. $message .= '-' . $padding_size . '-';
  236. $message .= substr($random, 0, $padding_size);
  237. }
  238. $message .= "\n";
  239. return $message;
  240. }
  241. function create_scribe_client() {
  242. try {
  243. // Set up the socket connections
  244. $scribe_servers = array('localhost');
  245. $scribe_ports = array(1463);
  246. print "creating socket pool\n";
  247. $sock = new TSocketPool($scribe_servers, $scribe_ports);
  248. $sock->setDebug(0);
  249. $sock->setSendTimeout(1000);
  250. $sock->setRecvTimeout(2500);
  251. $sock->setNumRetries(1);
  252. $sock->setRandomize(false);
  253. $sock->setAlwaysTryLast(true);
  254. $trans = new TFramedTransport($sock);
  255. $prot = new TBinaryProtocol($trans);
  256. // Create the client
  257. print "creating scribe client\n";
  258. $scribe_client = new scribeClient($prot);
  259. // Open the transport (we rely on PHP to close it at script termination)
  260. print "opening transport\n";
  261. $trans->open();
  262. } catch (Exception $x) {
  263. print "Unable to create global scribe client, received exception: $x \n";
  264. return null;
  265. }
  266. return $scribe_client;
  267. }
  268. function scribe_Log_test($messages, $scribe_client) {
  269. try {
  270. $result = $scribe_client->Log($messages);
  271. if ($result <> OK) {
  272. print "Warning: Log returned $result \n";
  273. }
  274. return $result;
  275. } catch (Exception $x) {
  276. print "Scribe client failed logging " . count($messages) .
  277. " messages with exception: $x \n";
  278. }
  279. }
  280. function super_stress_test($categories, $client_name, $rate, $total,
  281. $msg_per_call, $avg_size, $category_multiplier) {
  282. $pids = array();
  283. // Fork a new process for every category
  284. foreach ($categories as $category) {
  285. $pid = pcntl_fork();
  286. if($pid == -1) {
  287. print "Error: Could not fork\n";
  288. return;
  289. }
  290. else if($pid == 0) {
  291. // In child process
  292. print "Sending messages for category $category...\n";
  293. stress_test($category, $client_name, $rate, $total,
  294. $msg_per_call, $avg_size, $category_multiplier);
  295. print "Done sending messages for category $category.\n";
  296. Exit(0);
  297. } else {
  298. // In parent process
  299. $pids[] = $pid;
  300. }
  301. }
  302. // have parent wait for all children
  303. foreach ($pids as $pid) {
  304. pcntl_waitpid($pid, $status);
  305. }
  306. }
  307. ?>