1
0
mirror of https://github.com/danog/PrimeModule.git synced 2024-11-26 20:34:37 +01:00
PrimeModule/tests/testing.php

122 lines
5.4 KiB
PHP
Raw Normal View History

2017-02-21 18:35:05 +01:00
#!/usr/bin/env php
<?php
require 'vendor/autoload.php';
2017-02-25 03:15:43 +01:00
require 'StructClass.php';
require 'Struct.php';
require 'StructTools.php';
2017-02-21 20:37:13 +01:00
function get_time($callable, $param)
{
2017-02-21 18:35:05 +01:00
$a = microtime(true);
$result = $callable($param);
$time = microtime(true) - $a;
2017-02-21 20:37:13 +01:00
if (is_array($result)) {
$result = json_encode($result);
}
2017-02-21 18:35:05 +01:00
return [$time, $result];
}
2017-02-21 20:37:13 +01:00
function test($n)
{
$init = '| '.str_pad('result', strlen($n), ' ', STR_PAD_RIGHT).' | type | time |';
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL;
2017-02-21 18:35:05 +01:00
2017-02-21 20:37:13 +01:00
echo '|'.str_pad('Multiple factorization of '.$n, strlen($init) - 2, ' ', STR_PAD_BOTH).'|'.PHP_EOL;
echo '|'.str_pad('', strlen($init) - 2, '_', STR_PAD_RIGHT).'|'.PHP_EOL;
2017-02-21 18:35:05 +01:00
echo $init.PHP_EOL;
list($time, $result) = get_time(['\danog\PrimeModule', 'python_alt'], $n, true);
$GLOBALS['medium']['python_alt'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
list($time, $result) = get_time(['\danog\PrimeModule', 'python'], $n);
$GLOBALS['medium']['python'] += $time;
2017-02-22 02:55:10 +01:00
/*
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
list($time, $result) = get_time(['\danog\PrimeModule', 'native'], $n);
$GLOBALS['medium']['native'] += $time;
2017-02-22 02:55:10 +01:00
*/
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | python | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram'], $n);
$GLOBALS['medium']['wolfram'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | native | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
//echo '| '.str_pad($n, 6, " ", STR_PAD_RIGHT).' | auto | '.str_pad(get_time(['\danog\PrimeModule', 'auto'], $n), 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 20:37:13 +01:00
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
2017-02-21 18:35:05 +01:00
}
2017-02-21 20:37:13 +01:00
function test_single($n, $messy = false)
{
$init = '| '.str_pad('result', strlen($n), ' ', STR_PAD_RIGHT).' | type | time |';
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL;
echo '|'.str_pad('Single factorization of '.$n, strlen($init) - 2, ' ', STR_PAD_BOTH).'|'.PHP_EOL;
echo '|'.str_pad('', strlen($init) - 2, '_', STR_PAD_RIGHT).'|'.PHP_EOL;
2017-02-21 18:35:05 +01:00
echo $init.PHP_EOL;
list($time, $result) = get_time(['\danog\PrimeModule', 'python_single_alt'], $n);
$GLOBALS['medium']['python_alt'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
if (!$messy) {
list($time, $result) = get_time(['\danog\PrimeModule', 'python_single'], $n);
$GLOBALS['medium']['python'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | python | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
}
2017-02-21 20:37:13 +01:00
2017-02-21 18:35:05 +01:00
list($time, $result) = get_time(['\danog\PrimeModule', 'native_single'], $n);
$GLOBALS['medium']['native'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | native | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram_single'], $n);
$GLOBALS['medium']['wolfram'] += $time;
2017-02-21 20:37:13 +01:00
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
2017-02-21 18:35:05 +01:00
2017-02-21 20:37:13 +01:00
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
2017-02-21 18:35:05 +01:00
}
2017-02-21 20:37:13 +01:00
function random_string($length)
{
if (function_exists('random_bytes')) {
return random_bytes($length);
}
2017-02-21 18:35:05 +01:00
$s = '';
for ($x = 0; $x < $length; $x++) {
$s .= chr(rand(0, 255));
}
2017-02-21 20:37:13 +01:00
2017-02-21 18:35:05 +01:00
return $s;
}
2017-02-21 20:37:13 +01:00
function gen_payload()
{
2017-02-25 02:52:31 +01:00
return \danog\PHP\Struct::pack('<Bqqii', 10, 0, (int) (time() << 32), 20, 1615239032).random_string(16);
2017-02-21 18:35:05 +01:00
}
echo PHP_EOL.'----------- HUGE SEMIPRIME TESTS (100 semiprimes) ----------'.PHP_EOL;
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
$tg = fsockopen('tcp://149.154.167.40:443');
fwrite($tg, chr(239));
stream_set_timeout($tg, 1);
$tot = 100;
for ($x = 0; $x < $tot; $x++) {
fwrite($tg, gen_payload());
2017-02-25 02:52:31 +01:00
$number = \danog\PHP\Struct::unpack('>q', substr(stream_get_contents($tg, 85), 58, 8))[0];
2017-02-21 18:35:05 +01:00
test_single($number);
}
fclose($tg);
foreach ($medium as $type => $total) {
2017-02-21 20:37:13 +01:00
echo $type.': total time '.$total.', medium time '.($total / $tot).PHP_EOL;
2017-02-21 18:35:05 +01:00
}
echo PHP_EOL.'------------------- SMALL MULTIPLE FACTOR TESTS -------------------'.PHP_EOL;
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
foreach ([200, 327, 35, 13589] as $multiple) {
test($multiple);
}
foreach ($medium as $type => $total) {
2017-02-21 20:37:13 +01:00
echo $type.': total time '.$total.', medium time '.($total / 4).PHP_EOL;
2017-02-21 18:35:05 +01:00
}
echo PHP_EOL.'------------------- HUGE SEMIPRIME TESTS (MESSY) ------------------'.PHP_EOL;
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
$m = [1724114033281923457, 2189285106422392999, 3510535493073971807, 1756377470921216651, 1767867620107504387, 2149465210997855797];
foreach ($m as $messy) {
test_single($messy, true);
}
foreach ($medium as $type => $total) {
2017-02-21 20:37:13 +01:00
echo $type.': total time '.$total.', medium time '.($total / count($m)).PHP_EOL;
}