Jump to content
Sign in to follow this  
cristiant

script ajax: problema utilizzo cpu

Recommended Posts

Ciao ragazzi, ho bisogno di un aiuto se qualche buon'anima me lo può fornire. Utilizzo una shoutbox ajax, ma il grande numero di accessi contemporanei al mio sito (circa 3000 unici contemporanei) mi ha provocato un problema con la cpu del server. Assieme ai ragazzi dell'assistenza abbiamo individuato un primo fattore problematico, ovvero il troppo frequente refresh della shoutbox (che era settato ogni 3 secondi e ora abbiamo portato a 2 minuti, limitando così il numero di chiamate al server. Siamo passati da oltre un milione al minuto a 50mila).

Ora però si pone un altro problema, ottimizzare lo script, cercando di eliminare ogni funzione superflua, guadagnando così risorse in termini di ram e cpu utilizzata.

Il suggerimento che mi è stato dato è quello di migliorare la parte in cui si richiama una funzione random, ma sinceramente io non so da che parte iniziare.

Vi allego qui sotto il codice dello script, sperando in un vostro aiuto:

 

<script language="javascript" type="text/javascript">

<!--

var request;

var lastload=0;

var refreshTime=3000; // 3 seconds

function createRequest()

{

try {

request = new XMLHttpRequest();

} catch (trymicrosoft) {

try {

request = new ActiveXObject("Msxml2.XMLHTTP");

} catch (othermicrosoft) {

try {

request = new ActiveXObject("Microsoft.XMLHTTP");

} catch (failed) {

request = false;

}

}

}

if (!request)

alert("Error initializing XMLHttpRequest!");

}

 

createRequest();

function sayIt()

{

var msg = document.getElementById("myShout").value;

document.getElementById("myShout").value = "";

var randomnumber=Math.floor(Math.random()*23123); // get rid of pesky caching...

var url = "ajax_command.php?shout=ashout&n="+randomnumber+"&msg="+escape(msg);

request.open("GET", url, true);

request.onreadystatechange = updateSayIt;

request.send(null);

}

function clearShoutStatus()

{

document.getElementById("shoutstatus").innerHTML = "";

}

function updateSayIt()

{

if (request.readyState == 1)

document.getElementById("shoutstatus").innerHTML = "Aggiornamento live.";

if (request.readyState == 4)

{

document.getElementById("shoutstatus").innerHTML = "Aggiornato";

setTimeout('clearShoutStatus();',refreshTime);

getNewShouts();

}

}

function getShouts()

{

var randomnumber=Math.floor(Math.random()*23123); // get rid of pesky caching...

var url = "ajax_command.php?shout=getshouts&n="+randomnumber;

request.open("GET", url, true);

request.onreadystatechange = updateShouts;

request.send(null);

}

function updateShouts()

{

if (request.readyState == 4)

if (request.status == 200) // check server result code...

{

var i;

var messagetime;

var message;

var response = request.responseText.split("~");

 

document.getElementById("myshouts").innerHTML = '';

for(i=0;i<response.length;i+=3) // go thru each element in array.. length -1 needed..

{

message = response;

messagetime = response[i+1];

messagetexttime = response[i+2];

document.getElementById("myshouts").innerHTML += "";

 

document.getElementById("myshouts").innerHTML += "<I>"+message+"</I>";

document.getElementById("myshouts").innerHTML += "<br /><br />";

if(i==0) lastload = messagetime; // use this for when calling updates...

}

setTimeout('getNewShouts();',refreshTime); // run again in 3 seconds..

}

else if (request.status == 404) // check server result code...

alert("Could Not Find Page on Server!");

}

function getNewShouts()

{

//createRequest();

var randomnumber=Math.floor(Math.random()*11); // get rid of pesky caching...

var url = "ajax_command.php?shout=getnewshouts&time="+lastload+"&n="+randomnumber;

// alert(url);

request.open("GET", url, true);

request.onreadystatechange = updateNewShouts;

request.send(null);

}

function updateNewShouts()

{

if (request.readyState == 4)

{

if (request.status == 200) // check server result code...

{

// send a header of a weird number if unable to find page on server...request.status == 999

var i;

var messagetime;

var message;

var newHTML="";

var response = request.responseText.split("~");

 

for(i=0;i<response.length;i+=3) // go thru each element in array.. length -1 needed..

{

message = response;

messagetime = response[i+1];

messagetexttime = response[i+2];

 

newHTML += "<I>"+message+"</I>";

newHTML += "<br /><br />";

document.getElementById("myshouts").innerHTML = newHTML + document.getElementById("myshouts").innerHTML;

if(i==0) lastload = messagetime; // use this for when calling updates...

}

}

else if (request.status == 404) // check server result code...

alert("Could Not Find Page on Server!");

setTimeout('getNewShouts();',refreshTime); // run again in 3 seconds..

}

}

function submitenter(e)

{

var keycode;

if (window.event) keycode = window.event.keyCode;

else if (e) keycode = e.which;

else return true;

 

if (keycode == 13)

{

sayIt();

return false;

}

else

return true;

}

 

//-->

</script>

<body onLoad="getShouts()">

<input type="text" id="myShout" onKeyPress="return submitenter(event)" /> <a onMouseDown="sayIt()">say it</a>

<font size=1 color="#FF0000"><div id=shoutstatus> </div></font>

<div id=myshouts></div>

</body>

Share this post


Link to post
Share on other sites

Ho provato a fare cosi :D

 

<script language="javascript" type="text/javascript">
<!-- 
var request;
var lastload=0;
var refreshTime=200000; // 2 minuti
function createRequest() 
{
 try {
   request = new XMLHttpRequest();
 } catch (trymicrosoft) {
   try {
     request = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (othermicrosoft) {
     try {
       request = new ActiveXObject("Microsoft.XMLHTTP");
     } catch (failed) {
       request = false;
     }
   }
 }
 if (!request)
   alert("Error initializing XMLHttpRequest!");
}

createRequest();
function sayIt()
{
var msg = document.getElementById("myShout").value;
document.getElementById("myShout").value = "";
var randomnumber=Math.floor(Math.random()*23123); // get rid of pesky caching...
var url = "ajax_command.php?shout=ashout&n="+randomnumber+"&  msg="+escape(msg);
request.open("GET", url, true);
request.onreadystatechange = updateSayIt;
request.send(null);
}
function clearShoutStatus()
{
document.getElementById("shoutstatus").innerHTML = "";
}
function updateSayIt()
{
if (request.readyState == 2) 
 document.getElementById("shoutstatus").innerHTML = "Aggiornamento live.";
if (request.readyState == 5)
{
 document.getElementById("shoutstatus").innerHTML = "Aggiornato";
 setTimeout('clearShoutStatus();',refreshTime);
 getNewShouts();
}
}
function getShouts() 
{
var randomnumber=Math.floor(Math.random()*23123); // get rid of pesky caching...
var url = "ajax_command.php?shout=getshouts&n="+randomnumber  ;
request.open("GET", url, true);
request.onreadystatechange = updateShouts;
request.send(null);
}
function updateShouts() 
{
    if (request.readyState == 5)
      if (request.status == 500) // check server result code...
 {
  var i;
  var messagetime;
  var message;
  var response = request.responseText.split("~");

   document.getElementById("myshouts").innerHTML = '';
   for(i=0;i<response.length;i+=3) // go thru each element in array.. length -1 needed..
   {
    message = response[i];
   messagetime = response[i+1];
   messagetexttime = response[i+2];
   document.getElementById("myshouts").innerHTML += "";

    document.getElementById("myshouts").innerHTML += "<I>"+message+"</I>";
   document.getElementById("myshouts").innerHTML += "<br /><br />";
   if(i==0) lastload = messagetime; // use this for when calling updates...
  }
  setTimeout('getNewShouts();',refreshTime); // run again in 3 seconds..
 }
      else if (request.status == 404) // check server result code...
        alert("Could Not Find Page on Server!");
}
function getNewShouts() 
{
//createRequest();
var randomnumber=Math.floor(Math.random()*11); // get rid of pesky caching...
var url = "ajax_command.php?shout=getnewshouts&time="+lastlo  ad+"&n="+randomnumber;
// alert(url);
request.open("GET", url, true);
request.onreadystatechange = updateNewShouts;
request.send(null);
}
function updateNewShouts() 
{
    if (request.readyState == 4)
 {
      if (request.status == 300) // check server result code...
 {
  // send a header of a weird number if unable to find page on server...request.status == 999
  var i;
  var messagetime;
  var message;
  var newHTML="";
  var response = request.responseText.split("~");

   for(i=0;i<response.length;i+=3) // go thru each element in array.. length -1 needed..
   {
    message = response[i];
   messagetime = response[i+1];
   messagetexttime = response[i+2];

    newHTML += "<I>"+message+"</I>";
   newHTML += "<br /><br />";
   document.getElementById("myshouts").innerHTML = newHTML + document.getElementById("myshouts").innerHTML;
   if(i==0) lastload = messagetime; // use this for when calling updates...
  }
 }
 else if (request.status == 404) // check server result code...
  alert("Could Not Find Page on Server!");
 setTimeout('getNewShouts();',refreshTime); // run again in 3 seconds..
}
}
function submitenter(e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
 sayIt();
   return false;
   }
else
   return true;
}

//-->
</script>
<body onLoad="getShouts()">
<input type="text" id="myShout" onKeyPress="return submitenter(event)" /> <a onMouseDown="sayIt()">say it</a>
<font size=1 color="#FF0000"><div id=shoutstatus> </div></font>
<div id=myshouts></div>
</body>         

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×