com phpruntests: phpruntests - update taskScheduler-prototype: code-samples/taskScheduler/classes/taskScheduler.php
code-samples/taskScheduler/example4/main.php code-samples/taskScheduler/example4/taskFileWriter.php code-samples/taskScheduler/run.php

From: Date: Tue, 16 Jun 2009 22:43:30 +0000
Subject: com phpruntests: phpruntests - update taskScheduler-prototype: code-samples/taskScheduler/classes/taskScheduler.php
code-samples/taskScheduler/example4/main.php code-samples/taskScheduler/example4/taskFileWriter.php code-samples/taskScheduler/run.php
Groups: php.qa 
Request: Send a blank email to php-qa+get-66410@lists.php.net to get a copy of this message
Commit: 4127cd748679f8bf2e4abc4968c877138c3703ed Author: Georg Gradwohl <g2@php.net> Tue, 16 Jun 2009 22:43:30 +0000 Parents: 77195c38e5a7149fe425860539f35a803946e826 Branches: master Link: http://git.php.net/?p=phpruntests.git;a=commitdiff;h=4127cd748679f8bf2e4abc4968c877138c3703ed Log: phpruntests - update taskScheduler-prototype Changed paths: M code-samples/taskScheduler/classes/taskScheduler.php A code-samples/taskScheduler/example4/main.php A code-samples/taskScheduler/example4/taskFileWriter.php M code-samples/taskScheduler/run.php Diff: diff --git a/code-samples/taskScheduler/classes/taskScheduler.php b/code-samples/taskScheduler/classes/taskScheduler.php index d379ea6..7452925 100644 --- a/code-samples/taskScheduler/classes/taskScheduler.php +++ b/code-samples/taskScheduler/classes/taskScheduler.php @@ -9,14 +9,15 @@ class taskScheduler const MSG_QUEUE_SIZE = 1024; // max-size of a single message const KILL_CHILD = 'killBill'; // kill-signal to terminate a child - private $taskList = array(); - private $processCount = NULL; - private $inputQueue = NULL; - private $pidStore = array(); - private $time = 0; - private $countPass = 0; - private $countFail = 0; - private $groupTasks = false; + private $taskList = array(); // the list of the tasks to be executed + private $processCount = NULL; // the number of processes + private $inputQueue = NULL; // the input-queue (only used by the sender) + private $pidStore = array(); // stores the pids of all child-processes + private $time = 0; // the needed time + private $countPass = 0; // counts the passed tasks + private $countFail = 0; // counts the failed tasks + private $groupTasks = false; // are the tasks stored in groups? + private $memStore = array(); // stores the mem-usage after an incomming task /** @@ -252,6 +253,8 @@ class taskScheduler } for ($i=0; $i<$limit; $i++) { + + $this->memStore[] = memory_get_usage(true); if (msg_receive($resultQueue, 0, $type, self::MSG_QUEUE_SIZE, $task, true, NULL, $error)) { @@ -273,6 +276,8 @@ class taskScheduler $this->taskList[$index] = $task; logg("RECEIVER store task $index"); } + + } else logg("RECEIVER ERROR $error"); } @@ -352,7 +357,7 @@ class taskScheduler /** * the child is listening to the input-queue and executes the incomming * tasks. afterwards it setts the task-state and sends it back to the - * receiver by the result-queue. + * receiver via the result-queue. * after receiving the kill-signal from the receiver it terminates itself. * * @param int $cid the child-id (default=NULL) @@ -426,6 +431,7 @@ class taskScheduler print "Tasks:\t\t".$count."\n"; } else { + $count = sizeof($this->taskList); print "Tasks:\t\t".$count."\n"; } @@ -437,8 +443,12 @@ class taskScheduler if ($this->processCount > 0) { print "AVG sec/task:\t".round($this->time/$this->processCount,5)."\n"; + print "Memory-MAX:\t".number_format(max($this->memStore))."\n"; + print "Memory-MIN:\t".number_format(min($this->memStore))."\n"; + $avg = array_sum($this->memStore)/sizeof($this->memStore); + print "Memory-AVG:\t".number_format($avg)."\n"; } - + print "----------------------------------------\n"; flush(); } @@ -470,6 +480,30 @@ class taskScheduler } } + + + public function printMemStatistic($int=10) + { + print "MEMORY-USAGE"; + print "\n----------------------------------------\n"; + + $int = ceil(sizeof($this->memStore)/$int); + + $title = "TASK:\t"; + $body = "kB:\t"; + + for ($i=0; $i<sizeof($this->memStore); $i+=$int) { + + $title .= "$i\t"; + $body .= round($this->memStore[$i]/1000)."\t"; + } + + print $title."\n".$body; + + print "\n----------------------------------------\n"; + flush(); + } + } ?> \ No newline at end of file diff --git a/code-samples/taskScheduler/example4/main.php b/code-samples/taskScheduler/example4/main.php new file mode 100644 index 0000000..1fa8a7b --- /dev/null +++ b/code-samples/taskScheduler/example4/main.php @@ -0,0 +1,37 @@ +<?php + +include 'taskFileWriter.php'; + + +function createTaskList($lim=NULL) +{ + if (is_null($lim)) $lim = 5; + + $list = array(); + + for ($j=0; $j<$lim; $j++) { + + $list[$j] = array(); + + $subLim = $lim*($j+1); + + for ($i=0; $i<$subLim; $i++) { + + $d = (($i+$j)%($lim*2)); + $s = 0; + + if ($d == 0) { + $s = 1; + $d++; + } + + $d *= $lim; + + $list[$j][$i] = new taskFileWriter($j.$i, $d, $s); + } + } + + return $list; +} + +?> \ No newline at end of file diff --git a/code-samples/taskScheduler/example4/taskFileWriter.php b/code-samples/taskScheduler/example4/taskFileWriter.php new file mode 100644 index 0000000..775eae3 --- /dev/null +++ b/code-samples/taskScheduler/example4/taskFileWriter..php @@ -0,0 +1,51 @@ +<?php + +class taskFileWriter extends task implements taskInterface +{ + private $id = NULL; + private $durations = 1; + private $sleep = 0; + + private $txt = "Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; + + + public function __construct($id, $durations, $sleep) + { + $this->id = $id; + $this->durations = $durations; + $this->sleep = $sleep; + } + + + public function run() + { + $name = 'example4/files/tmp_'.$this->id.'.txt'; + $size = 0; + + for ($i=0; $i<=$this->durations; $i++) { + + $size += file_put_contents($name, $this->txt, FILE_APPEND | LOCK_EX); + } + + file_get_contents($name); + + $x = exp(asin(atan2($size, microtime(true)))); + + sleep($this->sleep); + + $y = sqrt(acosh(atan2($x, microtime(true)))); + + if ($x == $y) { + + $this->setMessage('impossible...'); + return false; + } + + unlink($name); + + return true; + } +} + + +?> \ No newline at end of file diff --git a/code-samples/taskScheduler/run.php b/code-samples/taskScheduler/run.php index be988d5..29b8e17 100644 --- a/code-samples/taskScheduler/run.php +++ b/code-samples/taskScheduler/run.php @@ -25,10 +25,11 @@ function logg($msg) { $argc = sizeof($argv); -if ($argc == 2 || $argc == 3) { +if ($argc >= 2 || $argc <= 3) { $src = $argv[1]; $count = isset($argv[2]) ? $argv[2] : NULL; + } else { die("USAGE: php run.php example processCount\n"); @@ -46,7 +47,7 @@ if (!file_exists($src)) { include $src; -$taskList = createTaskList(); +$taskList = createTaskList($count); // init scheduler @@ -61,6 +62,8 @@ $c->printStatistic(); $c->printFailedTasks(); +$c->printMemStatistic(10); + exit(0); ?>

« previous php.qa (#66410) next »