I have a small question I would like to ask you guys.
I have a simple script which grabs some xml data from an external source. The script works fine and fast, everything I want from it. The problem is that it is using 107% of the servers CPU (Obviously it is a quad core server, so its 107%/400%). This is way above my limit of 2%.
m not sure how I can fix this, ive tried to make the script as hogless as possible, but its still pretty high.
$xml = simplexml_load_file($getsimiliar) )
foreach( $xml as $similiarartists )
{
$img0 = $similiarartists->artist[0]->image; //Get images for similiar artists
$mbid0 = $similiarartists->artist[0]->mbid; //Get MusicBrainz IDs for similiar artists
$img1 = $similiarartists->artist[1]->image;
$mbid1 = $similiarartists->artist[1]->mbid;
$img2 = $similiarartists->artist[2]->image;
$mbid2 = $similiarartists->artist[2]->mbid;
$img3 = $similiarartists->artist[3]->image;
$mbid3 = $similiarartists->artist[3]->mbid;
$img4 = $similiarartists->artist[4]->image;
$mbid4 = $similiarartists->artist[4]->mbid;
$img5 = $similiarartists->artist[5]->image;
$mbid5 = $similiarartists->artist[5]->mbid;
$img6 = $similiarartists->artist[6]->image;
$mbid6 = $similiarartists->artist[6]->mbid;
$img7 = $similiarartists->artist[7]->image;
$mbid7 = $similiarartists->artist[7]->mbid;
$img8 = $similiarartists->artist[8]->image;
$mbid8 = $similiarartists->artist[8]->mbid;
$img9 = $similiarartists->artist[9]->image;
$mbid9 = $similiarartists->artist[9]->mbid;
if (empty($img0)) //If no image...
{
echo '<img src="'. $img0 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[0]->url .'">'. $similiarartists->artist[0]->name .'</a>';
if ($mbid0 != '') //If no musicbrainzID
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[0]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img1))
{
echo '<img src="'. $img1 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[1]->url .'">'. $similiarartists->artist[1]->name .'</a>';
if ($mbid1 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[1]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img2))
{
echo '<img src="'. $img2 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[2]->url .'">'. $similiarartists->artist[2]->name .'</a>';
if ($mbid2 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[2]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img3))
{
echo '<img src="'. $img3 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[3]->url .'">'. $similiarartists->artist[3]->name .'</a>';
if ($mbid3 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[3]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img4))
{
echo '<img src="'. $img4 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[4]->url .'">'. $similiarartists->artist[4]->name .'</a>';
if ($mbid4 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[4]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img5))
{
echo '<img src="'. $img5 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[5]->url .'">'. $similiarartists->artist[5]->name .'</a>';
if ($mbid5 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[5]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img6))
{
echo '<img src="'. $img6 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[6]->url .'">'. $similiarartists->artist[6]->name .'</a>';
if ($mbid6 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[6]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img7))
{
echo '<img src="'. $img7 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[7]->url .'">'. $similiarartists->artist[7]->name .'</a>';
if ($mbid7 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[7]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img8))
{
echo '<img src="'. $img8 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[8]->url .'">'. $similiarartists->artist[8]->name .'</a>';
if ($mbid8 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[8]->mbid.'">BBC Music</a>';
}
echo '<br />';
if (empty($img9))
{
echo '<img src="'. $img9 .'" />';
}
echo ' <a href="http://'. $similiarartists->artist[9]->url .'">'. $similiarartists->artist[9]->name .'</a>';
if ($mbid9 != '')
{
echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[9]->mbid.'">BBC Music</a>';
}
echo '<br />';
//Bio xml
$xml = simplexml_load_file($getinfo) )
foreach( $xml as $artist )
{
$bio = $artist->bio->summary; //Bio for arist
$image = $artist->image[2]; //Artist image
}
//******** setings ***************************************************************
$user_ID= "************************************"; //Set your lyricsfly.com User ID key
$artist= $currentartist; //Set the artist you want to find lyrics for
$title= $currenttitle; //Set the title for the song you want to find lyrics to
$song_number= 1; //Set to 1 if you want the first result or 2 if you want 2nd result and so on...
//******** setings ***************************************************************
$artist= urlencode(filterChar($artist)); //filter and encode for url string
$title= urlencode(filterChar($title)); //filter and encode for url string
$url="http://lyricsfly.com/api/api.php?i=".$user_ID."&a=".$artist."&t=".$title; //url construction string with pluged in from above values
$html=file_get_contents($url); //retrieve the entire xml page into string
$found_check= GetTag($html,"sg",$song_number); //get song data
if($found_check == "Query not found!") //check if song was found
{
echo "Sorry, lyrics for ". $currenttitle ." are not available in lyricsfly database.<br>"; //... if not print a message
echo "Please <a href='http://lyricsfly.com/submit/'>submit</a> some lyrics for it."; //and print link to submit form
}
elseif($found_check=="Wrong site ID!") //check if site ID is good
{
echo "Sorry, access to lyricsfly API has been terminated please contact the administrator"; //check if user ID access key is valid. Incase we terminate your site ID for any reason your users will see this message. You may optionally put a contact email so that you get notified by someone.
}
elseif($found_check=="Missing site ID!" or $found_check==FALSE) //check if user ID access key is missing and if the number of song titles is not passed the amount which was returned
{
echo "Sorry, the API code was not properly implemented"; //this is mostly for you. You should not see this message once your code is functioning properly.
}
else //finally, if everything check on we retrieve and print the data returned by lyricsfly database
{
$found_checksum= GetTag($html,"cs",$song_number); //get song checksum
$found_ID= GetTag($html,"id",$song_number); //get song ID
$found_title= GetTag($html,"tt",$song_number); //get song title
$found_artist= GetTag($html,"ar",$song_number); //get song artist
$found_album= GetTag($html,"al",$song_number); //get song album
$found_lyrics= GetTag($html,"tx",$song_number); //get song lyrics
$found_lyrics=str_replace("[br]","<br>",$found_lyrics); //replace all "[br]" with "<br>" for html page
$found_lyrics=str_replace("lyricsfly.com","<a href='http://lyricsfly.com'>lyricsfly.com</a>",$found_lyrics); //add html for lyricsfly.com link back
//print album name
echo "$found_lyrics"; //print lyrics
echo "<a href='http://lyricsfly.com/search/correction.php?".$found_checksum."&id=".$found_ID."'>Fix above lyrics here</a><br><br>"; //create a link to correction form for users to be able to correct lyrics
}
Im fairly sure the high CPU Usage is due to the XML Parsing, this may be wrong though =/
If there is ANYTHING you can say to help me or give me advice, I would be more than pleased!
Question
Tjcrazy
I have a small question I would like to ask you guys.
I have a simple script which grabs some xml data from an external source. The script works fine and fast, everything I want from it. The problem is that it is using 107% of the servers CPU (Obviously it is a quad core server, so its 107%/400%). This is way above my limit of 2%.
m not sure how I can fix this, ive tried to make the script as hogless as possible, but its still pretty high.
I was wondering if you guys have any ideas?
//Get the XML document loaded into a variable $xml = simplexml_load_file("http://www.simoncross.com/music/bbcnowplaying/proxy_nowplaying.php?networkId=radio1"); $currentartist = $xml->song[0]->artist; $currenttitle = $xml->song[0]->title; $currenttime = $xml->laststart; $date = date("D, j M Y"); //Recently Played Songs $time = $xml->song[1]->started;$time = str_replace("$date", "", $time);$time = str_replace("+0000", "", $time);$time = date("g:i:s a", strtotime("$time")); $time1 = $xml->song[2]->started;$time1 = str_replace("$date", "", $time1);$time1 = str_replace("+0000", "", $time1);$time1 = date("g:i:s a", strtotime("$time1")); $time2 = $xml->song[3]->started;$time2 = str_replace("$date", "", $time2);$time2 = str_replace("+0000", "", $time2);$time2 = date("g:i:s a", strtotime("$time2")); $time3 = $xml->song[4]->started;$time3 = str_replace("$date", "", $time3);$time3 = str_replace("+0000", "", $time3);$time3 = date("g:i:s a", strtotime("$time3")); $time4 = $xml->song[5]->started;$time4 = str_replace("$date", "", $time4);$time4 = str_replace("+0000", "", $time4);$time4 = date("g:i:s a", strtotime("$time4")); $time5 = $xml->song[6]->started;$time5 = str_replace("$date", "", $time5);$time5 = str_replace("+0000", "", $time5);$time5 = date("g:i:s a", strtotime("$time5")); $time6 = $xml->song[7]->started;$time6 = str_replace("$date", "", $time6);$time6 = str_replace("+0000", "", $time6);$time6 = date("g:i:s a", strtotime("$time6")); $time7 = $xml->song[8]->started;$time7 = str_replace("$date", "", $time7);$time7 = str_replace("+0000", "", $time7);$time7 = date("g:i:s a", strtotime("$time7")); $time8 = $xml->song[9]->started;$time8 = str_replace("$date", "", $time8);$time8 = str_replace("+0000", "", $time8);$time8 = date("g:i:s a", strtotime("$time8")); $parse1 = $xml->song[1]->title .' - '. $xml->song[1]->artist .' - '. $time .'<br />'; $parse2 = $xml->song[2]->title .' - '. $xml->song[2]->artist .' - '. $time1 .'<br />'; $parse3 = $xml->song[3]->title .' - '. $xml->song[3]->artist .' - '. $time2 .'<br />'; $parse4 = $xml->song[4]->title .' - '. $xml->song[4]->artist .' - '. $time3 .'<br />'; $parse5 = $xml->song[5]->title .' - '. $xml->song[5]->artist .' - '. $time4 .'<br />'; $parse6 = $xml->song[6]->title .' - '. $xml->song[6]->artist .' - '. $time5 .'<br />'; $parse7 = $xml->song[7]->title .' - '. $xml->song[7]->artist .' - '. $time6 .'<br />'; $parse8 = $xml->song[8]->title .' - '. $xml->song[8]->artist .' - '. $time7 .'<br />'; $parse9 = $xml->song[9]->title .' - '. $xml->song[9]->artist .' - '. $time8 .'<br />'; //Current Song $currentsong = $currenttitle ." - ". $currentartist; //XML Document for Schedule $xml = 'http://www0.rdthdo.bbc.co.uk/cgi-perl/api/query.pl?method=bbc.schedule.getProgrammes&channel_id=,BBCROne&end=2011-01-16T23:59:59Z&limit=3&detail=schedule'; $xml1 = simplexml_load_file($xml); foreach($xml1->schedule[0]->programme[0]->attributes() as $a => $b) { $currentdj = $b; } foreach($xml1->schedule[0]->programme[1]->attributes() as $a => $b) { $nextdj = $b; } //DJ Times $startcurrent = $xml1->schedule->programme[0]->start; $nexttime = $xml1->schedule->programme[1]->start; $endnext = $xml1->schedule->programme[2]->start; $startcurrent = str_replace('T', '', $startcurrent); $startcurrent = str_replace('Z', '', $startcurrent); $startcurrent = str_replace(date('Y-m-d a'), '', $startcurrent); $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); $tomorrow = date("Y-m-d", $tomorrow); $startcurrent = str_replace($tomorrow, '', $startcurrent); $startcurrent = date("g:ia", strtotime("$startcurrent")); $nexttime = str_replace('T', '', $nexttime); $nexttime = str_replace('Z', '', $nexttime); $nexttime = str_replace(date('Y-m-d a'), '', $nexttime); $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); $tomorrow = date("Y-m-d", $tomorrow); $nexttime = str_replace($tomorrow, '', $nexttime); $nexttime = date("g:ia", strtotime("$nexttime")); $endnext = str_replace('T', '', $endnext); $endnext = str_replace('Z', '', $endnext); $endnext = str_replace(date('Y-m-d a'), '', $endnext); $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); $tomorrow = date("Y-m-d", $tomorrow); $endnext = str_replace($tomorrow, '', $endnext); $endnext = date("g:ia", strtotime("$endnext")); //Strip of ' $currentartist = str_replace("'","",$currentartist); $currenttitle = str_replace("'","",$currenttitle); // Other XML Sources $getinfo = 'http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&artist='.$currentartist.'&track='.$currenttitle.''; $getsimiliar = 'http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist='.$currentartist.'&api_key=c910b0bc1e2d4e64964ebcd2d69c255c'; $getinfo = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&artist='.$currentartist;Further on the script, a bit more XML parsing.
$xml = simplexml_load_file($getsimiliar) ) foreach( $xml as $similiarartists ) { $img0 = $similiarartists->artist[0]->image; //Get images for similiar artists $mbid0 = $similiarartists->artist[0]->mbid; //Get MusicBrainz IDs for similiar artists $img1 = $similiarartists->artist[1]->image; $mbid1 = $similiarartists->artist[1]->mbid; $img2 = $similiarartists->artist[2]->image; $mbid2 = $similiarartists->artist[2]->mbid; $img3 = $similiarartists->artist[3]->image; $mbid3 = $similiarartists->artist[3]->mbid; $img4 = $similiarartists->artist[4]->image; $mbid4 = $similiarartists->artist[4]->mbid; $img5 = $similiarartists->artist[5]->image; $mbid5 = $similiarartists->artist[5]->mbid; $img6 = $similiarartists->artist[6]->image; $mbid6 = $similiarartists->artist[6]->mbid; $img7 = $similiarartists->artist[7]->image; $mbid7 = $similiarartists->artist[7]->mbid; $img8 = $similiarartists->artist[8]->image; $mbid8 = $similiarartists->artist[8]->mbid; $img9 = $similiarartists->artist[9]->image; $mbid9 = $similiarartists->artist[9]->mbid; if (empty($img0)) //If no image... { echo '<img src="'. $img0 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[0]->url .'">'. $similiarartists->artist[0]->name .'</a>'; if ($mbid0 != '') //If no musicbrainzID { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[0]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img1)) { echo '<img src="'. $img1 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[1]->url .'">'. $similiarartists->artist[1]->name .'</a>'; if ($mbid1 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[1]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img2)) { echo '<img src="'. $img2 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[2]->url .'">'. $similiarartists->artist[2]->name .'</a>'; if ($mbid2 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[2]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img3)) { echo '<img src="'. $img3 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[3]->url .'">'. $similiarartists->artist[3]->name .'</a>'; if ($mbid3 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[3]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img4)) { echo '<img src="'. $img4 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[4]->url .'">'. $similiarartists->artist[4]->name .'</a>'; if ($mbid4 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[4]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img5)) { echo '<img src="'. $img5 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[5]->url .'">'. $similiarartists->artist[5]->name .'</a>'; if ($mbid5 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[5]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img6)) { echo '<img src="'. $img6 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[6]->url .'">'. $similiarartists->artist[6]->name .'</a>'; if ($mbid6 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[6]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img7)) { echo '<img src="'. $img7 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[7]->url .'">'. $similiarartists->artist[7]->name .'</a>'; if ($mbid7 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[7]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img8)) { echo '<img src="'. $img8 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[8]->url .'">'. $similiarartists->artist[8]->name .'</a>'; if ($mbid8 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[8]->mbid.'">BBC Music</a>'; } echo '<br />'; if (empty($img9)) { echo '<img src="'. $img9 .'" />'; } echo ' <a href="http://'. $similiarartists->artist[9]->url .'">'. $similiarartists->artist[9]->name .'</a>'; if ($mbid9 != '') { echo '- <a href="http://www.bbc.co.uk/music/artists/'.$similiarartists->artist[9]->mbid.'">BBC Music</a>'; } echo '<br />'; //Bio xml $xml = simplexml_load_file($getinfo) ) foreach( $xml as $artist ) { $bio = $artist->bio->summary; //Bio for arist $image = $artist->image[2]; //Artist image } //******** setings *************************************************************** $user_ID= "************************************"; //Set your lyricsfly.com User ID key $artist= $currentartist; //Set the artist you want to find lyrics for $title= $currenttitle; //Set the title for the song you want to find lyrics to $song_number= 1; //Set to 1 if you want the first result or 2 if you want 2nd result and so on... //******** setings *************************************************************** $artist= urlencode(filterChar($artist)); //filter and encode for url string $title= urlencode(filterChar($title)); //filter and encode for url string $url="http://lyricsfly.com/api/api.php?i=".$user_ID."&a=".$artist."&t=".$title; //url construction string with pluged in from above values $html=file_get_contents($url); //retrieve the entire xml page into string $found_check= GetTag($html,"sg",$song_number); //get song data if($found_check == "Query not found!") //check if song was found { echo "Sorry, lyrics for ". $currenttitle ." are not available in lyricsfly database.<br>"; //... if not print a message echo "Please <a href='http://lyricsfly.com/submit/'>submit</a> some lyrics for it."; //and print link to submit form } elseif($found_check=="Wrong site ID!") //check if site ID is good { echo "Sorry, access to lyricsfly API has been terminated please contact the administrator"; //check if user ID access key is valid. Incase we terminate your site ID for any reason your users will see this message. You may optionally put a contact email so that you get notified by someone. } elseif($found_check=="Missing site ID!" or $found_check==FALSE) //check if user ID access key is missing and if the number of song titles is not passed the amount which was returned { echo "Sorry, the API code was not properly implemented"; //this is mostly for you. You should not see this message once your code is functioning properly. } else //finally, if everything check on we retrieve and print the data returned by lyricsfly database { $found_checksum= GetTag($html,"cs",$song_number); //get song checksum $found_ID= GetTag($html,"id",$song_number); //get song ID $found_title= GetTag($html,"tt",$song_number); //get song title $found_artist= GetTag($html,"ar",$song_number); //get song artist $found_album= GetTag($html,"al",$song_number); //get song album $found_lyrics= GetTag($html,"tx",$song_number); //get song lyrics $found_lyrics=str_replace("[br]","<br>",$found_lyrics); //replace all "[br]" with "<br>" for html page $found_lyrics=str_replace("lyricsfly.com","<a href='http://lyricsfly.com'>lyricsfly.com</a>",$found_lyrics); //add html for lyricsfly.com link back //print album name echo "$found_lyrics"; //print lyrics echo "<a href='http://lyricsfly.com/search/correction.php?".$found_checksum."&id=".$found_ID."'>Fix above lyrics here</a><br><br>"; //create a link to correction form for users to be able to correct lyrics }Im fairly sure the high CPU Usage is due to the XML Parsing, this may be wrong though =/
If there is ANYTHING you can say to help me or give me advice, I would be more than pleased!
Tim
Link to comment
Share on other sites
4 answers to this question
Recommended Posts