tamerlib/tests/jobs_test.php

38 lines
No EOL
1.1 KiB
PHP

<?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;