From a93da840a420a2958e12687d3a662e505f657e8e Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Mon, 25 Sep 2017 19:10:02 +0200 Subject: [PATCH] pagine di test SAM ed FF separate... test x cancellazione di pulizia orphan data --- appServer/test_ff.php | 141 +++++++++++++++++++++++++++++++++++++++ appServer/test_redis.php | 72 +++++++++++--------- appServer/test_sam.php | 120 +++++++++++++++++++++++++++++++++ 3 files changed, 301 insertions(+), 32 deletions(-) create mode 100644 appServer/test_ff.php create mode 100644 appServer/test_sam.php diff --git a/appServer/test_ff.php b/appServer/test_ff.php new file mode 100644 index 0000000..c3dbff5 --- /dev/null +++ b/appServer/test_ff.php @@ -0,0 +1,141 @@ +

EQN

+Testing procedure Redis per EQN...
+connect("redis01"); +$redis->set_error_function('redis_error'); + +$server = gethostname(); +echo "Server ".$server." inizializzato, proseguo test
"; + +#------------------------------------------------------ +# VALORI DI CONFIGURAZIONE (portare su file ext!!!) +#------------------------------------------------------ +# ScoreList Posizioni +$semaforo = "EQN:USERS:SEMAPHORE"; +$tblPos = "EQN:USERS:POS"; +$tblLast= "EQN:USERS:UPD"; +$tblCanPos = "EQN:CANDIDATE:POS"; +$tblCanLast= "EQN:CANDIDATE:UPD"; +# timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 60 sec (o meno...) +$tOutSem_s = 60; +# età massima dei valori a sistema (40 min x cancellare vecchi...) +$maxAge_m = 40; +# controllo se serve cleanUp +$semOk=0; + +# Salva posizione utente +function salvaPosizione($redis, $tblPos, $tblLast, $User_ID, $Lat, $Lon, $DTime) { + # salvo dataora verifica + $redis->cmd('ZADD', $tblLast, $DTime, $User_ID ) + // # salvo posizione Geolocalizzata + ->cmd('GEOADD',$tblPos, $Lon, $Lat, $User_ID ) + ->set(); +} + +# Verifica SE ci sia semaforo rosso oppure se sia necessario a fine programma fare cleanUp +function checkSemaforo($redis, $semaforo, $tOutSem_s) { + // verifico semaforo... + $answ=$redis->cmd('EXISTS', $semaforo )->get(); + // se NON c'è lo creo + if($answ==0) + { + $redis->cmd('SET', $semaforo, date("YmdHis")) + ->cmd('EXPIRE', $semaforo, $tOutSem_s) + ->set(); + } + return $answ; +} +# effettua pulizia tabelle REDIS UTENTI (POS e UPD) +function cleanUp($redis, $tblPos, $tblLast, $DTime) { + echo "DTime Max: ".$DTime."
"; + // recupero in blocco elenco user_ID vecchi... + $answ=$redis->cmd('ZRANGEBYSCORE', $tblLast, 0, $DTime) + ->set(); + // costruisco stringa comando x dati GEO vecchi + $array=$answ[0]; + # imposto transazione... + $redis->cmd('MULTI'); + # inizio scrittura... + for ($i = 0; $i < count($array); ++$i) { + $redis->cmd('ZREM', $tblPos, $array[$i]); + } + // costruisco stringa comando x calcellazione dati vecchi DTime + $redis->cmd('ZREMRANGEBYSCORE', $tblLast, 0, $DTime); + # chiavo vera esecuzione transazione + $redis->cmd('EXEC')->set(); +} +# rimuove record di un singolo user_ID su richiesta dalle 2 tabelle +function remUserId($redis, $tblPos, $tblLast, $uID) { + $redis->cmd('MULTI'); + $redis->cmd('ZREM', $tblPos, $uID); + $redis->cmd('ZREM', $tblLast, $uID); + $redis->cmd('EXEC')->set(); +} +# 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]; +} +#restituisce gli utenti entro un raggio in KM(!) da certe coordinate +function cntInRadius($redis, $tblPos, $center_lat , $center_lon, $km){ + $answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km')->set(); + return count($answ[0]); +} + +#restituisce latitudine e longitudine medie dei record all'interno di un certo raggio +function avgInRadius($redis, $tblPos, $center_lat , $center_lon, $km){ + $answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km','WITHCOORD')->set(); + $answ=$answ[0]; + + $avg_lat=0; + $avg_lon=0; + for ($i=0;$i0){ + $avg_lat=$avg_lat/count($answ); + $avg_lon=$avg_lon/count($answ); + } + return array($avg_lat,$avg_lon); +} + + +$cnt=cntInRadius($redis,$tblCanPos,19.4,-99.15,30); +var_dump($cnt); +#ECHO "trovati ".$cnt."records
"; +echo "Elapsed time (ms): ".StopWatch::elapsed()."
"; + +$lat_lon=avgInRadius($redis,$tblCanPos,19.4,-99.5,22); +print($lat_lon[0].' '.$lat_lon[1]); + +# mostro infine count record rimasti!!! +$num1=cntRec($redis, $tblLast); +$num2=cntRec($redis, $tblPos); +$num3=cntRec($redis, $tblCanLast); +$num4=cntRec($redis, $tblCanPos); +echo "
Check EQN:USERS: ".$num1." valori in ".$tblLast." | ".$num2." valori in ".$tblPos."
"; +echo "
Check EQN:USERS: ".$num3." valori in ".$tblCanLast." | ".$num4." valori in ".$tblCanPos."
"; +echo "Elapsed time (ms): ".StopWatch::elapsed()."
"; + + +// 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 +?> diff --git a/appServer/test_redis.php b/appServer/test_redis.php index ae2ee86..c3dbff5 100644 --- a/appServer/test_redis.php +++ b/appServer/test_redis.php @@ -26,6 +26,8 @@ echo "Server ".$server." inizializzato, proseguo test
"; $semaforo = "EQN:USERS:SEMAPHORE"; $tblPos = "EQN:USERS:POS"; $tblLast= "EQN:USERS:UPD"; +$tblCanPos = "EQN:CANDIDATE:POS"; +$tblCanLast= "EQN:CANDIDATE:UPD"; # timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 60 sec (o meno...) $tOutSem_s = 60; # età massima dei valori a sistema (40 min x cancellare vecchi...) @@ -88,40 +90,47 @@ function cntRec($redis, $tblReq) { // ritorno valore return $answ[0]; } - -# Genero un UID univoco con nome server (numerico) + numero casuale -$uID = substr($server, 2, 2).rand(0,999999999); -# genero coordinate casuali LAT/LON -$cLat = rand(-84,84); -$cLon = rand(-179,179); -$currDT = date("YmdHis"); - -# in primis check semaforo SE debba fare pulizia -$semOk=checkSemaforo($redis, $semaforo, $tOutSem_s); -$num=cntRec($redis, $tblLast); -echo"Semaforo: ".$semOk." | Trovati ".$num." valori in REDIS
"; -echo "Elapsed time: ".StopWatch::elapsed()."
"; -echo "
"; - -# 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 "Elapsed time: ".StopWatch::elapsed()."
"; -echo "
"; - -# verifico semaforo $tOutPos2Db (se devo svuotare redis) -if($semOk==0) -{ - $maxDT = date("YmdHis",strtotime("-".$maxAge_m." min")); - cleanUp($redis, $tblPos, $tblLast, $maxDT); - $num=cntRec($redis, $tblLast); +#restituisce gli utenti entro un raggio in KM(!) da certe coordinate +function cntInRadius($redis, $tblPos, $center_lat , $center_lon, $km){ + $answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km')->set(); + return count($answ[0]); } +#restituisce latitudine e longitudine medie dei record all'interno di un certo raggio +function avgInRadius($redis, $tblPos, $center_lat , $center_lon, $km){ + $answ=$redis->cmd('GEORADIUS', $tblPos, $center_lon, $center_lat, $km, 'km','WITHCOORD')->set(); + $answ=$answ[0]; + + $avg_lat=0; + $avg_lon=0; + for ($i=0;$i0){ + $avg_lat=$avg_lat/count($answ); + $avg_lon=$avg_lon/count($answ); + } + return array($avg_lat,$avg_lon); +} + + +$cnt=cntInRadius($redis,$tblCanPos,19.4,-99.15,30); +var_dump($cnt); +#ECHO "trovati ".$cnt."records
"; +echo "Elapsed time (ms): ".StopWatch::elapsed()."
"; + +$lat_lon=avgInRadius($redis,$tblCanPos,19.4,-99.5,22); +print($lat_lon[0].' '.$lat_lon[1]); + # mostro infine count record rimasti!!! -$num=cntRec($redis, $tblLast); -echo "
Fatto! ".$num." valori rimasti
"; -echo "Elapsed time: ".StopWatch::elapsed()."
"; +$num1=cntRec($redis, $tblLast); +$num2=cntRec($redis, $tblPos); +$num3=cntRec($redis, $tblCanLast); +$num4=cntRec($redis, $tblCanPos); +echo "
Check EQN:USERS: ".$num1." valori in ".$tblLast." | ".$num2." valori in ".$tblPos."
"; +echo "
Check EQN:USERS: ".$num3." valori in ".$tblCanLast." | ".$num4." valori in ".$tblCanPos."
"; +echo "Elapsed time (ms): ".StopWatch::elapsed()."
"; // esempio: @@ -130,4 +139,3 @@ echo "Elapsed time: ".StopWatch::elapsed()."
"; // https://stackoverflow.com/questions/33196237/how-to-set-expire-when-using-redis-geoadd // https://redis.io/commands/zremrangebyrank ?> - diff --git a/appServer/test_sam.php b/appServer/test_sam.php new file mode 100644 index 0000000..d838ae9 --- /dev/null +++ b/appServer/test_sam.php @@ -0,0 +1,120 @@ +

EQN

+Testing procedure Redis per EQN...
+connect("redis01"); +$redis->set_error_function('redis_error'); + +#------------------------------------------------------ +# VALORI DI CONFIGURAZIONE (portare su file ext!!!) +#------------------------------------------------------ +# ScoreList Posizioni +$semaforo = "EQN:USERS:SEMAPHORE_TEST"; +$tblPos = "EQN:USERS:POS"; +$tblLast = "EQN:USERS:UPD"; +$tblCanPos = "EQN:CANDIDATE:POS"; +$tblCanLast = "EQN:CANDIDATE:UPD"; +# timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 60 sec (o meno...) +$tOutSem_s = 10; +# età massima dei valori a sistema (40 min x cancellare vecchi...) +$maxAge_m = 40; +# controllo se serve cleanUp +$semOk=0; + +# Verifica SE ci sia semaforo rosso oppure se sia necessario a fine programma fare cleanUp +function checkSemaforo($redis, $semaforo, $tOutSem_s) { + // verifico semaforo... + $answ=$redis->cmd('EXISTS', $semaforo )->get(); + // se NON c'è lo creo + if($answ==0) + { + $redis->cmd('SET', $semaforo, date("YmdHis")) + ->cmd('EXPIRE', $semaforo, $tOutSem_s) + ->set(); + } + return $answ; +} +function my_array_diff($a, $b) { + $map = $out = array(); + foreach($a as $val) $map[$val] = 1; + foreach($b as $val) if(isset($map[$val])) $map[$val] = 0; + foreach($map as $val => $ok) if($ok) $out[] = $val; + return $out; +} +# effettua pulizia degli ORFANI tabelle REDIS UTENTI (POS che eccedono gli UPD) +function cleanUpOrphan($redis, $tblPos, $tblLast) { + echo "Chiamato CleanupOrphan
"; + // recupero in blocco elenco user_ID da POS e da UPD... + $answ=$redis->cmd('ZRANGE', $tblPos, 0, -1) + ->cmd('ZRANGE', $tblLast, 0, -1) + ->set(); + $ar2del=my_array_diff($answ[0],$answ[1]); + #$ar2del=array_diff($answ[0],$answ[1]); + #print_r($ar2del); + + # solo se ci sono differenze... + if(count($ar2del)>0) + { + # imposto transazione... + $redis->cmd('MULTI'); + $task=""; + # inizio scrittura... + for ($i = 0; $i < count($ar2del); ++$i) { + $redis->cmd('ZREM', $tblPos, $ar2del[$i]); + $task.='ZREM | '. $tblPos." | ".$ar2del[$i]."
"; + } + echo "Effettuata pulizia tabella:
".$task."
"; + # chiamo vera esecuzione transazione + $redis->cmd('EXEC')->set(); + } + else + { + ECHO "Tabelle allineate, nulla da cancellare..."; + } +} +# 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]; +} +# in primis check semaforo SE debba fare pulizia +$semOk=checkSemaforo($redis, $semaforo, $tOutSem_s); +# verifico semaforo $tOutPos2Db (se devo svuotare redis) +if($semOk==0) +{ + # effettuo scan dei CANDITATE POS, se NON li trovo in UPD li ELIMINO... + cleanUpOrphan($redis, $tblCanPos, $tblCanLast); +} + +# mostro infine count record rimasti!!! +$num1=cntRec($redis, $tblLast); +$num2=cntRec($redis, $tblPos); +$num3=cntRec($redis, $tblCanLast); +$num4=cntRec($redis, $tblCanPos); +echo "
Check EQN:USERS: ".$num1." valori in ".$tblLast." | ".$num2." valori in ".$tblPos."
"; +echo "
Check EQN:USERS: ".$num3." valori in ".$tblCanLast." | ".$num4." valori in ".$tblCanPos."
"; +echo "Elapsed time (ms): ".StopWatch::elapsed()."
"; + + + + +// 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 +?> +