-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadTestingSession.class.php
More file actions
595 lines (507 loc) · 16.4 KB
/
LoadTestingSession.class.php
File metadata and controls
595 lines (507 loc) · 16.4 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
/* Copyright: RedLine13.com . */
require_once('LoadTestingPageResponse.class.php');
/**
* Load Testing Session is the CURL wrapper. This is not required but does provide some easy calling and managing of web requests.
*
* CURL Options Set by Default include
* CURLOPT_RETURNTRANSFER (1)
* CURLOPT_HEADER (0) - Do not include headers in output
* CURLOPT_FOLLOWLOCATION (1) - Follow Redirects
* CURLOPT_FRESH_CONNECT (0) - Do not use persist connections.
* CURLOPT_ENCODING ('') - Enable compression
* CURLOPT_CONNECTTIMEOUT (30) - 30 second time out
* CURLOPT_TIMEOUT (120) - Overall timeout
* CURLOPT_COOKIEJAR - Setup location for cookie jar.
* CURLOPT_SSL_VERIFYPEER (0) - Don't validate SSL
*/
class LoadTestingSession
{
/** Test Number */
private $testNum = null;
/** Random token for test */
private $rand = null;
/** Cookie directory */
private $cookieDir = null;
/** Output directory */
private $outputDir = null;
/** Flags */
private $flags = 0;
/** Load Resources Flag */
const LOAD_RESOURCES = 0x00000001;
/** Verbose Flag */
const VERBOSE = 0x00000002;
/** Curl Handle */
private $ch = null;
/** Cookie jar (i.e. filename) */
private $cookieJar = null;
/** Last Response Headers */
private $lastRespHeaders = array();
/** Last URL */
private $lastUrl = null;
/** Min. delay (in ms) after fetching page */
private $minDelay = 0;
/** Max. delay (in ms) after fetching page */
private $maxDelay = 0;
/** Track last used delay. */
private $delay = null;
/** Resource Cache */
private $resourceCache = array();
/** Resource Data */
private $resourceData = array();
/** Base URL that resources will be loaded for */
public $loadableResourceBaseUrl = null;
/**
* Constructor
* @param int $testNum Test number
* @param string $rand Random token for test
* @param string $cookieDir Cookie directory (default: 'cookies')
* @param string $outputDir Output directory (default: 'output')
*/
public function __construct($testNum, $rand, $cookieDir = 'cookies', $outputDir = 'output')
{
$this->testNum = $testNum;
$this->rand = $rand;
$this->cookieDir = $cookieDir;
$this->outputDir = $outputDir;
// Seed random number generate
srand($testNum * time());
// Set up curl
$this->ch = curl_init();
// Setup curl_exec to return output
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
// Don't include HTTP headers in output
curl_setopt($this->ch, CURLOPT_HEADER, 0);
// Set up function to get headers
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array($this,'getLastCurlRespHeaders'));
// Include request header in curl info
curl_setopt($this->ch, CURLINFO_HEADER_OUT, 1);
// Follow redirects
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
// Don't use persistent connection
// curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($this->ch, CURLOPT_FRESH_CONNECT, 1);
// Enable compression
curl_setopt($this->ch, CURLOPT_ENCODING, '');
// Timeouts
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 120);
// Set cookie jar
$this->cookieJar = tempnam($this->cookieDir, 'cookie-');
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookieJar);
// Don't check SSL
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
}
/** __get */
public function __get($name)
{
if (property_exists($this, $name))
return $this->$name;
return null;
}
/** Enable resource loading */
public function enableResourceLoading() {
$this->flags |= self::LOAD_RESOURCES;
}
/** Disable resource loading */
public function disableResourceLoading() {
$this->flags &= ~self::LOAD_RESOURCES;
}
/** Verbose */
public function verbose() {
$this->flags |= self::VERBOSE;
// Output cookie jar file
echo 'Cookie jar: ' . $this->cookieJar . PHP_EOL;
}
/** Non-verbose */
public function nonVerbose() {
$this->flags &= ~self::VERBOSE;
}
/**
* Set delay (in ms) on page load.
* @param int $minDelay Min. artificial delay (in ms) on page load.
* @param int $maxDelay Max. artificial delay (in ms) on page load.
*/
public function setDelay($minDelay, $maxDelay)
{
if ($minDelay > $maxDelay)
{
$swap = $minDelay;
$minDelay = $maxDelay;
$maxDelay = $swap;
}
$this->minDelay = $minDelay;
$this->maxDelay = $maxDelay;
if ($this->maxDelay === null)
$this->maxDelay = $this->minDelay;
}
/**
* Send back recently used delay value.
*/
public function getDelay()
{
return $this->delay;
}
/** Cleanup */
public function cleanup()
{
// Close curl
curl_close($this->ch);
// Delete cookie file
if ($this->cookieJar)
unlink($this->cookieJar);
}
/**
* Set last curl response headers
*/
public function getLastCurlRespHeaders($ch, $header)
{
$this->lastRespHeaders[] = trim($header);
return strlen($header);
}
/**
* Fetch raw data form a URL with no delays or output
* @param string $url URL to go to
* @param mixed $post POST data, or null
* @param array $headers HTTP headers to send
* @param bool $saveData True to save data to file system
* @return string Raw response
* @throws Exception
*/
public function fetchRawDataFromUrl($url, $post = null, $headers = array(), $saveData = false)
{
// Set referer
if ($this->lastUrl != null)
curl_setopt($this->ch, CURLOPT_REFERER, $this->lastUrl);
$this->lastUrl = $url;
// Set post info
if (!empty($post))
{
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
}
else
curl_setopt($this->ch, CURLOPT_POST, 0);
// Set up headers
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
// Get page
curl_setopt($this->ch, CURLOPT_URL, $url);
$this->lastRespHeaders = array(); // Clear last response headers
if (($content = curl_exec($this->ch)) === false)
throw new Exception(curl_error($this->ch));
// Save data
if ($saveData)
{
static $pageNum = 0;
$pageNum++;
file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . 'test'.$this->testNum.'-rawData'.$pageNum.'-info.txt', print_r(curl_getinfo($this->ch), 1).print_r($this->lastRespHeaders, 1));
file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . 'test'.$this->testNum.'-rawData'.$pageNum.'-content.txt', $content);
}
return $content;
}
/**
* Remove query string and fragment from URL
*
* @param string $url URL
*
* @return string URL without query string and fragment
*/
public function removeQueryStringAndFragmentFromUrl($url)
{
$pos = strrpos($url, '?');
if ($pos !== false)
return substr($url, 0, $pos);
$pos = strrpos($url, '#');
if ($pos !== false)
return substr($url, 0, $pos);
return $url;
}
/**
* Go to a url.
* @param string $url URL to go to
* @param mixed $post POST data, or null
* @param array $headers HTTP headers to send
* @param boolean $saveData writing it to file.
* @return LoadTestingPageResponse
* @throws Exception
*/
public function goToUrl($url, $post = null, $headers = array(), $saveData = false, $isUser = true )
{
// Add slight delay to simulate user delay
if ($this->minDelay || $this->maxDelay)
{
$delay = rand($this->minDelay*1000, $this->maxDelay*1000);
$this->delay = ($delay/1000000);
if ($this->flags & self::VERBOSE)
echo 'Delay: ' . ($delay/1000000) . "s\n";
usleep($delay);
}
// Set referer
if ($this->lastUrl != null)
curl_setopt($this->ch, CURLOPT_REFERER, $this->lastUrl);
$this->lastUrl = $url;
$rtn = new LoadTestingPageResponse();
// Set post info
if (!empty($post))
{
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);
}
else
curl_setopt($this->ch, CURLOPT_POST, 0);
// Set up headers
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
// Get page
if ($this->flags & self::VERBOSE)
echo date('m/d/Y G:i:s') . PHP_EOL;
curl_setopt($this->ch, CURLOPT_URL, $url);
$this->lastRespHeaders = array(); // Clear last response headers
$startTime = time();
if (($content = curl_exec($this->ch)) === false){
$endTime = time();
$totalTime = $endTime - $startTime;
if ( $isUser ){
recordPageTime($endTime, $totalTime, true, 0);
}
recordURLPageLoad($this->removeQueryStringAndFragmentFromUrl($url), $endTime, $totalTime, true, 0);
throw new Exception(curl_error($this->ch));
}
$curlInfo = curl_getinfo($this->ch);
$rtn->setContent($content, $curlInfo['content_type']);
// Get info
$rtn->setInfo($curlInfo);
if ($this->flags & self::VERBOSE)
echo date('m/d/Y G:i:s') . PHP_EOL;
// Record bandwidth
$info = $rtn->getInfo();
$kb = 0;
if ($info['download_content_length'] > 0){
$kb = (int)$info['download_content_length']/1024;
}
else if ($info['size_download'] > 0){
$kb = (int)$info['size_download']/1024;
}
// Check response code
$respCode = $rtn->getHttpStatus();
$respError = false;
if (!($respCode >= 200 && $respCode <= 399))
$respError = true;
// Record info
$endTime = time();
$totalTime = $rtn->getTotalTime();
if ( $isUser ){
recordPageTime($endTime, $totalTime, $respError, 0);
}
recordURLPageLoad($this->removeQueryStringAndFragmentFromUrl($url), $endTime, $totalTime,$respError,$kb);
// Save files
if ($saveData)
{
static $pageNum = 0;
$pageNum++;
file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . 'test'.$this->testNum.'-page'.$pageNum.'-info.txt', print_r($rtn->getInfo(), 1).print_r($this->lastRespHeaders, 1));
file_put_contents($this->outputDir . DIRECTORY_SEPARATOR . 'test'.$this->testNum.'-page'.$pageNum.'-content.html', $rtn->getContent());
}
// Load resources
if ( !$respError && $this->flags & self::LOAD_RESOURCES)
$this->loadResources($rtn);
return $rtn;
}
/**
* Parse page and request other resources
* @param LoadTestingPageResponse $page Page object
*/
public function loadResources(LoadTestingPageResponse $page)
{
// TODO: Allow all resources?
if ($this->loadableResourceBaseUrl)
{
$resources = array();
// Get CSS hrefs
foreach ($page->getCssHrefs() as $href)
if (strpos($href, $this->loadableResourceBaseUrl) === 0)
$resources[] = $href;
// Get image hrefs
foreach ($page->getImageHrefs() as $href)
if (strpos($href, $this->loadableResourceBaseUrl) === 0)
$resources[] = $href;
// Get javascript srcs
foreach ($page->getJavascriptSrcs() as $src)
if (strpos($src, $this->loadableResourceBaseUrl) === 0)
$resources[] = $src;
// Set up new curl object
if ( !empty( $resources ) && $ch = curl_init())
{
// Set options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
//curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// Use persistent connection
//curl_setopt($ch, CURLOPT_MAXCONNECTS, 4);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 0);
// Enable compression
curl_setopt($ch, CURLOPT_ENCODING, '');
// Timeouts
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
// Set cookie jar
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieJar);
// Don't check SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Get each resource
$numInCache = $num304s = 0;
foreach ($resources as $resource)
{
// Check if this is in the cache
if (isset($this->resourceCache[$resource]) && $this->resourceCache[$resource] > time())
$numInCache++;
else
{
// Set up headers
$headers = array(
'Cache-Control:max-age=0',
'Connection: keep-alive',
'Keep-Alive: 300'
);
// Check if we have a last modified
if (isset($this->resourceData[$resource]['Last-Modified']))
$headers[] = 'If-Modified-Since: ' . $this->resourceData[$resource]['Last-Modified'];
// Check if we have an ETag
if (isset($this->resourceData[$resource]['ETag']))
$headers[] = 'If-None-Match: ' . $this->resourceData[$resource]['ETag'];
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Make request
curl_setopt($ch, CURLOPT_URL, $resource);
$startTime = time();
if (($content = curl_exec($ch)) === false){
$endTime = time();
recordPageTime($endTime, 0, true, 0);
recordURLPageLoad($this->removeQueryStringAndFragmentFromUrl($resource),$endTime,0,true,0);
throw new Exception(curl_error($ch));
}
// Check status code
$info = curl_getinfo($ch);
if ((int)$info['http_code'] == 304)
$num304s++;
// Record bandwidth
$kb = 0;
if ($info['download_content_length'] > 0){
$kb = (int)$info['download_content_length']/1024;
}
else if ($info['size_download'] > 0){
$kb = (int)$info['size_download']/1024;
}
// Check response code
$respCode = !empty($info['http_code']) ? (int)$info['http_code'] : null;
$respError = false;
if (!($respCode >= 200 && $respCode <= 399)){
$respError = true;
$err = array("uri" => $this->removeQueryStringAndFragmentFromUrl($resource), "code" => $respCode);
recordError(json_encode($err));
}
// Record load
$endTime = time();
recordURLPageLoad($this->removeQueryStringAndFragmentFromUrl($resource), $endTime, !empty($info['total_time']) ? (float)$info['total_time'] : 0.0, $respError, $kb );
// Add resource data
if (!isset($this->resourceData[$resource]))
{
$this->resourceData[$resource] = array(
'Last-Modified' => null,
'ETag'
);
}
// Read headers
$lines = preg_split("/(\r?\n)|\r/", $content);
array_shift($lines);
foreach($lines as $line)
{
if (empty($line))
break;
//if ($this->flags & self::VERBOSE)
//echo $line . PHP_EOL;
// Parse header
if (!preg_match('/^([^:]+):(.*)$/AD', $line, $match))
throw new Exception("{$resource}: Bad header \"{$line}\"");
$header = strtolower(trim($match[1]));
$value = strtolower(trim($match[2]));
// Check for cache headers
$expires = null;
if ($header == 'cache-control' && preg_match('/max-age=([0-9]+)/AD', $value, $match))
$expires = time()+$match[1];
else if ($header == 'expires')
$expires = strtotime($value);
if ($expires)
$this->resourceCache[$resource] = $expires;
// Other headers
if ($header == 'last-modified')
$this->resourceData[$resource]['Last-Modified'] = $value;
if ($header == 'etag')
$this->resourceData[$resource]['ETag'] = $value;
}
}
}
if ($this->flags & self::VERBOSE)
echo "Page requires " . count($resources) . " resources for base domain. {$numInCache} found in cache. {$num304s} 304 Not Modified responses.\n";
// Close curl
curl_close($ch);
}
}
}
/**
* Get automatic value for form field
* @param string $name Field name
* @return string Value, or null
*/
public function getFormAutoValue($name)
{
$lowerAlpha = 'abcdefghijklmnopqrstuvwxyz';
// Names
if (preg_match('/first.*name/i', $name))
return 'Load';
elseif (preg_match('/last.*name/i', $name))
return 'Test' . $this->testNum;
// E-mails
else if (preg_match('/email/i', $name))
return $lowerAlpha[rand(0,25)].$lowerAlpha[rand(0,25)].'-LoadTest' . $this->testNum . '-' . time() .'@example.com';
// Passwords
else if (preg_match('/password/i', $name))
return 'password';
// Address
else if (preg_match('/address/i', $name))
return $this->testNum . ' Load Test Ave.';
// City
else if (preg_match('/city/i', $name))
return 'Moorestown';
// State
else if (preg_match('/country/i', $name))
return 'US';
// State
else if (preg_match('/state/i', $name))
return 'NJ';
// Zip code
else if (preg_match('/zipcode/i', $name))
return '08057';
// Dob (Random)
else if (preg_match('/dob/i', $name))
return rand(1,12) . '/' . rand(1,28) . '/' . rand(1950, 2005);
// Phone (Random)
else if (preg_match('/phone/i', $name))
return rand(100, 999) . '-555-' . sprintf('%04d', rand(0, 9999));
// Phone (Random)
else if (preg_match('/gender/i', $name))
return rand(0,1) ? 'M' : 'F';
// Credit card info
else if (preg_match('/cardNumber/i', $name))
return '4111111111111111';
else if (preg_match('/cvv/i', $name))
return '123';
else if (preg_match('/cardExpiresMonth/i', $name))
return rand(1,12);
else if (preg_match('/cardExpiresYear/i', $name))
return 2050;
return null;
}
}
?>