Location: PHPKode > projects > Webbi Framework > victorjonsson-Webbi-framework-2c5dcec/www/algotest.php
<?php
include 'include/common.php';
use webbi\util\AlgorithmComparison;

class StringTest extends AlgorithmComparison {

    private $var_a = 'hej';
    private $var_b = 'hej igen';
    private $var_c = 'Ã¥terigen hejsan';
    private $array = array('key_a' => 'hej hej', 'key_b' => 'hej igen');

    public function __construct() {
        parent::__construct('String concatination using \'', 'String concatination using "');
    }

    function testA() {
        $str = '';
        $local_var = 'hejsan';
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
        $str .= $this->var_a;
        $str = $this->var_a.' '.$this->var_b.' hejsan hej '.$str;
        $str .= 'hejsan igen '.$this->array['key_a'].' - '.$this->array['key_b'].$local_var;
    }

    function testB() {
        $str = "";
        $local_var = "hejsan";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
        $local_var = "hejsan";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
        $str .= "{$this->var_a}";
        $str = "{$this->var_a} {$this->var_b} hejsan hej $str";
        $str .= "hejsan igen {$this->array['key_a']} - {$this->array['key_b']}$local_var";
    }
}

class ArrayTest extends AlgorithmComparison {

    private $a;
    private $b;

    public function __construct() {
        $this->a = array_fill(0, 150000, $this->genRandomString());
        $this->b = array_fill(0, 150000, $this->genRandomString());
        parent::__construct('array_walk($var, create_function(...))', 'foreach($var as $v) { ... }');
    }

    public function testA() {
      //  $a = '';
        array_walk($this->a, create_function('&$a', '$a = strtolower($a);'));
    }

    public function testB() {
        foreach($this->b as &$source) { $source = strtolower($source); }
    }

    private function genRandomString($length = 10) {
        $characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $string = '';

        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters)-1)];
        }

        return $string;
    }
}


class ForTest extends AlgorithmComparison {

    private $array;

    public function __construct() {
        $this->array = array_fill(0, 100, function() {return 'hejsan';});
        parent::__construct('for($i=0; $i < count($this->array); $i++)', '$len = count($this->array); for($i=0; $i < $len; $i++)');
    }

    public function testA() {
        for($i=0; $i < count($this->array); $i++) {
            $a = 1;
            $b = 2;
            $c = $a + $b;
        }
    }

    public function testB() {
        $arr_len = count($this->array);
        for($i=0; $i < $arr_len; $i++) {
            $a = 1;
            $b = 2;
            $c = $a + $b;
        }
    }
}

class XmlLoadingTest extends AlgorithmComparison {

    private $xml_file = 'include/local_component/webbi_html_unit/info.xml';

    public function __construct() {
        parent::__construct('simplexml_load_file(...)', 'simplexml_load_string(file_get_contents(...))');
    }

    public function testA() {
        $xml = simplexml_load_file($this->xml_file);
    }

    public function testB() {
        $xml = simplexml_load_string(file_get_contents($this->xml_file));
    }
}

class InArrayTest extends AlgorithmComparison {

    private $array = array('something' => 'hejsan');

    public function __construct() {
        parent::__construct('in_array(\'something\', $this->array)', 'isset($this->array[\'something\'])');
    }

    public function testA() {
        if(in_array('something', $this->array)) {
            $i = 0;
        }
    }

    public function testb() {
        if(isset($this->array['something'])) {
            $i = 0;
        }
    }
}

class DirectAccessTest extends AlgorithmComparison {

    public function __construct() {
        parent::__construct('foreach($this->theArray() as $val)', '$array = $this->theArray(); foreach($array as $val)');
    }

    private function theArray() {
        return array(
            1244,
            422,
            5323,
            52342323,
            526,
            5666,
            6,
            134555,
            1244,
            422
        );
    }

    public function testA() {
        $total = 0;
        foreach($this->theArray() as $val) {
            $total += $val;
        }
    }

    public function testb() {
        $total = 0;
        $array = $this->theArray();
        foreach($array as $val) {
            $total += $val;
        }
    }
}


class LambdaTest extends AlgorithmComparison {

    public function __construct() {
        parent::__construct('$var = function() {...} $var();', 'function f() {...} call_user_func(\'f\');');
    }

    public function testA() {
        $GLOBALS['my_func']();
    }

    public function testb() {
        call_user_func('my_func');
    }
}

$my_func = function() {
  $i = 23497;
  $i += 23489234;
  return $i / 12 * 43.14;
};

function my_func() {
  $i = 23497;
  $i += 23489234;
  return $i / 12 * 43.14;
}

class ArrayTest2 extends AlgorithmComparison {

    public function __construct() {
        parent::__construct('for', 'foreach');
    }

    private $arr = array(32,435,'asdfas',43,1,434,'sdf', 2344,'42',32,435,'asdfas',43,1,434,'sdf', 2344,'42',32,435,'asdfas',43,1,434,'sdf', 2344,'42');

    public function testA() {
        $len = count($this->arr);
        for($i=0; $i < $len; $i++) {

        }
    }

    public function testb() {
        foreach($this->arr as $a) {
            
        }
    }
}

class LambdaTest2 extends AlgorithmComparison {

    public function __construct() {
        parent::__construct('$var = function() {...} $var();', '$f = \'my_func\'; $f();');
    }

    public function testA() {
        $func = function() {
          $i = 23497;
          $i += 23489234;
          return $i / 12 * 43.14;
        };

        $func();
    }

    public function testb() {
        $f = 'my_func';
        $f();
    }
}


class ValidatingThisConcept extends AlgorithmComparison {

    public function __construct() {
        parent::__construct('', 'These two tests should be practically the same, just to proof that the results of these tests is valid');
    }

    function testB() {
        $a = 1234901234;
        $b = -3495;
        $c = 134;
        round( $c * $a / $b );
    }

    function testA() {
        $a = 1234901234;
        $b = -3495;
        $c = 134;
        round( $c * $a / $b );
    }
}

?>
<style type="text/css">

    * {
        font-size: 11px;
        font-family: Arial;
    }

    hr {display:none}

    table{
        margin-bottom:10px;
    }

    table td:FIRST-CHILD {
        width: 270px;
    }

    table thead  {
        background: #f2F2f2;
    }

    p {
        padding:20px 10px;
        background: #F2F2F2;
        border-top: #E1E1E1 solid 1px;
        border-bottom: #FFF solid 1px;
        margin:0;
    }
    
</style>
<?php

$tests = new \webbi\Map();
$tests->put('ArrayTest', 10);
$tests->put('StringTest', '');
$tests->put('ForTest', '');
$tests->put('XmlLoadingTest', '');
$tests->put('InArrayTest', '');
$tests->put('DirectAccessTest', '');
$tests->put('LambdaTest', '');
$tests->put('ArrayTest2', '');
$tests->put('ValidatingThisConcept', '');

$test_request = array();
if(isset($_GET['run'])) {
    $test_request = explode(',', $_GET['run']);
    foreach($test_request as $test) {
        $test = basename($test);
        if($tests->containsKey($test)) {
            $num = $tests->getValue($test);
            $test_obj = new $test();
            echo $test_obj->runTest(true, is_int($num) ? $num : null);
        }
        else {
            echo '<p style="background:red; font-weight:bold">Test '.$test.' is not defined</p>';
        }
    }
}

$iterator = $tests->iterator();
while($iterator->hasNext()) {
    $test_class = $iterator->nextKey()->__toString();
    /** @var \webbi\util\AlgorithmComparison $test_obj */
    $test_obj = new $test_class();
    ?>
    <p>
        <input type="checkbox" name="<?=$test_class?>" value="1" <?=in_array($test_class, $test_request) ? ' checked="checked"':''?>/>
        <strong style="background:#ffffe0"><?=$test_obj->getTestAName()?></strong>
        &nbsp;vs&nbsp;
        <strong style="background:#dbf2fa"><?=$test_obj->getTestBName()?></strong>
    </p>
    <?php
}
?>
<p>
    <input type="button" onclick="runTests();" value="Run" />
</p>
<script type="text/javascript">

    function runTests() {
        var checkboxes = document.getElementsByTagName('input');
        var requestedTest = '';
        for(var i=0; i < checkboxes.length; i++) {
            if(checkboxes[i].checked) {
                requestedTest += checkboxes[i].name + ',';
            }
        }

        if(requestedTest != '') {
            requestedTest = requestedTest.substring(0, requestedTest.length - 1);
            var currentUrl = document.location.href.split('?run=')[0];
            document.location = currentUrl +'?run='+requestedTest;
        }
    }

</script>
Return current item: Webbi Framework