Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PHP_Related.test.php « modules « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4a8b95f5bc25e83734779b66d0fedbe2c39f4fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
if(!defined("PATH_TEST_TO_ROOT")) {
	define('PATH_TEST_TO_ROOT', '..');
}
if(!defined('CONFIG_TEST_INCLUDED'))
{
	require_once PATH_TEST_TO_ROOT ."/../tests/config_test.php";
}

Zend_Loader::loadClass('Piwik_Timer');

class test_staticAttr
{
	static public $a = 'testa';
	public $b = 'testb';
}

class test_magicMethodStaticAttr
{
	static $test = "test";
	
	function __get($name)
	{
		print("reading static attr ; __get called");
		return 1;
	}
}
		
class Test_PHP_Related extends UnitTestCase
{
	function __construct( $title = '')
	{
		parent::__construct( $title );
	}
	
	public function setUp()
	{
	}
	
	public function tearDown()
	{
	}
	
	public function testMergeArray()
	{
		$a = array('label' => 'test');
		$b = array('test' => 1, 1 => 2100);
		
		$expected = array(
		'label' => 'test',
		'test' => 1, 1 => 2100
		);
		
//		$this->assertEqual( array_merge($a,$b), $expected);
		$this->assertEqual( $a+$b, $expected);
	}
	
	/**
	 * test reading static attribute of a variable class
	 */
	public function test_staticAttr()
	{
		// use this trick to read the static attribute of the class
		// $class::$methodsNotToPublish doesn't work
		$vars = get_class_vars("test_staticAttr");
		
		$this->assertEqual( $vars['a'], 'testa' );
	}
	
	static $countSort=0;
	function test_usortcalledHowManyTimes()
	{
		$a=array();
		//generate fake 1000 elements access
		for($i=0;$i<1000;$i++)
		{
			$a[]=mt_rand();
		}
		$timer = new Piwik_Timer;
		function countSort($a,$b)
		{
			Test_PHP_Related::$countSort++;
			return $a < $b ? -1 : 1;
		}
		//sort using usort
		usort($a, "countSort");
		
		// in the function used count nb of times called
//		print("called ".self::$countSort." times to sort the 1000 elements array");
		
//		echo $timer;
	}
	
	/**
	 * __get is not called when reading a static attribute from a class... snif 
	 */
	public function test_magicMethodStaticAttr()
	{
		$val = test_magicMethodStaticAttr::$test;
		
		$this->assertEqual( $val, "test" );
	}
	
	function test_array2XML()
	{
		
		function array_to_simplexml($array, $name="config" ,&$xml=null )
		{
		    if(is_null($xml))
		    {
		        $xml = new SimpleXMLElement("<{$name}/>");
		    }
		   
		    foreach($array as $key => $value)
		    {
		        if(is_array($value))
		        {
		            $xml->addChild($key);
		            array_to_simplexml($value, $name, $xml->$key);
		        }
		        else
		        {
		            $xml->addChild($key, $value);
		        }
		    }
		    return $xml;
		}
		$test=array("TEST"=>"nurso",
       	 		"none"=>null,
       		 "a"=>"b",
        	array(
          	  "c"=>"d",
          		  array("d"=>"e"))
          );
          
		$xml = array_to_simplexml($test);
		
//		print("<pre>START");print($xml);print("START2");
//		print_r($xml->asXML());
//		print("</pre>");
	}
	/**
	 * misc tests for performance
	 * 
	 * - we try to serialize data when data is a huge multidimensional array
	 * - we then compare the results when data is the huge array 
	 *   but split in hundreds of smaller arrays.
	 * 
	 * clearly the best solution is to split the array in multiple small arrays
	 */
	public function _test_serializeHugeTable()
	{
		$timer = new Piwik_Timer;
		$a=array();
		//generate table
		for($i=0;$i<100;$i++)
		{
			$category=array();
			for($j=0;$j<10;$j++)
			{
				for($k=0;$k<20;$k++)
				{
					$infoPage=array(10,50,1500,15000,1477,15669,15085,45454,87877,222);
					$a[$i][$j][] = $infoPage;
				}
				
			}
			
			
			for($k=0;$k<15;$k++)
			{
				$infoPage=array(154548781,10,50,10,1477,15669,15085);
				$a[$i][] = $infoPage;
			}
		}
		
		//echo "<br>after generation array = ". $timer;
		//echo "<br>count array = ". count($a,1);
		
		$serialized = serialize($a);
		$size = round(strlen($serialized)/1024/1024,3);
		//echo "<br>size serialized string = ". $size."mb";
		//echo "<br>after serialization array = ". $timer;
		
		$serialized = gzcompress($serialized);
		$size = round(strlen($serialized)/1024/1024,3);
		//echo "<br>size compressed string = ". $size."mb";
		//echo "<br>after compression array = ". $timer;
		
		$a = gzuncompress($serialized);
		//echo "<br>after uncompression array = ". $timer;
		$a = unserialize($a);
		//echo "<br>after unserialization array = ". $timer;
		
	}
	public function _test_serializeManySmallTable()
	{
		$timer = new Piwik_Timer;
		$a=array();
		//echo "<br>";
		//generate table
		for($i=0;$i<100;$i++)
		{
			$category=array();
			for($j=0;$j<10;$j++)
			{
				for($k=0;$k<20;$k++)
				{
					$infoPage=array(10,50,1500,15000,1477,15669,15085,45454,87877,222);
					$a[$i][$j][] = $infoPage;
				}
				
			}
			for($k=0;$k<15;$k++)
			{
				$infoPage=array(-1,10,50,10,1477,15669,15085);
				$a[$i][] = $infoPage;
			}
		}
		//echo "<br>after generation array = ". $timer;
		//echo "<br>count array = ". count($a,1);
		
		$allSerialized=array();
		for($i=0;$i<100;$i++)
		{
			for($j=0;$j<10;$j++)
			{
				$allSerialized[] = serialize($a[$i][$j]);
			}
			
			$allSerialized[] = serialize( array_slice($a[$i], 10,15));
		}
		
		//echo "<br>after serialize the subs-arrays = ". $timer;
		//echo "<br>count array = ". count($allSerialized,1);
		
		$size=0;
		foreach($allSerialized as $str)
		{
			$size+=strlen($str);
		}
		$size = round($size/1024/1024,3);
		//echo "<br>size serialized string = ". $size."mb";
		
		$acompressed=array();
		$size = 0;
		foreach($allSerialized as $str)
		{
			$compressed=gzcompress($str);
			$size+=strlen($compressed);
			$acompressed[] = $compressed;
		}
		$size = round($size/1024/1024,3);
		//echo "<br>size compressed string = ". $size."mb";
		//echo "<br>after compression all sub arrays = ". $timer;
	}
	
}