I heard a lot about how fast NodeJS is. But my simple test case shows, that Apache with PHP is much faster. The codes look like this:
PHP code
require_once("mysqlconnect.php");
$start = round(microtime(true) * 1000);
$r = mysql_query("SELECT field1 FROM mytable");
$arr = array();
while($s = mysql_fetch_array($r)){
$arr[] = $s;
}
$obj = json_encode($arr);
$end = round(microtime(true) * 1000);
echo $end-$start;
NodeJS
var mysql = require('mysql'),
start = new Date().getTime(),
connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'testdb'
}),
json = '',
query = 'SELECT field1 FROM mytable';
connection.connect();
connection.query(query, function(err, results, fields) {
json = JSON.stringify(results);
var elapsed = new Date().getTime() - start;
console.log(elapsed);
connection.end();
});
The table contains 1000 rows and ten tests show that PHP even without any accelerators is at least 3 times faster. Why is that? Does it mean that NodeJS beats PHP only in some special cases?
Related
So I'm trying to fetch a points table for users to add points in by garnering their total points and adding it with the installation points, however by trying to fetch their "latest" points, new users who do not have an existing row in the table will throwback an error "InvalidArgumentException; Data Missing". This would be my code below, and are there any other ways around this?
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
$points['points_total'] = $currentpoint + $battery['point_installation'];
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first(); of your code return an object and you are try to perform math (addition) operation on that object which is not possible. You can update your code like below.
$currentpoint = Points::where('user_id', $input['user_id'])->latest()->first();
$points['user_id'] = $input['user_id'];
$points['points_add'] = $battery['point_installation'];
$points['points_subtract'] = 0;
if($currentpoint != null)
{
$points['points_total'] = $currentpoint->points_total + $battery['point_installation'];
}
else {
$points['points_total'] = $battery['point_installation'];
}
$points['points_source_id'] = 1;
$points['created_at'] = $mytime;
$points['updated_at'] = $mytime;
$point = Points::create($points);
I made this code with:
https://github.com/influxdata/influxdb-php
https://www.highcharts.com/docs/working-with-data/live-data
<?php
include('/opt/lampp/htdocs/example/vendor/autoload.php');
$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';
/*
$client = new \InfluxDB\Client($host, $port);
$database = $client->selectDB('aTimeSeries');
*/
//the exact same thing
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass#%s:%s/%s', $host, $port, $dbname));
//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');
//recup le point
$points = $result->getPoints();
// Set the JSON header
header("Content-type: application/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000.
$x = time() * 1000;
// The y value is a random number
//$y = rand(0,100);
$y = $points;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
Output:
[1523603506000,[{"time":"2018-04-13T07:11:45.208943754Z","value":48}]]
But I would like this:
[1523603506000,48]
Or this:
[2018-04-13T07:11:45.208943754Z,48]
If I try to output $points only, I got only the last part of the array, but it is not what I would like either.
PS: if you have a better solution to do the same thing, maybe with nodeJS, I will surely listen
Thanks for your help,
I had another problem, but I solved it.
But still have 2 hours of lag, but it display right in highcharts now.
<?php
include('/opt/lampp/htdocs/example/vendor/autoload.php');
$host = '127.0.0.1';
$port = 8086;
$dbname = 'aTimeSeries';
//directly get the database object
$database = \InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass#%s:%s/%s', $host, $port, $dbname));
//query of the last value
$result = $database->query('select * from valeurs group by * order by desc limit 1');
//get the point
$points = $result->getPoints();
//make the array right
$points = json_encode($points);
$points = json_decode($points);
//take only the seconds of the time
$points[0]->time = substr($points[0]->time, 0,20);
// Set the JSON header
header("Content-type: text/json");
// The x value is the current JavaScript time, which is the Unix time multiplied by 1000 and take the time
$x = strtotime($points[0]->time) * 1000;
// The y value take the value which is a random value
$y = $points[0]->value;
// Create a PHP array and echo it as JSON
$ret = array($x, $y);
echo json_encode($ret);
?>
I usually work with web hosting companies but I decided to start learning working with servers to expand my knowledge.
I'll better give a real example to explain my question the best:
I have a web application that gathers data from a slow API that returns JSON data of products.
I have a function running every 1AM running a lot of queries on "id"s in my database.
Crontab:
0 1 * * * cd /var/www/html/tools; php index.php aso Cli_kas kas_alert
So this creates a process for the app (please correct me here if I'm wrong) and each process creates threads, and just to be more accurate, they are multi-threads since they do more than one thing: like pulling data from the DB to get the right variables and string them to the API queries, getting the data from the API, organizing it, searching the relevant data, and then inserting new data to the database.
The main PHP functions:
// MAIN: Cron Job Function
public function kas_alert() {
// 0. Deletes all the saved data from the `data` table 1 month+ ago.
// $this->kas_model->clean_old_rows();
// 1. Get 'prod' table
$data['table'] = $this->kas_model->prod_table();
// 2. Go through each row -
foreach ( $data['table'] as $row ) {
// 2.2. Gets all vars from the first query.
$last_row_query = $this->kas_model->get_last_row_of_tag($row->tag_id);
$last_row = $last_row_query[0];
$l_aaa_id = $last_row->prod_aaa_id;
$l_and_id = $last_row->prod_bbb_id;
$l_r_aaa = $last_row->dat_data1_aaa;
$l_r_and = $last_row->dat_data1_bbb;
$l_t_aaa = $last_row->dat_data2_aaa;
$l_t_and = $last_row->dat_data2_bbb;
$tagword = $last_row->tag_word;
$tag_id = $last_row->tag_id;
$country = $last_row->kay_country;
$email = $last_row->u_email;
$prod_name = $last_row->prod_name;
// For the Weekly report:
$prod_id = $last_row->prod_id;
$today = date('Y-m-d');
// 2.3. Run the tagword query again for today on each one of the tags and insert to DB.
if ( ($l_aaa_id != 0) || ( !empty($l_aaa_id) ) ) {
$aaa_data_today = $this->get_data1_aaa_by_id_and_kw($l_aaa_id, $tagword, $country);
} else{
$aaa_data_today['data1'] = 0;
$aaa_data_today['data2'] = 0;
$aaa_data_today['data3'] = 0;
}
if ( ($l_and_id != 0) || ( !empty($l_and_id) ) ) {
$bbb_data_today = $this->get_data1_bbb_by_id_and_kw($l_and_id, $tagword, $country);
} else {
$bbb_data_today['data1'] = 0;
$bbb_data_today['data2'] = 0;
$bbb_data_today['data3'] = 0;
}
// 2.4. Insert the new variables to the "data" table.
if ($this->kas_model->insert_new_tag_to_db( $tag_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $aaa_data_today['data2'], $bbb_data_today['data2'], $aaa_data_today['data3'], $bbb_data_today['data3']) ){
}
// Kas Alert Outputs ($SEND is echoed in it's original function)
echo "<h1>prod Name: $prod_id</h1>";
echo "<h2>tag id: $tag_id</h2>";
var_dump($aaa_data_today);
echo "aaa old: ";
echo $l_r_aaa;
echo "<br> aaa new: ";
echo $aaa_data_today['data1'];
var_dump($bbb_data_today);
echo "<br> bbb old: ";
echo $l_r_and;
echo "<br> bbb new: ";
echo $bbb_data_today['data1'];
// 2.5. Check if there is a need to send something
$send = $this->check_if_send($l_aaa_id, $l_and_id, $l_r_aaa, $aaa_data_today['data1'], $l_r_and, $bbb_data_today['data1']);
// 2.6. If there is a trigger, send the email!
if ($send) {
$this->send_mail($l_aaa_id, $l_and_id, $aaa_data_today['data1'], $bbb_data_today['data1'], $l_r_aaa, $l_r_and, $tagword, $email, $prod_name);
}
}
}
For #Raptor, this is the function that get's the API data:
// aaa tag Query
// Gets aaa prod dataing by ID.
public function get_data_aaa_by_id_and_tg($id, $tag, $query_country){
$tag_for_url = rawurlencode($tag);
$found = FALSE;
$i = 0;
$data = array();
// Create a stream for Json. That's how the code knows what to expect to get.
$context_opts = array(
'http' => array(
'method' => "GET",
'header' => "Accepts: application/json\r\n"
));
$context = stream_context_create($context_opts);
while ($found == FALSE) {
// aaa Query
$json_query_aaa = "https://api.example.com:443/aaa/ajax/research_tag?app_id=$id&term=$tag_for_url&page_index=$i&country=$query_country&auth_token=666";
// Get the Json
$json_query_aaa = file_get_contents($json_query_aaa, false, $context);
// Turn Json to a PHP array
$json_query_aaa = json_decode($json_query_aaa, true);
// Get the data2
$data2 = $json_query_aaa['tag']['data2'];
if (is_null($data2)){ $data2 = 0; }
// Get data3
$data3 = $json_query_aaa['tag']['phone_prod']['data3'];
if (is_null($data3)){ $data3 = 0; }
// Finally, the main prod array.
$json_query_aaa = $json_query_aaa['tag']['phone_prod']['app_list'];
if ( count($json_query_aaa) > 2 ) {
for ( $j=0; $j<count($json_query_aaa); $j++ ) {
if ( $json_query_aaa[$j]['id'] == $id ) {
$found = TRUE;
$data = $json_query_aaa[$j]['data'] + 1;
break;
}
if ($found == TRUE){
break;
}
}
$i++;
} else {
$data = 0;
break;
}
}
$data['data1'] = $data;
$data['data2'] = $data2;
$data['data3'] = $data3;
return $data;
}
All threads are stacked one after an other, and when one thread is done, only then - the second thread can proceed, ect'.
And in technical view on this, all threads wait in the RAM until the one before them is done working "inside" the CPU. (correct me if I'm wrong again :] )
This doesn't even "tickle" the servers RAM or CPU when looking at it in the process manager (I use "htop"). RAM is at 400M/4.25G and CPU at ONLY 0.7%-1.3%.
Making me feel this isn't the best I can get from my current server, and getting slow results from my web app.
How do I get things done in a way that all threads work in parallel, but not to a point that my app crashes due to lacks of CPU or RAM?
i have to connect to an api via socket and send / recv some data.
the company send me a php-example-file with this code for reading data from the socket:
function readAnswer() {
$size = fgets($this->socketPtr, 64);
$answer = "";
$readed = 0;
while($readed < $size) {
$part = fread($this->socketPtr, $size - $readed);
$readed += strlen($part);
$answer .= $part;
}
return $answer;
}
This works for me. But in python i get from times to times an error.
not everything from the socket is recv.
my python try looks like this:
def read_answer(self,the_socket,timeout=0.5):
the_socket.setblocking(0)
total_data=[]
data=''
begin=time.time()
while 1:
if total_data and time.time()-begin > timeout:
break
elif time.time()-begin > timeout*2:
break
try:
data = the_socket.recv(8192)
if data:
total_data.append(data)
begin=time.time()
else:
time.sleep(0.1)
except:
pass
return ''.join(total_data)
i recv data as a dict / array. and from time to time i only get a int (msg length i think)
so what would be a better way to read the data from socket.
ah the api sends the data in a correct way, i checked this. it's only this little function ;(
After using the code below (thanks falsetru) and added a readed=len(data) i run into another problem:
this is the working php code:
function _parse_answer($answerData)
{
$result = array();
$lines = explode("\n", $answerData);
$data = explode("&", $lines[0]);
foreach($data as $piece)
{
$keyval = explode("=", $piece, 2);
$result[$keyval[0]] = $keyval[1];
}
for($i=1;$i<count($lines);$i++)
{
$result["csv"][]=$lines[$i];
}
return $result;
}
and this my crappy python code:
def parse_answer(self,data):
#print "dd_demo_api: answer: (%s)" % (data)
if data:
result = {}
lines = data.split("\n")
index_list = 0
if len(lines) == 1:
index_list = 0
else:
index_list = 1
pieces = lines[index_list].split("&")
for x in pieces:
keyval = x.split("=")
result[keyval[0]] = keyval[1]
iterlines = iter(lines)
next(iterlines)
next(iterlines)
count = 1
result["csv"] = {}
for x in iterlines:
result["csv"][count] = x.split(";")
return result
else:
return 0
i think here is some optimization required? ;(
Python version does not do the same thing with PHP version.
Try following code:
def read_answer(self, sock):
size = int(sock.recv(64).strip().rstrip('\0'))
# Above is not exactly same as `fgets`.
# If that causes an issue, use following instead.
#
# f = sock.makefile('r')
# size = int(f.readline(64).rstrip('\0'))
#
# and replace `sock.recv(n)` with `f.read(n)` in the following loop.
total_data = []
readed = 0
while readed < size:
data = sock.recv(size - readed)
if data:
total_data.append(data)
readed += len(data)
return b''.join(total_data)
I am using Smarty with this settings:
$smarty = new Smarty;
$smarty -> caching =3600;
$smarty -> compile_check =true;
$smarty -> compile_dir = 'theme/compile/';
$smarty -> config_dir = 'theme/libs/';
$smarty -> cache_dir = 'theme/cache/';
$smarty -> plugins_dir = 'theme/libs/plugins/';
$smarty->left_delimiter = '{';
$smarty->right_delimiter = '}';
$smarty -> clear_compiled_tpl();
I want to program a simple visitors counter with this function :
function counter() {
$ip = $_SERVER['REMOTE_ADDR'];
$now = time();
$y1 = jgmdate("Y ", $now);
$y1 = (int) $y1;
$m1 = jgmdate("m", $now);
$m1 = (int) $m1;
$d1 = jgmdate("d", $now);
$d1 = (int) $d1;
$result3 = mysql_query("SELECT `times`,`id` FROM `stat_ip` where `IP`='$ip' AND `year`='$y1' AND `month`='$m1' AND `day`='$d1' ;");
unset($n3);
$n3 = (int) mysql_num_rows($result3);
echo $n3;
if ($n3 == 0) {
mysql_query("INSERT INTO `stat_ip` (`id` ,`IP` ,`year` ,`month` ,`day`) VALUES (NULL , '$ip', '$y1', '$m1', '$d1') ;");
} else if ($n3 == 1) {
$row3 = mysql_fetch_array($result3);
$s = (int) $row3['times'] + 1;
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;");
} else {
echo("error");
}
}
everything is ok , but my query executes more than a time in this line(Probably all queries) :
mysql_query("UPDATE `stat_ip` SET `times` = '$s' WHERE `id` = '".$row3['id']."' ;") ;
I think smarty has something to do with my problem!
when I write this code :
$q=$smarty -> fetch('index.tpl');
the query executes 1 time however when I change my code to :
$q=$smarty -> fetch('index.tpl');
echo $q;
or
$smarty -> display('index.tpl');
my queries execute more than one time!:(
for more information:
http://www.smarty.net/forums/viewtopic.php?p=81161
A Common mistake is to have an empty img-tag or script-tag in your html output - this makes the browser re-request the current page.
Why are you calling $smarty->clear_compiled_tpl();?
It does delete all compiled template and cause a recompilation each time a page is called.
Remove this line.
Where and how did you call for function counter?
my problem is solved :
I was using slide show that has this line in java script codes :
I changed the src="#" to src="" at jquery.nivo.slider.js
And it works fine now however I really don't know why browsers work funny !!!
thank you
Smarty is just a template engine, it shouldn't cause problems with the database. Your problem is likely somewhere outside the template engine.