TestRelationHelper.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. /**
  4. * Created by PhpStorm.
  5. * User: stanley-king
  6. * Date: 16/6/23
  7. * Time: 下午10:21
  8. */
  9. define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
  10. require_once(BASE_ROOT_PATH . '/global.php');
  11. require_once(BASE_ROOT_PATH . '/fooder.php');
  12. require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
  13. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  14. require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
  15. class testRelationHelper extends TestCase
  16. {
  17. public static function setUpBeforeClass(): void
  18. {
  19. Base::run_util();
  20. }
  21. public function testUpContactsA()
  22. {
  23. //36490
  24. $contacts = array('13911129868','13911129869','13911129870','13911129871','13911129867','15618951806','18612396521');
  25. relation_helper::onUpContacts(36490,'13911129867','江海苗',$contacts);
  26. }
  27. public function testUpContactsB()
  28. {
  29. //36490
  30. $contacts = array('13911129867','13911129869','13911129870','13911129872');
  31. relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
  32. }
  33. public function testOnLogin()
  34. {
  35. relation_helper::onLogin(36490);
  36. relation_helper::onLogin(36511);
  37. }
  38. public function testUpContactsC()
  39. {
  40. try
  41. {
  42. $contacts = '%5B%22178-0211-8910%22%2C%22182-6227-3057%22%2C%22189-6195-0030%22%5D';
  43. $contacts = session_helper::parse_contacts($contacts);
  44. relation_helper::onUpContacts(36511,'18612396521','秦梦洁',$contacts);
  45. } catch (Exception $ex) {
  46. echo $ex->getMessage();
  47. }
  48. }
  49. public function testMakeType()
  50. {
  51. $reg = '/make_type=(\d+)/i';
  52. $ret = preg_match($reg, '发送红包,红包单号: xxxxxx make_type=6', $arr);
  53. }
  54. public function testFollower()
  55. {
  56. $follower = relation_helper::follower(36490);
  57. if(!empty($follower)) {
  58. //$follower = self::separate_page($follower,$pages);
  59. $members = Model('member')->field('*')->where(array('member_id' => array('in',$follower)))->limit(false)->select();
  60. $desc = array();
  61. foreach ($members as $val) {
  62. $info = new member_info($val);
  63. }
  64. //return self::outsuccess(array('followers' => $follower,'mem_desc' => , 'mobile_page' => mobile_page($pages)));
  65. }
  66. }
  67. public function testDate()
  68. {
  69. $title = strftime("熊猫美妆%m月%d日红包风云榜",1469030400);
  70. }
  71. public function testSort()
  72. {
  73. $mod = Model('member_relation');
  74. $item = $mod->field('*')->where(array('member_mobile' => '13911129867'))->find();
  75. $mobiles = unserialize($item['unbuild_mobiles']);
  76. // perfor_start();
  77. // for($i = 0; $i < 10000; ++$i) {
  78. // sort($mobiles);
  79. // }
  80. // perfor_end("testSort","string sort");
  81. foreach ($mobiles as $mobile) {
  82. $iMobiles[] = intval($mobile);
  83. }
  84. perfor_start();
  85. for($i = 0; $i < 10000; ++$i) {
  86. sort($iMobiles);
  87. }
  88. perfor_end("testSort","int sort");
  89. $len = count($iMobiles);
  90. perfor_start();
  91. for($i = 0; $i < 10000; ++$i) {
  92. $pos = $i % $len;
  93. $val = $iMobiles[$pos];
  94. //algorithm::array_erase($iMobiles,$pos);
  95. $pos = algorithm::lower_bonud($iMobiles,$val);
  96. //algorithm::array_insert($iMobiles,$pos,$val);
  97. }
  98. perfor_end("testSort","int sort");
  99. echo perfor_log();
  100. }
  101. public function testArrayInsert()
  102. {
  103. $x = array(1,3,5,9,10);
  104. $pos = algorithm::lower_bonud($x,3);
  105. $posx = algorithm::upper_bound($x,3);
  106. $y = algorithm::array_insert($x,$pos,0);
  107. $pos = algorithm::lower_bonud($x,4);
  108. $posx = algorithm::upper_bound($x,4);
  109. //$find = algorithm_helper::binary_search($x,0);
  110. }
  111. public function testLowerbound()
  112. {
  113. //$x = array(1,3,5,9,10);
  114. // $x = array(39625);
  115. $x = ['0','1','2'];
  116. $find = algorithm::binary_search($x,1);
  117. // $find = algorithm::binary_search($x,0);
  118. // $find = algorithm::binary_search($x,39625);
  119. // $pos = algorithm::lower_bonud($x,39625);
  120. // $pos = algorithm::lower_bonud($x,2);
  121. // $pos = algorithm::lower_bonud($x,3);
  122. // $pos = algorithm::lower_bonud($x,14);
  123. }
  124. public function testUpper()
  125. {
  126. $x = array(1,3,5,9,10);
  127. $pos = algorithm::upper_bound($x,0);
  128. $pos = algorithm::upper_bound($x,2);
  129. $pos = algorithm::upper_bound($x,3);
  130. $pos = algorithm::upper_bound($x,14);
  131. }
  132. public function testRelation()
  133. {
  134. perfor_start();
  135. for($i = 0; $i < 10000; ++$i) {
  136. $mod = Model('member_relation');
  137. $mod->findByMobile('13911129867');
  138. }
  139. perfor_end("testRelation","10000 findByMobile");
  140. $mod = Model('member_relation');
  141. $item = $mod->findByMobile('13911129867');
  142. perfor_start();
  143. for($i = 0; $i < 10000; ++$i) {
  144. unserialize($item['unbuild_mobiles']);
  145. }
  146. perfor_end("testRelation","10000 unserialize");
  147. perfor_start();
  148. for($i = 0; $i < 10000; ++$i) {
  149. $relation = new \relation\mem_relation(36490);
  150. //$relation->pass_subscribe();
  151. }
  152. perfor_end("testRelation","10000 pass_subscribe");
  153. echo perfor_log();
  154. }
  155. public function testInitInvitors()
  156. {
  157. $path = BASE_DATA_PATH . "/mobile/invitors.txt";
  158. $file = fopen($path,'r');
  159. if(!is_resource($file)) {
  160. Log::record(__METHOD__ . " file is not exist",Log::ERR);
  161. return false;
  162. }
  163. while(!feof($file)) {
  164. $line = fgets($file);
  165. $kv = preg_split('/,/',$line);
  166. if(count($kv) != 2) continue;
  167. $key = trim($kv[0]);
  168. $val = trim($kv[1]);
  169. $this->update_invitor($key,$val);
  170. }
  171. fclose($file);
  172. }
  173. private function update_invitor($invitor,$user)
  174. {
  175. if(empty($invitor)) return false;
  176. $mod = Model('member');
  177. $invitor_info = $mod->where(['member_mobile' => $invitor])->find();
  178. if(empty($invitor_info)) return false;
  179. $member_id = intval($invitor_info['member_id']);
  180. $user_info = $mod->where(['member_mobile' => $user])->find();
  181. if(empty($user_info)) return false;
  182. $inviter_id = intval($user_info['inviter_id']);
  183. if($inviter_id > 0) return false;
  184. $ret = $mod->where(['member_mobile' => $user])->update(['inviter_id' => $member_id]);
  185. if($ret == false) {
  186. Log::record(__METHOD__ . " update {$user} invitor id err",Log::ERR);
  187. }
  188. else {
  189. Log::record(__METHOD__ . " update {$user} invitor id success",Log::DEBUG);
  190. }
  191. }
  192. public function testSeralize()
  193. {
  194. $stdout = fopen('php://stdout', 'w');
  195. $x = 'a:97:{i:0;i:36486;i:1;i:36487;i:2;i:36492;i:3;i:36628;i:4;i:37214;i:5;i:37234;i:6;i:37378;i:7;i:37623;i:8;i:37629;i:9;i:38606;i:10;i:38610;i:11;i:38647;i:12;i:38671;i:13;i:39086;i:14;i:39090;i:15;i:39187;i:16;i:39262;i:17;i:39285;i:18;i:39286;i:19;i:39287;i:20;i:39290;i:21;i:39302;i:22;i:39303;i:23;i:39304;i:24;i:39306;i:25;i:39307;i:26;i:39308;i:27;i:39309;i:28;i:39311;i:29;i:39313;i:30;i:39314;i:31;i:39316;i:32;i:39318;i:33;i:39319;i:34;i:39320;i:35;i:39321;i:36;i:39323;i:37;i:39328;i:38;i:39330;i:39;i:39338;i:40;i:39339;i:41;i:39350;i:42;i:39351;i:43;i:39352;i:44;i:39353;i:45;i:39391;i:46;i:39392;i:47;i:39398;i:48;i:39404;i:49;i:39410;i:50;i:39411;i:51;i:39412;i:52;i:39413;i:53;i:39414;i:54;i:39416;i:55;i:39417;i:56;i:39418;i:57;i:39419;i:58;i:39421;i:59;i:39422;i:60;i:39426;i:61;i:39433;i:62;i:39447;i:63;i:39459;i:64;i:39461;i:65;i:39462;i:66;i:39463;i:67;i:39465;i:68;i:39466;i:69;i:39471;i:70;i:39502;i:71;i:39517;i:72;i:39629;i:73;i:39632;i:74;i:39633;i:75;i:39634;i:76;i:39635;i:77;i:39636;i:78;i:39637;i:79;i:39638;i:80;i:39639;i:81;i:39646;i:82;i:39651;i:83;i:39652;i:84;i:39657;i:85;i:39659;i:86;i:39671;i:87;i:39676;i:88;i:39677;i:89;i:39678;i:90;i:39679;i:91;i:39680;i:92;i:39681;i:93;i:39682;i:94;i:39686;i:95;i:39687;i:96;i:39688;}';
  196. $start = microtime(true);
  197. for($i = 0; $i < 10000;$i++) {
  198. $y = unserialize($x);
  199. }
  200. $period = microtime(true) - $start;
  201. fwrite($stdout,"unserialize use time={$period}\n");
  202. $x = implode(',',$y);
  203. $start = microtime(true);
  204. for($i = 0; $i < 10000;$i++) {
  205. $y = explode(',',$x);
  206. }
  207. $period = microtime(true) - $start;
  208. fwrite($stdout,"explode use time={$period}\n");
  209. }
  210. public function testUnsubscribe()
  211. {
  212. $l = new relation\mem_relation(39668);
  213. $l->unsubscribe(39653);
  214. $r = new relation\mem_relation(39653);
  215. $r->unsubscribe(39668);
  216. }
  217. }