Login   Register  
PHP Classes

File: examples/case-studies/system/performance/thread_load_balancer.php

Recommend this page to a friend!
  Classes of ASCOOS CMS   Ascoos OS   examples/case-studies/system/performance/thread_load_balancer.php   Download  
File: examples/case-studies/system/performance/thread_load_balancer.php
Role: Example script
Content typex: text/plain
Description: Example script
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change: Update of examples/case-studies/system/performance/thread_load_balancer.php
Date: 1 month ago
Size: 2,870 bytes
 

Contents

Class file image Download
<?php
/**
 * @ASCOOS-NAME : Ascoos OS
 * @ASCOOS-VERSION : 26.0.0
 * @ASCOOS-SUPPORT : [email protected]
 * @ASCOOS-BUGS : https://issues.ascoos.com
 *
 * @desc <English> Dynamically distributes tasks across threads based on current CPU and memory load.
 * @desc <Greek> ????????? ???????? ???????? ?? ?????? ?? ???? ??? ???????? ????? CPU ??? ??????.
 *
 * @since PHP 8.2.0
 */
declare(strict_types=1);

use
ASCOOS\OS\Kernel\Threads\TThreadHandler;
use
ASCOOS\OS\Kernel\Systems\TCoreSystemHandler;

global
$AOS_LOGS_PATH;

// <English> Define configuration for logging and thresholds
// <Greek> ??????? ????????? ??? ????????? ??? ???? ??????
$properties = [
   
'logs' => [
       
'useLogger' => true,
       
'dir' => $AOS_LOGS_PATH . '/',
       
'file' => 'thread_balancer.log'
   
],
   
'thresholds' => [
       
'cpu_max' => 80, // Max CPU load before skipping thread
       
'memory_max' => 85 // Max memory usage before skipping thread
   
]
];

// <English> Initialize system and thread handlers
// <Greek> ???????????? ????????? ?????????? ??? ???????
$systemHandler = new TCoreSystemHandler($properties);
$threadHandler = new TThreadHandler($properties);

// <English> Define a pool of tasks
// <Greek> ??????? ?????? ????????
$tasks = [
    fn() =>
sleep(1),
    fn() =>
file_put_contents('/tmp/task1.txt', 'Task 1 completed'),
    fn() =>
file_put_contents('/tmp/task2.txt', 'Task 2 completed'),
    fn() =>
sleep(2),
    fn() =>
file_put_contents('/tmp/task3.txt', 'Task 3 completed')
];

// <English> Iterate through tasks and assign to threads based on system load
// <Greek> ????????? ???????? ??? ???????? ?? ?????? ????? ?????? ??????????
foreach ($tasks as $index => $task) {
   
$cpuLoad = $systemHandler->get_cpu_load();
   
$memoryStats = $systemHandler->get_memory_stats();
   
$memoryLoad = $memoryStats['percent'];

    if (
$cpuLoad < $properties['thresholds']['cpu_max'] && $memoryLoad < $properties['thresholds']['memory_max']) {
       
$threadHandler->startThread("task_$index", $task);
       
error_log("Thread task_$index started (CPU: $cpuLoad%, Memory: $memoryLoad%)");
    } else {
       
error_log("Thread task_$index skipped due to high load (CPU: $cpuLoad%, Memory: $memoryLoad%)");
    }
}

// <English> Monitor and clean up threads
// <Greek> ????????????? ??? ?????????? ???????
$threadHandler->monitorThreads();

// <English> Free resources
// <Greek> ???????????? ?????
$systemHandler->Free($systemHandler);
$threadHandler->Free($threadHandler);