TestServer.php 4.9 KB

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