Refactored main class, improved a few things here and there. Looks polished enough

This commit is contained in:
Netkas 2023-06-18 17:17:13 -04:00
parent 75c6062a3e
commit 411898af2a
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
14 changed files with 351 additions and 300 deletions

View file

@ -3,57 +3,44 @@
class ExampleClass
{
/**
* @var array
* Sleeps for the given number of seconds, plus a random number of seconds between 0 and 100.
*
* @param int $seconds
* @return int
* @throws Exception
*/
private $data;
/**
* ExampleClass constructor.
*/
public function __construct()
public function sleep(int $seconds=1): int
{
$this->data = [];
sleep($seconds);
return random_int(0, 100) + $seconds;
}
/**
* Sets a value in the data array
* Calculates pi using the Leibniz formula.
*
* @param int $iterations
* @return float
*/
public function pi(int $iterations): float
{
$pi = 0;
$sign = 1;
for ($i = 0; $i < $iterations; $i++)
{
$pi += $sign / (2 * $i + 1);
$sign *= -1;
}
return $pi * 4;
}
/**
* Throws an exception.
*
* @param string $key
* @param mixed $value
* @return void
* @throws Exception
*/
public function set(string $key, mixed $value): void
public function throwException(): void
{
$this->data[$key] = $value;
}
/**
* Gets a value from the data array
*
* @param string $key
* @return mixed
*/
public function get(string $key): mixed
{
return $this->data[$key];
}
/**
* Checks if a key exists in the data array
*
* @param string $key
* @return bool
*/
public function exists(string $key): bool
{
return isset($this->data[$key]);
}
/**
* @return void
*/
public function clear(): void
{
$this->data = [];
throw new Exception('This is an exception.');
}
}

14
tests/exception_test.php Normal file
View file

@ -0,0 +1,14 @@
<?php
// Import everything
require 'ncc';
require __DIR__ . DIRECTORY_SEPARATOR . 'ExampleClass.php';
import('net.nosial.tamerlib');
// Initialize TamerLib
\TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::CLIENT);
// Start 8 workers.
\TamerLib\tm::createWorker(8, __DIR__ . DIRECTORY_SEPARATOR . 'worker.php');
// Throw an exception, this be thrown in the client.
echo \TamerLib\tm::doWait('throwException');

38
tests/jobs_test.php Normal file
View file

@ -0,0 +1,38 @@
<?php
// Import everything
require 'ncc';
require __DIR__ . DIRECTORY_SEPARATOR . 'ExampleClass.php';
import('net.nosial.tamerlib');
// Initialize TamerLib
\TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::CLIENT);
// Start 8 workers.
\TamerLib\tm::createWorker(12, __DIR__ . DIRECTORY_SEPARATOR . 'worker.php');
// For testing purposes
$total_sleep = 0;
// Run 5 sleep jobs
for($i = 0; $i < 5; $i++)
{
$sleep_time = random_int(1, 5);
\TamerLib\tm::sleep($sleep_time);
$total_sleep += $sleep_time;
}
// Run 30 pi jobs
for($i = 0; $i < 30; $i++)
{
\TamerLib\tm::pi(50);
}
// Wait for all the jobs to complete
$start_time = time();
\TamerLib\tm::wait(static function($job_id, $return){
echo sprintf('Job %s completed with return value %s.', $job_id, $return) . PHP_EOL;
});
// Script ends here once all the jobs are complete.
echo sprintf('Total sleep time: %s seconds.', $total_sleep) . PHP_EOL;
echo sprintf('Total execution time: %s seconds.', time() - $start_time) . PHP_EOL;

View file

@ -1,62 +0,0 @@
<?php
use LogLib\Log;
use TamerLib\Enums\TamerMode;
use TamerLib\Objects\ServerConfiguration;
use TamerLib\tm;
require 'ncc';
import('net.nosial.tamerlib');
// Start as client mode, if no configuration is passed on then
// Tamer will spawn its own Redis server and use it.
tm::initialize(TamerMode::CLIENT);
tm::createWorker(20, __DIR__ . DIRECTORY_SEPARATOR . 'worker.php');
$total_sleep = 0;
$start_time = time();
$jobs = [];
// Start doing programming!
// Loop 2 times, each time we will do a sleep job
for($i = 0; $i <= 2; $i++)
{
$sleep_time = random_int(5, 10);
$total_sleep += $sleep_time;
/** @noinspection PhpUndefinedMethodInspection */
$job_id = tm::sleep($sleep_time);
// Log the result
print(sprintf('Created task %s', $job_id) . PHP_EOL);
$jobs[$job_id] = null;
}
// Loop 200 times, each time we will do a Pi calculation job
for($i = 0; $i < 200; $i++)
{
$iterations = random_int(100000, 1000000);
/** @noinspection PhpUndefinedMethodInspection */
$job_id = tm::calculate_pi($iterations);
// Log the result
print(sprintf('Created task %s', $job_id) . PHP_EOL);
$jobs[$job_id] = null;
}
print('Waiting for jobs to finish...' . PHP_EOL);
// Wait for all jobs to finish
tm::wait(static function ($job_id, $result) use (&$jobs) {
print(sprintf('Job %s finished with result %s', $job_id, $result) . PHP_EOL);
$jobs[$job_id] = $result;
});
// Finally do some fancy calling
var_dump($jobs);
print(sprintf('Total sleep time: %s', $total_sleep) . PHP_EOL);
print(sprintf('Total execution time: %s', time() - $start_time) . PHP_EOL);

View file

@ -1,44 +1,18 @@
<?php
// Import everything
require 'ncc';
require __DIR__ . DIRECTORY_SEPARATOR . 'ExampleClass.php';
import('net.nosial.tamerlib');
// Initialize as a worker, will fail if the process is executed directly
// Initialize TamerLib
\TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::WORKER);
// Callback Examples
\TamerLib\tm::addFunction('sleep', function($sleep_time){
sleep($sleep_time);
return $sleep_time;
});
\TamerLib\tm::addFunction('calculate_pi', function($iterations){
$pi = 0;
$sign = 1;
for($i = 0; $i < $iterations; $i++)
{
$pi += $sign * (1 / (2 * $i + 1));
$sign *= -1;
}
return $pi * 4;
});
// Function pointer examples
// Register the functions
$example_class = new ExampleClass();
\TamerLib\tm::addFunction('getValue', [$example_class, 'get']);
\TamerLib\tm::addFunction('setValue', [$example_class, 'set']);
\TamerLib\tm::addFunction('valueExists', [$example_class, 'exists']);
\TamerLib\tm::addFunction('clearValues', [$example_class, 'clear']);
\TamerLib\tm::addFunction('sleep', [$example_class, 'sleep']);
\TamerLib\tm::addFunction('pi', [$example_class, 'pi']);
\TamerLib\tm::addFunction('throwException', [$example_class, 'throwException']);
// Run forest, run!
while (true)
{
try
{
\TamerLib\tm::run();
}
catch(Exception $e)
{
print($e->getMessage() . PHP_EOL);
}
}
// Run the worker
\TamerLib\tm::run();