summaryrefslogtreecommitdiffstats
path: root/utils/load_test.php
blob: 544b6d677d66eb78154e0faef3959f7bb44d85b3 (plain) (blame)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php

/**
* skripta ki zaporedno klice izpolnjevanje ankete na podanem naslovu $_GET['url'] in stevilu iteracij $_GET['iterate']
*/

include('../function.php');

if (!isset($_GET['url'])) $_GET['url'] = 'http://www.1ka.si/loadtest';
if (!isset($_GET['iterate'])) $_GET['iterate'] = 10;
if (!isset($_GET['uniqueID'])) $_GET['uniqueID'] = '';

$lt = new LoadTest();
$lt->url($_GET['url']);
$lt->run($_GET['iterate']);

/**
* 
* Class, ki na podanem URLju zacne izpolnjevati formo.
* Ce je na naslednji strani (ki jo dobi nazaj) zopet forma, nadaljuje izpolnjevanje (za izpolnjevanje celotne ankete na vecih straneh)
* 
* Izpolnjevanje od zacetka se pozene v podanem stevilu iteracij.
* 
*/
class LoadTest {
	
	private $time_start;
	private $start;
	private $url;
	private $subrequests = false;
	
	/**
	* zabelezimo zacetek izvajanja skripte
	* 
	*/
	function __construct () {
		$this->start = microtime(true);	
	}
	
	/**
	* URL na katerem bomo zaceli izpolnjevati formo
	* 
	* @param mixed $url
	*/
	function url ($url) {
		$this->url = $url;
	}
	
	/**
	* pozenemo izpolnjevanje forme v stevilu iteracij
	* 
	* @param mixed $iterate
	*/
	function run ($iterate = 1) {
		
		for ($i=1; $i<=$iterate; $i++) {
			$this->time_start = microtime(true);
			
			$this->fill_form($this->url);
			
			$time_seconds = microtime(true) - $this->time_start;
			echo $i.'. form filled in '.$time_seconds.' seconds<br>';
			flush(); @ob_flush();
		}
	
		$time_seconds = microtime(true) - $this->start;
		echo '<br><b>All '.$iterate.' forms filled in '.$time_seconds.' seconds</b>';
		
		flush(); @ob_flush();
	}
	
	/**
	* izpolnjuje nek form, dokler ne pride do strani brez form elementa
	* 
	* @param string $url
	* @param mixed $post
	*/
	function fill_form ($url, $post=null) {
		if ($url == '') return;
		$i = 0;
		
		do {
			
			list($header, $content) = $this->post_request($url, $post);
			if ($this->subrequests) {
				$this->post_request('http://www.1ka.si/admin/survey/minify/g=jsfrontend');
				$this->post_request('http://www.1ka.si/admin/survey/script/calendar/calendar.js');
				$this->post_request('http://www.1ka.si/admin/survey/script/calendar/lang/calendar-si.js');
				$this->post_request('http://www.1ka.si/admin/survey/script/calendar/calendar-setup.js');
				$this->post_request('http://www.1ka.si/admin/survey/script/calendar/calendar.css');
				$this->post_request('http://www.1ka.si/main/survey/skins/Default.css');
			}
			
			$url = '';
			
			// ce stran poslje redirect
			if (strpos($header, 'HTTP/1.1 302 Found') !== false) {
				
				$h = explode("\n", $header);
				foreach ($h AS $l) if (strpos($l, 'Location:') !== false) $location = $l;
				
				$url = trim( substr($location, 10) );
				$post = null;
				
			// obicen page, ki ga gremo parsat
			} else {
			
				$form = $this->parse_form($content);
				
				if ( isset($form['action']) ) $url = $form['action'];
				
				$form['input'] = $this->randomize_form($form['input']);
		
				if ( isset($form['input']) ) $post = $form['input'];

			}
			
			if (++$i >= 10000) { echo 'BREAK'; break; }	// preprecimo, da se ne zacikla
		
		} while ($url != '');
		
		/*if (strpos($content, 'Hvala za sodelovanje') === false)
			echo '<hr>'.$header.'<br>'.$content.'<hr>';
		else
			echo '<hr>KONEC<hr>';*/
	}
	
	/**
	* sparsa podano HTML vsebino strani in vrne array s podatki form-a
	* 
	* @param mixed $content
	*/
	function parse_form ($content) {
		$form = array();
		
		$dom = new DOMDocument();
		@$dom->loadHTML($content);
		$dom->preserveWhiteSpace = false; 
		
		$form_el = $dom->getElementsByTagName('form');
		foreach ($form_el AS $oneform) // na strani mora biti samo en form... ker drugace ne vemo katerega izbrati
			$form['action'] = $oneform->getAttribute('action');
		
		// gremo cez input polja
		$inputs = $dom->getElementsByTagName('input');
		
		foreach ($inputs AS $input) {
			$name = $input->getAttribute('name');
			$value = $input->getAttribute('value');
			$type = $input->getAttribute('type');
			if ($name != '') {
				$form['input'][$name]['type'] = $type;
				$form['input'][$name][] = $value;
			}
		}
		
		return $form;
	}
	
	/**
	* zrandomizira vrednosti forma
	* 
	* @param mixed $form
	*/
	function randomize_form($form) {
		/*echo '<pre>';
		echo "\noriginale: ";
		print_r($form);*/
		
		if ( count($form) == 0 ) return $form;
		
		foreach ($form AS $key => $input) {
		
			// radio button - izberemo enega nakljucno
			if ($input['type'] == 'radio') {
				$pos = rand(0, count($input)-2);
				$form[$key] = $input[$pos];
			
			// checkbox (razlika je v tabeli in navadnih, ker imajo razlicen nacin poimenovanja, in se ne da drugace zaznati skupin... zakompliciran..)
			} elseif ($input['type'] == 'checkbox') {
				
				// navaden checkbox - izberemo enega nakljucno
				if ( count($input) > 2 ) {
					$pos = rand(0, count($input)-2);
					$form[$key] = $input[$pos];
					
				// multigrid checkbox - vsak checkbox obkljukamo z verjetnostjo 50% (ker se ne da razbrati vrstic zaradi takega poimenovanja)
				} else {
					foreach ($input AS $k => $v) {
						if ($v != 'checkbox')
							if (rand(0,1) >= 0.5) $form[$key] = $v; else unset($form[$key]);
					}
				}
			
			// textfield - vpisemo nek random string
			} elseif ($input['type'] == 'text') {
				$form[$key] = ($_GET['uniqueID']!=''?$_GET['uniqueID'].'-':'') . substr(sha1(rand(0,1).time()), 0, 10);
			
			
			// ce je samo 1 element, nimamo kaj randomizirat (count je 2, ker je en type)
			} else {
				$form[$key] = $input[0];
				
			}
		
			
		}
		
		/*echo "\nrandomized:";
		print_r($form);
		echo '</pre>';*/
		
		return $form;
	}
	
	/**
	* en primercek, ki poslje vse parametre. request_show.php pa izpise vse post, get in cookieje ki jih prejme
	* 
	*/
	function test_example() {

		$post = array('test' => 'foobar', 'okay' => 'yes', '6' => 'test');

		$get = array ('get'=>'gett', 'get222'=>'123');

		$cookie = array ('ena' => 'prvi', 'dva' => 'drugiff');
		 
 		list($header, $content) = $this->post_request(
		    "http://test.1ka.si/utils/request_show.php?pa_v_urlju=tudi_dela",
		    $post,
		    $get,
		    $cookie
		);
		 
		echo $header.'<hr>'.$content;
	}

	/**
	* naredi request (POST oz GET, nastavi tudi COOKIE) in vrne rezultat
	*  
	*/
	function post_request($url, $_post=null, $_get=null, $_cookie=null, $referer='') {
 		
 		if ($referer == '') $referer = $url;
 		
 		if ($_post != null) {
		    $data = array();    
		    while (list($n, $v) = each($_post)) {
		        $data[] = "$n=$v";
		    }    
		    $data = implode('&', $data);
		}
		
 		if ($_get != null) {
 			$get = array();
 			while (list($n,$v) = each($_get)) {
				$get[] = "$n=$v";
 			}
 			$get = '?'.implode('&', $get);
		} else $get = '';
	 
 		if ($_cookie != null) {
 			$cookie = array();
 			while (list($n,$v) = each($_cookie)) {
				$coookie[] = "$n=$v";
 			}
 			$cookie = implode('; ', $coookie);
		}
				
	    // sparsamo url
	    $url = parse_url($url);
	    if ($url['scheme'] != 'http') { 
	        die('Only HTTP request are supported !');
	    }
	 
	    $host = $url['host'];
	    $path = $url['path'];
	    if (isset($url['query'])) $query = ($get==''?'?':'&').$url['query']; else $query = '';
	
		set_time_limit(0);
		
	    $fp = fsockopen($host, 80);
	 
	 	//stream_set_blocking($fp, false);
		stream_set_timeout($fp, 86400);
	
	    // posljemo header
	    if ($_post != null)
	    	fputs($fp, "POST {$path}{$get}{$query} HTTP/1.1\r\n");
	    else
	    	fputs($fp, "GET {$path}{$get}{$query} HTTP/1.1\r\n");
	    fputs($fp, "Host: $host\r\n");
	    fputs($fp, "Referer: $referer\r\n");
	    if ($_cookie != null)
	    	fputs($fp, "Cookie: $cookie\r\n");
	    fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
	    if ($_post != null)
	    	fputs($fp, "Content-length: ". strlen($data) ."\r\n");
	    fputs($fp, "Connection: close\r\n\r\n");
	    if ($_post != null)
	    	fputs($fp, $data);
	 
	    $result = ''; 
	    while(!feof($fp)) {
	        $result .= fgets($fp, 128);
	    }
	 
	    fclose($fp);
	 
	    // locimo header od podatkov
	    $result = explode("\r\n\r\n", $result, 2);
	 
	    $header = isset($result[0]) ? $result[0] : '';
	    $content = isset($result[1]) ? $result[1] : '';
	 
	 	// header in podatke vrnemo v arrayu
	    return array($header, $content);
	}

}

?>