-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.php
791 lines (505 loc) · 23.3 KB
/
engine.php
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
<?php
/*
engine.php
All the wrapper functions that interface with an <engine> file.
NOTE: to be able to test properly, an engine should never trigger_error. it must always return a result.
if there is an error, return error('message')
*/
function call($category, $function, $params = array(), $testMode = false) {
/* track memory perfomance */ if (true) {
if (!empty($GLOBALS['chewittcom']) && isDebugMode()) {
$bytes_startMemory = memory_get_usage();
}
}
/* track time perfomance */ if (true) {
$currentEngine = $category . '/' . $function;
// bank resource usage to the PARENT engine from the last timer point to now, and reset the timer
bankComponentUsage('engine');
if (empty($GLOBALS['performance']['currentEngine'])) { // this is the first time any engine has been called
} else {
$parentEngine = $GLOBALS['performance']['currentEngine'];
if ('core/validate' === $currentEngine) {
$GLOBALS['performance']['engines'][$parentEngine]['validateCalls']++;
} else {
$GLOBALS['performance']['engines'][$parentEngine]['subCalls']++;
}
}
// time past this point should be banked to THIS engine
startBankingToNewComponentInstance('engine', $currentEngine);
$seconds_birthTime = $GLOBALS['performance']['engineTimer'];
}
$fileName = engineGetFileName($category, $function);
$paramDefinitions = engineGetDefinition($fileName);
/* process and validate parameters */ if (true) {
if (!empty($paramDefinitions)) {
try {
$params = engineProcessParams($paramDefinitions, $params, '/' . $category . '/' . $function);
} catch (Exception $e) {
if ($testMode) {
return error('[in engine ' . $category . '/' . $function . ']: ' . $e->getMessage());
} else {
trigger_error($e->getMessage(), E_USER_ERROR);
}
}
}
}
/* execute the action */ if (true) {
$result = engineExecute($fileName, $params, '/' . $category . '/' . $function);
}
/* error handling */ if (true) {
if (isError($result)) {
$errors = $result[1];
if (!is_array($errors) && !empty($GLOBALS['chewittcom']) && isDebugMode()) { // if the error value is an array, it's for percolating up as a form error, so leave it clean
$errors = '[in engine ' . $category . '/' . $function . ']: ' . $errors;
}
return error($errors);
}
}
/* track time perfomance */ if (true) {
// bank all resource usage to the current engine from the last timer point to now, and reset the timer
bankComponentUsage('engine');
// component is finished so record its lifespan
$seconds_alive = $GLOBALS['performance']['engineTimer'] - $seconds_birthTime;
$GLOBALS['performance']['engines'][$currentEngine]['ms_alive'] = round($GLOBALS['performance']['engines'][$currentEngine]['ms_alive'] + $seconds_alive * 1000, 2);
/* space-saving single-string view
$GLOBALS['performance']['list'] .=
'engine: ' . $category . '/' . $function .
'ms_duration: ' . ($seconds_duration * 1000) .
'bytes_memoryFootprint: ' . $bytes_memoryPeak . '\n';
*/
/* show every call in order
$GLOBALS['performance'][$profilingIndex] = array(
'engine' => $category . '/' . $function,
'ms_duration' => ($seconds_duration * 1000),
'bytes_memoryFootprint' => $bytes_memoryPeak,
);
*/
if (!empty($parentEngine)) {
$GLOBALS['performance']['currentEngine'] = $parentEngine;
}
}
if (!empty($GLOBALS['chewittcom']) && isDebugMode()) {
/* track memory perfomance */ if (true) {
$bytes_memoryPeak = memory_get_peak_usage() - $bytes_startMemory;
$GLOBALS['performance']['engines'][$currentEngine]['kb_memoryFootprint'] += round($bytes_memoryPeak / 1000);
}
unset($GLOBALS['lastEngine_startParams']); // the engine executed successfully, so we can forget about the debugging data
}
return $result;
}
function startBankingToNewComponentInstance($componentType, $componentName) {
if (!empty($GLOBALS['performance']['current' . ucfirst($componentType)])) {
$parentComponent = $GLOBALS['performance']['current' . ucfirst($componentType)];
}
$GLOBALS['performance']['current' . ucfirst($componentType)] = $componentName;
if (empty($GLOBALS['performance'][$componentType . 's'][$componentName])) { // this is the first time this component has been called
$GLOBALS['performance'][$componentType . 's'][$componentName]['seconds_duration'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['numCalls'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['ms_alive'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['ms_working'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['kb_memoryFootprint'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['subCalls'] = 0;
$GLOBALS['performance'][$componentType . 's'][$componentName]['validateCalls'] = 0;
}
$GLOBALS['performance'][$componentType . 's'][$componentName]['numCalls']++;
if (!empty($parentComponent)) {
if (empty($GLOBALS['performance'][$componentType . 's'][$componentName]['calledBy'][$parentComponent])) {
$GLOBALS['performance'][$componentType . 's'][$componentName]['calledBy'][$parentComponent] = 1;
} else {
$GLOBALS['performance'][$componentType . 's'][$componentName]['calledBy'][$parentComponent]++;
}
}
}
function bankComponentUsage($componentType) {
if (empty($GLOBALS['performance']['current' . ucfirst($componentType)])) { // this is the beginning of the first call to this type of component
$GLOBALS['performance'][$componentType . 'Timer'] = microtime(true);
return;
}
$componentName = $GLOBALS['performance']['current' . ucfirst($componentType)];
$startTime = $GLOBALS['performance'][$componentType . 'Timer'];
// set or reset the timer
$GLOBALS['performance'][$componentType . 'Timer'] = microtime(true);
$seconds_newComponentActivity = $GLOBALS['performance'][$componentType . 'Timer'] - $startTime;
$GLOBALS['performance'][$componentType . 's'][$componentName]['ms_working'] = round($GLOBALS['performance'][$componentType . 's'][$componentName]['ms_working'] + $seconds_newComponentActivity * 1000, 2);
}
function engineGetCachedTestSummary($category, $function) {
// returns several metrics including how many tests there are, how many are passing, and performance data
$siteName = engineDetermineSite($category, $function);
$cacheFile = fileo(array(
'path' => array('cache', 'testResults', $siteName, $category),
'filename' => $function . '.txt',
));
if (!$cacheFile->exists()) return null;
$summary = $cacheFile->contentsAsNestedArray($cacheFile);
foreach ($summary as $key => $value) {
$summary[$key] = firstKey($value);
if (is_null($summary[$key])) $summary[$key] = 0;
}
return $summary;
}
function engineGetTestResults($category, $function, $site) {
$fileName = engineGetFileName($category, $function);
$results = array(
'single' => testSingle($category, $function, $site),
'validation' => testValidation($category, $function, $site),
'full' => testFull($category, $function, $site),
'summary' => array(),
);
//$validationResults = testValidation($category, $function, $site);
$singleTotal = 0;
$singlePassed = 0;
//if (!empty($results['single']['cases'])) {
foreach ($results['single'] as $paramName => $cases) {
foreach ($cases as $caseName => $caseResult) {
$singleTotal++;
if ($caseResult['compliant']) $singlePassed++;
}
}
//}
$fullTotal = 0;
$fullPassed = 0;
foreach ($results['full']['cases'] as $case) {
$fullTotal++;
if ($case['compliant']) $fullPassed++;
}
$results['summary'] = array(
'instant' => time(),
'total' => ($singleTotal + $fullTotal),
'passing' => ($singlePassed + $fullPassed),
'ms_longestDuration' => round(max($results['validation']['ms_longestDuration'], $results['full']['ms_longestDuration']), 3),
'kb_maxMemory' => round(max($results['validation']['kb_maxMemory'], $results['full']['kb_maxMemory']), 3),
);
/* every time we retest, store the result summary for quick reference or non-admin access */ if (true) {
$siteName = engineDetermineSite($category, $function);
/* in the filesystem cache */ if (true) {
$cacheFile = fileo(array(
'path' => array('cache', 'testResults', $siteName, $category),
'filename' => $function . '.txt',
'createDirectories' => true,
));
$cacheFile->overwriteContentsWithIndentedLines($results['summary'], 'delete all current content');
}
/* in the database */ if (true) {
/*
$result = dbSave('app_engine_test_results', parameterize(array(
'site' => $siteName,
'category' => $category,
'function' => $function,
'totalTests' => $results['summary']['total'],
'passingTests' => $results['summary']['passing'],
'msLongestDuration' => $results['summary']['ms_longestDuration'],
'kbMaxMemory' => $results['summary']['kb_maxMemory'],
)));
*/
}
}
return $results;
}
function testSingle($category, $function) {
//test single parameters (using a default value for the rest) and check the return state
$fileName = engineGetFileName($category, $function);
//$paramDefinitions = engineGetDefinition($fileName);
$testCases = engineGetTestCases($fileName);
/* get an array of safe values for required params */ if (true) {
$safeArray = array();
foreach ($testCases['single'] as $paramName => $paramDef) {
if (isset($paramDef['safe'])) {
$safeArray[$paramName] = $paramDef['safe'];
} else {
trigger_error('[in engine ' . $category . DS . $function . ']: safe value not set for parameter "' . $paramName . '"');
}
}
}
$results = array();
//$results['kb_maxMemory'] = 0; // TODO: make this work
//$results['ms_longestDuration'] = 0; // TODO: make this work
foreach ($testCases['single'] as $paramName => $paramDef) {
foreach (array('pass', 'fail') as $expectedResult) {
foreach ($paramDef[$expectedResult] as $value) {
$input = array_merge($safeArray, array($paramName => $value));
$output = call($category, $function, $input, true);
$prettyValue = gettype($value) . '_' . $value . '_';
$results[$paramName][$prettyValue]['expected'] = $expectedResult;
if (isError($output)) { // we would expect the engine would usually fail when passing only one parameter
$errors = $output[1];
/*
if (empty($errors[$paramName])) {
$results[$paramName][$prettyValue]['actual'] = 'NO PARAM ERRORS';
$results[$paramName][$prettyValue]['compliant'] = ('pass' == $expectedResult);
} else {
*/
if (is_array($errors)) {
$results[$paramName][$prettyValue]['actual'] = $errors[$paramName];
} else {
$results[$paramName][$prettyValue]['actual'] = $errors;
}
$results[$paramName][$prettyValue]['compliant'] = ('fail' == $expectedResult);
//}
$results[$paramName][$prettyValue]['errors'] = $errors;
} else {
$results[$paramName][$prettyValue]['actual'] = 'FUNCTION PASSED';
$results[$paramName][$prettyValue]['compliant'] = ('pass' == $expectedResult);
}
}
}
}
return $results;
}
function testFull($category, $function) {
// test full cases and compare the expected return value with the actual
$fileName = engineGetFileName($category, $function);
//$paramDefinitions = engineGetDefinition($fileName);
$testCases = engineGetTestCases($fileName);
$results = array();
$results['kb_maxMemory'] = 0;
$results['ms_longestDuration'] = 0;
$results['cases'] = array();
$index = 0;
foreach ($testCases['full'] as $case) {
if ( (count($case) !== 2) || !hasKey($case, 'input') || !hasKey($case, 'output') ) trigger_error('each full test case must be in the form array(\'input\' => array(...), \'output\' => ...)');
$results['cases'][$index] = engineExecuteTest($category, $function, $case['input'], $case['output']);
if (!empty($case['description'])) {
$results['cases'][$index]['description'] = $case['description'];
}
if ($results['cases'][$index]['kb_memoryFootprint'] > $results['kb_maxMemory']) {
$results['kb_maxMemory'] = $results['cases'][$index]['kb_memoryFootprint'];
}
if ($results['cases'][$index]['ms_duration'] > $results['ms_longestDuration']) {
$results['ms_longestDuration'] = $results['cases'][$index]['ms_duration'];
}
$index++;
}
return $results;
}
function testValidation($category, $function) {
$fileName = engineGetFileName($category, $function);
$desc = false;
$init = true;
$paramDefinitions = require($fileName);
$desc = false;
$init = false;
$test = true;
$testCases = require($fileName);
$results = array();
$results['kb_maxMemory'] = 0;
$results['ms_longestDuration'] = 0;
$index = 0;
if (!empty($testCases['validation'])) {
foreach ($testCases['validation'] as $type => $cases) {
foreach ($cases as $case) {
$results['cases'][$index] = engineExecuteTest($category, $function, array('type' => $type, 'value' => $case[0]), $case[1]);
if ($results['cases'][$index]['kb_memoryFootprint'] > $results['kb_maxMemory']) {
$results['kb_maxMemory'] = $results['cases'][$index]['kb_memoryFootprint'];
}
if ($results['cases'][$index]['ms_duration'] > $results['ms_longestDuration']) {
$results['ms_longestDuration'] = $results['cases'][$index]['ms_duration'];
}
$index++;
}
}
}
return $results;
}
// private functions:
function engineGetFileName($category, $function, $site = null) {
/* any engine can be implemented in the core app, or the site, or both.
if the engine is implemented in both places, use the site-specific implementation.
*/
$site = engineDetermineSite($category, $function, $site);
if ('app' === $site) {
return engineFilenameApp($category, $function);
} else {
return engineFilenameSite($category, $function, $site);
}
}
function engineFilenameApp($category, $function) {
return APP_ROOT . '/engines/' . $category . '/' . $function . '.php';
}
function engineFilenameSite($category, $function, $site) {
return VERSION_ROOT . '/sites/' . $site . '/engines/' . $category . '/' . $function . '.php';
}
function engineDetermineSite($category, $function, $site = null) {
/* any engine can be implemented in the core app, or in one or more individaul sites, or both.
if the engine is implemented in both places, use the site-specific implementation.
*/
if (!is_string($category)) trigger_error('engine category was not a string: ' . $category);
if (!is_string($function)) trigger_error('engine category was not a string: ' . $category);
if (empty($category)) trigger_error('engine category was empty');
if (empty($function)) trigger_error('engine category was empty');
if (!empty($site)) {
$fileName = engineFilenameSite($category, $function, $site);
if (isValidResource($fileName)) return $site;
trigger_error('site-specific engine not found: /' . $fileName);
}
// try current site root - is this a good idea?
$siteFileName = SITE_ROOT . '/engines/' . $category . '/' . $function . '.php';
if (isValidResource($siteFileName)) return SITE;
// TODO: try other site roots?
$appFileName = engineFilenameApp($category, $function);
if (isValidResource($appFileName)) return 'app';
trigger_error('engine not found: /' . $category . '/' . $function . ' using site ' . SITE . '(looking for ' . $appFileName . ')');
}
function engineGetDescription($fileName) {
$desc = true;
$init = true; // in case there's no $desc conditional in the engine file
$description = require($fileName);
if (!is_string($description)) return; // assume there was no $desc conditional and that we got the init array back instead
$description = trim($description);
if (empty($description)) return;
return $description;
}
function engineGetDefinition($fileName) {
// returns the parameter definition of an engine file
// should probably be called engineGetParams or engineGetParamDefinition
$desc = false;
$init = true;
$paramDefinitions = require($fileName);
if (!is_array($paramDefinitions)) trigger_error('invalid parameter definition in engine file ' . $fileName);
return $paramDefinitions;
}
function engineGetTestCases($fileName) {
$desc = false;
$init = false;
$test = true;
$cases = require($fileName);
if (empty($cases)) trigger_error('no test case definition in engine file ' . $fileName);
if ( !hasKey($cases, 'single') || !hasKey($cases, 'full') ) trigger_error('invalid case definition in engine file ' . $fileName);
return $cases;
}
function engineProcessParams($expectedParams, $actualParams, $engineString) {
/* process the parameters sent to an engine and validate them based on what's expected from the engine definition
there's a reason we throw exceptions instead of triggering errors in this function:
if there's a validation error while running the engine as a test, we don't want to terminate execution, so throw an error that we can catch and test against
*/
if (!is_array($actualParams)) trigger_error("engine parameters must be passed as an array, e.g. call('foo', 'bar', array('baz' => 'quux'))");
if (isset($expectedParams['validateInputType'])) {
$validateInputType = $expectedParams['validateInputType'];
unset($expectedParams['validateInputType']);
} else {
$validateInputType = true;
}
$cleanParams = array();
foreach ($expectedParams as $expectedParam) {
/* check that this parameter's definition is complete */ if (true) {
if (!isset($expectedParam['name'])) throw new Exception('Parameter definition with no name in ' . $engineString);
$paramName = $expectedParam['name'];
foreach(array('type', 'required') as $item) {
if (!isset($expectedParam[$item])) throw new Exception($paramName . ' parameter missing required definition component "' . $item . '" in ' . $engineString);
}
}
//if (isset($actualParams[$paramName])) {
if (array_key_exists($paramName, $actualParams)) {
$paramIsSet = true;
$paramValue = $actualParams[$paramName];
unset($actualParams[$paramName]);
if (!$validateInputType || (null === $paramValue) || ('any' === $expectedParam['type']) || ('/core/validate' === $engineString)) { //no validation will be performed!
$cleanParams[$paramName] = $paramValue;
} else {
$result = call('core', 'validate', array('value' => $paramValue, 'type' => $expectedParam['type']));
if (isError($result)) throw new Exception('invalid parameter "' . $paramName . '" in ' . $engineString . ': ' . $result[1] . '(value was [' . gettype($paramValue) . ']' . $paramValue . ')');
$cleanParams[$paramName] = $result;
}
} else {
if ($expectedParam['required']) throw new Exception('required parameter "' . $paramName . '" in ' . $engineString . ' was empty.');
if (array_key_exists('default', $expectedParam)) {
$cleanParams[$paramName] = $expectedParam['default'];
}
}
}
if (!empty($actualParams)) throw new Exception('unexpected param(s) sent to engine ' . $engineString . ': ' . implode(' & ', array_keys($actualParams)));
return $cleanParams;
}
function engineExecute($fileNameXYZ, $paramsXYZ, $engineStringXYZ) {
// use unusual parameter names to avoid variable name collisions when we call extract()
extract($paramsXYZ);
if (isset($desc)) trigger_error('cannot pass parameter named "desc"; it will conflict with engine variables');
if (isset($init)) trigger_error('cannot pass parameter named "init"; it will conflict with engine variables');
if (isset($test)) trigger_error('cannot pass parameter named "test"; it will conflict with engine variables');
$desc = false;
$init = false;
$test = false;
/*
if (!isProduction()) {
writeLog('latest_request', 'calling engine ' . $engineStringXYZ . ' with parameters ' . json_encode(get_defined_vars()) . '\n');
}
*/
ob_start();
//if ( !empty($GLOBALS['chewittcom']) && isDebugMode() ) {
if (!empty($GLOBALS['chewittcom'])) {
$GLOBALS['debug']['engine']['current']['beforeParams'] = get_defined_vars();
}
$result = require($fileNameXYZ);
/*
if (!isProduction()) {
writeLog('latest_request', '...result: ' . $result . '\n');
}
*/
//if ( !empty($GLOBALS['chewittcom']) && isDebugMode() ) {
if (!empty($GLOBALS['chewittcom'])) {
if ($GLOBALS['debug']['settings']['debugEngines']) {
//$GLOBALS['debug']['engine']['current']['after'] = get_defined_vars();
$GLOBALS['debug']['engine'] [] = array(
'name' => $engineStringXYZ,
'beforeParams' => $paramsXYZ,
//'beforeParams' => $GLOBALS['debug']['engine']['current']['beforeParams'],
//'afterParams' => get_defined_vars(),
'return' => $result,
);
} else {
unset($GLOBALS['debug']['engine']['current']['before']);
}
}
$outputBuffer = ob_get_clean();
if (!empty($outputBuffer)) {
if (isDebugMode()) {
echo $outputBuffer;
} else {
trigger_error('engine created output: ' . ( strlen($outputBuffer) > 50 ? substr($outputBuffer, 0, 50) . '...' : $outputBuffer ));
}
}
return $result;
}
function engineExecuteTest($category, $function, $input, $expectedOutput) {
$memoryBefore = memory_get_usage();
$start = microtime(true);
$output = call($category, $function, $input, true);
$seconds_duration = microtime(true) - $start;
$ms_duration = $seconds_duration * 1000;
$memoryPeak = memory_get_peak_usage();
$bytes_memoryFootprint = $memoryPeak - $memoryBefore;
if (('pass' === $expectedOutput) || ('fail' === $expectedOutput)) {
$simpleOutput = (isError($output)) ? 'fail' : 'pass';
if ($expectedOutput === $simpleOutput) {
$output = $simpleOutput;
}
}
$results = array(
'input' => $input,
'expected' => $expectedOutput,
'actual' => $output,
'compliant' => ($expectedOutput === $output),
'kb_memoryFootprint' => $bytes_memoryFootprint / 1024,
'ms_duration' => $ms_duration,
);
return $results;
}
function x_benchmark($category, $function) {
$init = false;
$test = true;
$testCases = require(APP_ROOT . DS . 'engines' . DS . $category . DS . $function . '.php');
/* get an array of safe values for required params */ if (true) {
$safeArray = array();
foreach ($testCases['single'] as $paramName => $paramDef) {
$safeArray[$paramName] = $paramDef['safe'];
}
}
$numIterations = 10;
$start = microtime(true);
for ($i = 0; $i < $numIterations; $i++) {
$output = call($category, $function, $safeArray);
}
//return round((microtime(true) - $start), 2) . ' seconds for ' . $numIterations . ' iterations';
$seconds_duration = (microtime(true) - $start) / $numIterations;
$ms_duration = $seconds_duration * 1000;
return round($ms_duration, 2) . ' ms';
}