TestRelationHelper.php 8.7 KB

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