TestServer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace loyalsoft;
  3. include_once __DIR__ . '/AppServer.php';
  4. /**
  5. * Description of TestServer
  6. * UT 测试
  7. * @author jgao
  8. */
  9. class TestServer {
  10. //put your code here
  11. /**
  12. * 入口函数专用测试
  13. * @param type $req
  14. */
  15. static public function testApi($req) {
  16. DebugHelper::debug($req);
  17. $app = new AppServer();
  18. $ret = $app->api(new Req($req));
  19. DebugHelper::debug($ret);
  20. }
  21. /**
  22. * 运行环境自检:
  23. * PHP 版本,扩展模块
  24. * redis操作性
  25. * sqldb操作性
  26. */
  27. static function selfTest() {
  28. // 检查gameOnline状态
  29. echoLine("GAME_ONLINE := " . (GAME_ONLINE ? "True" : "False"));
  30. // 检查phpversion
  31. echoLine('PHP version: ' . PHP_VERSION);
  32. if (version_compare(PHP_VERSION, REQUIRED_PHP_VERSION, '<')) {
  33. echo ('<font color="red">This code request at least PHP version ' #
  34. . REQUIRED_PHP_VERSION . '. </font><br/>\n');
  35. }
  36. echoLine("loaded extensions:"); // 已加载的扩展模块信息
  37. echoLine(var_export(get_loaded_extensions(), true)); # 打印
  38. $required = array('iconv', 'json', 'mcrypt', 'SPL', 'pcre', # # 打算做个检查推荐模块的逻辑呢
  39. 'Reflection', 'session', 'zip', 'zlib', 'PDO', 'openssl',
  40. 'curl', 'mbstring', 'mysqli', 'pdo_mysql', 'redis',
  41. 'sockets', 'xdebug');
  42. // 检查redis地址, 可操作性
  43. if (gMem()->set('test-selftest', "self cheking...")) {
  44. echoLine("Redis check ok!");
  45. } else {
  46. echoLine("Redis check failed!");
  47. echoLine(var_export(config::Inst()->nosql, true));
  48. }
  49. // 检查MySQL地址, 可操作性
  50. if (daoInst()->tableExist('tab_token_gift')) {
  51. echoLine("sqldb test ok");
  52. } else {
  53. echoLine("sqldb test failed!");
  54. echoLine(var_export(config::Inst()->paydb), true);
  55. }
  56. }
  57. public function testMemDelete($key) {
  58. $mem = gMem();
  59. $res = $mem->delete($key);
  60. return $res;
  61. }
  62. public function testMemSet($key, $value) {
  63. $mem = gMem();
  64. $res = $mem->set($key, $value);
  65. return $res;
  66. }
  67. public function testMemGet($key) {
  68. $mem = gMem();
  69. $res = $mem->get($key);
  70. return $res;
  71. }
  72. public static function dtCurrent() {
  73. $timezone = date_default_timezone_get();
  74. echoLine($timezone . " " . date('Y-m-d H:i:s'));
  75. echoLine("tsweek: " . TimeUtil::tsWeek());
  76. echoLine("tsday: " . TimeUtil::tsDay());
  77. }
  78. //----------------------------------------------------
  79. public static function efficiencyOfOldLog() {
  80. /**
  81. * 测试结果说明,采用写本地文件的性能还是略高于写redis数据库的.
  82. */
  83. for ($i = 0; $i < 1000; $i++) {
  84. CLog::pay("我们的祖国像花园,花园里花朵真鲜艳.和暖的阳光照耀着我们,每个人的脸上都笑开颜.");
  85. }
  86. }
  87. public function testRedisGet($key) {
  88. $mem = gMem();
  89. $ret = $mem->get($key);
  90. DebugHelper:: var_dump($ret);
  91. $mem->close();
  92. return $ret;
  93. }
  94. /**
  95. * 删除某个key
  96. * @param type $key
  97. */
  98. public function testRedisDel($key) {
  99. $mem = gMem();
  100. $num = $mem->delete($key);
  101. echo $num > 0 ? 'ok' : 'not exist';
  102. $mem->close();
  103. }
  104. public function testRedisLua($key, $value) {
  105. $mem = gMem();
  106. $script = <<<SCR
  107. if redis.call("get",KEYS[1]) == ARGV[1]
  108. then
  109. return redis.call("del",KEYS[1])
  110. else
  111. return 0
  112. end
  113. SCR;
  114. DebugHelper:: var_dump($script);
  115. $ret = $mem->redis->eval($script, 1, $key, $value);
  116. DebugHelper:: var_dump($ret);
  117. $mem->close();
  118. }
  119. public function testBinSearch($v) {
  120. $arr = array(1, 2, 3, 4, 4, 11, 12, 124);
  121. $start = 0;
  122. $end = count($arr) - 1;
  123. while ($start <= $end) {
  124. $index = intval(($start + $end) / 2);
  125. if ($v < $arr[$index]) {
  126. $end = $index - 1;
  127. } elseif ($v > $arr[$index]) {
  128. $start = $index + 1;
  129. } else {
  130. echo($index);
  131. return;
  132. }
  133. }
  134. echo($index);
  135. }
  136. public function tsetIp() {
  137. DebugHelper::debug(HttpUtil::getClientEP());
  138. }
  139. public function testLogfile() {
  140. CLog::warn("日志测试", 'log');
  141. }
  142. }