Files
EQN/appServer/test_redis.php
T
2017-09-23 00:21:26 +02:00

140 lines
4.4 KiB
PHP

<h1>EQN</h1>
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);
}
$redis = new redis_cli ();
$redis->connect("redis01");
$redis->set_error_function('redis_error');
$server = gethostname();
echo "Server ".$server." inizializzato, proseguo test<hr/>";
#------------------------------------------------------
# VALORI DI CONFIGURAZIONE (portare su file ext!!!)
#------------------------------------------------------
# ScoreList Posizioni
$semaforo = "EQN:USERS:SEMAPHORE";
$tblPos = "EQN:USERS:POS";
$tblLast= "EQN:USERS:UPD";
# timeout (x semaforo) IN SECONDI x gestione update POSIZIONI sul DB (cancello vecchi, inserisco nuovi): 1 min (poi 5?)
$tOutSemaforo = 15;//30;
# età massima dei valori a sistema (2 minuti, in prod 30!!!)
$maxAgePos = 30;
# num record da processare in blocco
$nBlockRec = 10;
# 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, $tOutSemaforo) {
// verifico semaforo...
$answ=$redis->cmd('EXISTS', $semaforo )->get();
// se NON c'è lo creo
if($answ==0)
{
$redis->cmd('SET', $semaforo, date("YmdHi"))
->cmd('EXPIRE', $semaforo, $tOutSemaforo)
->set();
}
return $answ;
}
# effettua pulizia tabelle REDIS UTENTI (POS e UPD)
function cleanUp($redis, $tblPos, $tblLast, $DTime) {
echo "DTime Max: ".$DTime."<br/>";
// recupero in blocco elenco user_ID vecchi...
$answ=$redis->cmd('ZRANGEBYSCORE', $tblLast, 0, $DTime)
->set();
#var_dump($answ);
#var_dump($answ[0]);
// costruisco stringa comando x dati GEO vecchi
$array=$answ[0];
# inizio scrittura...
for ($i = 0; $i < count($array); ++$i) {
# echo $answ[0][$i]."<br/>";
$redis->cmd('ZREM', $tblPos, $answ[0][$i])->set();
}
// costruisco stringa comando x calcellazione dati vecchi DTime
$redis->cmd('ZREMRANGEBYSCORE', $tblLast, 0, $DTime)->set();
// cerco i dati NUOVI (per DB) da persistere su DB $nBlockRec alla volta...
// insert into notifications_redis (enabled,user_id,latitude,longitude) values (1,$user_id,$user_latitude,$user_longitude)
//
// enabled non serve più ma è settato a 1 per compatibilità all'indietro. Per estrarre è
//
// select user_id,latitude,longitude from notifications_redis
// 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
}
# 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
$uID = substr($server, 2, 2).rand(0,999999999);
# genero coordinate casuali LAT/LON
$cLat = rand(-84,84);
$cLon = rand(-179,179);
$currDT = date("YmdHi");
# in primis check semaforo SE debba fare pulizia
$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."<br/>";
echo "Elapsed time: ".StopWatch::elapsed()." <br/>";
echo "<hr/>";
# verifico semaforo $tOutPos2Db (se devo svuotare e aggiornare DB da redis)
if($semOk==0)
{
# lasciato x fare test: elimino se + vecchi di 1 minuti...
#$maxDT = date("YmdHi",strtotime("-1 min"));
$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!!!
$num=cntRec($redis, $tblLast);
echo "<hr/>Fatto! ".$num." valori rimasti<br/>";
echo "Elapsed time: ".StopWatch::elapsed()." <br/>";
?>