aggiunta classe stopwatch e avanzamento x gestione timing...
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
class StopWatch {
|
||||
/**
|
||||
* @var $startTimes array The start times of the StopWatches
|
||||
*/
|
||||
private static $startTimes = array();
|
||||
|
||||
/**
|
||||
* Start the timer
|
||||
*
|
||||
* @param $timerName string The name of the timer
|
||||
* @return void
|
||||
*/
|
||||
public static function start($timerName = 'default') {
|
||||
self::$startTimes[$timerName] = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the elapsed time in seconds
|
||||
*
|
||||
* @param $timerName string The name of the timer to start
|
||||
* @return float The elapsed time since start() was called
|
||||
*/
|
||||
public static function elapsed($timerName = 'default') {
|
||||
return microtime(true) - self::$startTimes[$timerName];
|
||||
}
|
||||
}
|
||||
?>
|
||||
+36
-22
@@ -3,6 +3,10 @@ Testing procedure Redis per EQN...
|
||||
<?php
|
||||
|
||||
require 'redis.php';
|
||||
require 'stopWatch.php';
|
||||
|
||||
# avvio misura tempi...
|
||||
StopWatch::start();
|
||||
|
||||
function redis_error($error) {
|
||||
// throw new error($error);
|
||||
@@ -12,7 +16,8 @@ $redis = new redis_cli ();
|
||||
$redis->connect("redis01");
|
||||
$redis->set_error_function('redis_error');
|
||||
|
||||
echo "Server inizializzato, proseguono test<hr/>";
|
||||
$server = gethostname();
|
||||
echo "Server ".$server." inizializzato, proseguo test<hr/>";
|
||||
|
||||
#------------------------------------------------------
|
||||
# VALORI DI CONFIGURAZIONE (portare su file ext!!!)
|
||||
@@ -24,11 +29,11 @@ $tblLast= "EQN:USERS:UPD";
|
||||
# timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 1 min (poi 5?)
|
||||
$tOutSemaforo = 30;
|
||||
# età massima dei valori a sistema (2 minuti, in prod 30!!!)
|
||||
$maxAgePos = 1;
|
||||
$maxAgePos = 30;
|
||||
# num record da processare in blocco
|
||||
$nBlockRec = 10;
|
||||
# controllo se serve cleanUp
|
||||
$doClean=0;
|
||||
$semOk=0;
|
||||
|
||||
# definizione funzioni helper x gestione REDIS dell'oggetto (UserID = numero, DTime in intero fino ai minuti yyMMddHHmm)
|
||||
|
||||
@@ -46,7 +51,6 @@ function checkSemaforo($redis, $semaforo, $tOutSemaforo) {
|
||||
// verifico semaforo...
|
||||
$answ=$redis->cmd('EXISTS', $semaforo )->get();
|
||||
// se NON c'è lo creo
|
||||
#echo "check semaforo: answ: ".$answ;
|
||||
if($answ==0)
|
||||
{
|
||||
$redis->cmd('SET', $semaforo, date("YmdHi"))
|
||||
@@ -54,24 +58,22 @@ function checkSemaforo($redis, $semaforo, $tOutSemaforo) {
|
||||
->set();
|
||||
}
|
||||
return $answ;
|
||||
|
||||
}
|
||||
|
||||
# effettua pulizia tabelle REDIS UTENTI (POS e UPD)
|
||||
function cleanUp($redis, $tblPos, $tblLast, $DTime) {
|
||||
// chiamo IN BLOCCO cancellazione dati vecchi (prima GEO e poi DTime)
|
||||
|
||||
// esempio:
|
||||
// http://srv.earthquakenetwork.it/test_redis.php
|
||||
// https://github.com/ziogas/PHP-Redis-implementation
|
||||
// https://stackoverflow.com/questions/33196237/how-to-set-expire-when-using-redis-geoadd
|
||||
// https://redis.io/commands/zremrangebyrank
|
||||
|
||||
// chiamo IN BLOCCO calcellazione dati vecchi (prima GEO e poi DTime)
|
||||
echo "DTime Max: ".$DTime."<br/>";
|
||||
// cerco i dati NUOVI (per DB) da persistere su DB $nBlockRec alla volta...
|
||||
}
|
||||
|
||||
# conta quanti record ci siano nelle tab indicata
|
||||
function cntRec($redis, $tblReq) {
|
||||
// conteggio!
|
||||
$answ=$redis->cmd('ZCOUNT', $tblReq, '-inf', '+inf')->set();
|
||||
// ritorno valore
|
||||
return $answ[0];
|
||||
}
|
||||
|
||||
# Genero un UID univoco con nome server (numerico) + numero casuale
|
||||
$server = gethostname();
|
||||
$uID = substr($server, 2, 2).rand(0,999999999);
|
||||
# genero coordinate casuali LAT/LON
|
||||
$cLat = rand(-80,80);
|
||||
@@ -79,20 +81,32 @@ $cLon = rand(-170,170);
|
||||
$currDT = date("YmdHi");
|
||||
|
||||
# in primis check semaforo SE debba fare pulizia
|
||||
$doClean=checkSemaforo($redis, $semaforo, $tOutSemaforo);
|
||||
$semOk=checkSemaforo($redis, $semaforo, $tOutSemaforo);
|
||||
$num=cntRec($redis, $tblLast);
|
||||
echo"Semaforo: ".$semOk." | Trovati ".$num." valori in REDIS<br/>";
|
||||
echo "Elapsed time: ".StopWatch::elapsed()." <br/>";
|
||||
echo "<hr/>";
|
||||
|
||||
# effettuo update informazione posizione dell'utente (insert/update)
|
||||
salvaPosizione($redis, $tblPos, $tblLast, $uID, $cLat, $cLon, $currDT);
|
||||
|
||||
echo"Valore salvato uID: ".$uID." | LAT: ".$cLat." | LON: ".$cLon." | DT: ".$currDT;
|
||||
echo"Valore salvato uID: ".$uID." | LAT: ".$cLat." | LON: ".$cLon." | DT: ".$currDT."<br/>";
|
||||
echo "Elapsed time: ".StopWatch::elapsed()." <br/>";
|
||||
echo "<hr/>";
|
||||
|
||||
# verifico semaforo $tOutPos2Db (se devo svuotare e aggiornare DB da redis)
|
||||
$maxDT = date("YmdHi");
|
||||
cleanUp($redis, $tblPos, $tblLast, $maxDT);
|
||||
if($semOk==0)
|
||||
{
|
||||
$maxDT = date("YmdHi",strtotime("-30 min"));
|
||||
cleanUp($redis, $tblPos, $tblLast, $maxDT);
|
||||
$num=cntRec($redis, $tblLast);
|
||||
echo"Eseguito cleanUp: rimangono ".$num." valori in REDIS";
|
||||
echo "<hr/>";
|
||||
}
|
||||
|
||||
# mostro infine count record rimasti!!!
|
||||
|
||||
|
||||
echo "<hr/>fatto!<br/>";
|
||||
echo "Elapsed time: ".StopWatch::elapsed()." <br/>";
|
||||
|
||||
?>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user