SitePoint Sponsor

User Tag List

Results 1 to 23 of 23
  1. #1
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    efficiency and optimization

    I have made a CMS/ecommerce package. I have been working on it for a year and a half. I have had the occasional bug or two and was always able to fix them promptly. but now I am getting ridiculed about my scripts not being efficent. the server is a good one (or was told it to be) and everything I have done as far as coding was up to par, to what I have learned over the past 3 years form examing and reading others code.

    I have my package using gzip, I use a db connection class and each connection has been mysql_free_result(). the catelog part of the page loads items in a pagenation effect. the user controls the output as far as how per page they want. an optimizer has been installed on the server and frankly the site loads pretty darn fast even for a dialup connection.

    now, what consist of efficient php coding? what else could I do or am I really that bad of a coder?

    comments appreciated.
    success is not by chance, it is by choice.

  2. #2
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get the Zend Studio with it's profiler. You'll see what parts of your code take the most time to execute. Usually it's DB class/SQL queries that are often very slow.
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IlyaDevyatovsky
    Get the Zend Studio with it's profiler. You'll see what parts of your code take the most time to execute. Usually it's DB class/SQL queries that are often very slow.
    Also,
    What OS is on the server? What optimizer are you using? What version of php are you using? Are you using IIS or apache or something else? What version of mysql are you using?

    What type of connection does this server have to the internet? What are the server specs?

    I normally run php apps on Linux with Apache. I use phpAccelerator. It could be my imagination but after installing it on a P90 with 32 megs of ram on a 1.2 gig hdd running Linux, Apache, PHP,MySQL, my forum ran extremely fast. People actually sent emails commenting on how they saw a difference in speed in the forum. (I made the forum myself)

    So it could be code as well as whats running it.

    Silly

  4. #4
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IlyaDevyatovsky
    Get the Zend Studio with it's profiler. You'll see what parts of your code take the most time to execute. Usually it's DB class/SQL queries that are often very slow.
    that is not a bad idea. this question has been spawned by some recent threads I have posted. more info can be found Here

    Quote Originally Posted by Sillysoft
    Also,
    What OS is on the server? What optimizer are you using? What version of php are you using? Are you using IIS or apache or something else? What version of mysql are you using?

    What type of connection does this server have to the internet? What are the server specs?

    I normally run php apps on Linux with Apache. I use phpAccelerator. It could be my imagination but after installing it on a P90 with 32 megs of ram on a 1.2 gig hdd running Linux, Apache, PHP,MySQL, my forum ran extremely fast. People actually sent emails commenting on how they saw a difference in speed in the forum. (I made the forum myself)

    So it could be code as well as whats running it.
    the link I posted will answer most of that. the type of connection I can't answer cause I don't know. the server specs are pretty high, 2gig of ram, a 2 or 3 gig proccessor (I think) nothing to really hender the running of code. the box has been split for server load, mysql is on another box altogether and it seems fine. so it seems the sql queries are ok.

    this thread is not meant to be part of tha tone, I want to concentrate on the code in this one if that is what it is. I am not all convinced it is the code but I want to make sure I am coding effeciently and keep doing so.
    success is not by chance, it is by choice.

  5. #5
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Server uptime: 9 hours 51 minutes 39 seconds
    Total accesses: 1896995 - Total Traffic: 10.6 GB
    CPU Usage: u401.5 s116.11 cu65.62 cs0 - 1.64% CPU load
    53.4 requests/sec - 314.0 kB/second - 5.9 kB/request
    42 requests currently being processed, 16 idle workers
    Actually, it takes only 1.64% CPU load with 53 requests a second (if I understand the stats right). I think it's quite fast.

    In some cases implementing cache with a DB class would save some CPU time, but it really depends on how the class operates.
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  6. #6
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, that was when the box was running good. now in that other thread I mentioned it gets at 100% and the only process running is the httpd. so you would think if it was a script problem it would at least show me what script is running, right?

    the db class is something I picked up long ago. it is pretty basicand I have seen most scripts run almost the same code just slighly different.
    PHP Code:
    function query($Query_String) {
        
    $this->Query_ID mysql_query($Query_String,$this->Link_ID);
        if (!
    $this->Query_ID) {
          
    $this->halt("Invalid SQL: ".$Query_String);
        }
        return 
    $this->Query_ID;
      } 
    there is the query part. not much huh?
    success is not by chance, it is by choice.

  7. #7
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    yeah, that was when the box was running good. now in that other thread I mentioned it gets at 100% and the only process running is the httpd. so you would think if it was a script problem it would at least show me what script is running, right?
    httpd couldn't take 100% all the time as the average was 1.64%. Did I miss anything?

    function query($Query_String) there is the query part. not much huh?
    How do you parse the result of query? How does your script take the values out of the class, what is the method for this?

    As an example -- how would you script proceed if you need to print out 100 values from a database using your DB class?
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  8. #8
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IlyaDevyatovsky
    httpd couldn't take 100% all the time as the average was 1.64%. Did I miss anything?



    How do you parse the result of query? How does your script take the values out of the class, what is the method for this?

    As an example -- how would you script proceed if you need to print out 100 values from a database using your DB class?
    that log snipplet was when the sit was running good, I have not seen one when the cpu is maxed out.

    I use this to fetch all the rows, either in a while look or a for loop.
    PHP Code:
    function fetch($Query_ID=-1) {   
        if (isset(
    $this->Query_ID)) {
          
    $this->record mysql_fetch_array($this->Query_ID);
        } else {
          if (!empty(
    $Query_String)) {
            
    $this->halt("Invalid query id (".$this->Query_ID.") on this query: $Query_String");
          } else {
            
    $this->halt("Invalid query id ".$this->Query_ID." specified");
          }
        } 
    for instance it would go something like this
    PHP Code:
    $db_down = new DB;
    $db_down->connect();
    $sql2 $db_down->query("SELECT mp_prodid, mp_downloadUrl from $memprod where mp_prodid= '$id'");
        if (
    $db_down->num_rows($sql2)){
            
    $row $db_down->fetch($sql2);
            
    etc... 
    success is not by chance, it is by choice.

  9. #9
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is not in this code. Try the zend software to check what exactly takes the time. As I remember they have 15 days trial that will allow you to check you scripts.
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  10. #10
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will, thanks.
    success is not by chance, it is by choice.

  11. #11
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well I did the profiler. it had a lot on the functions file which is what holds the db class so of course there is oging to be a lot of calls to query and stuff. but other than that is looks fine. I will do some more testing.

    anything else that I might be missing??
    success is not by chance, it is by choice.

  12. #12
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    well I did the profiler. it had a lot on the functions file which is what holds the db class so of course there is oging to be a lot of calls to query and stuff. but other than that is looks fine. I will do some more testing.

    anything else that I might be missing??
    If the profiler didnt give much vision of any issues code wise, then you need to also look at whats running the code. What version of php is installed on the system at this time? What kind of optimizer are you running?

    In the other post it shows php3 is running on this. Is this still the case?

    Silly

  13. #13
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no it shouldn't be php3, we have 4.3.6 running. as far as optimizers we use Turck MMcache and I think he has php accelerator installed.

    it was actually a typo, we do not have php3 running.
    success is not by chance, it is by choice.

  14. #14
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sahajin
    no it shouldn't be php3, we have 4.3.6 running. as far as optimizers we use Turck MMcache and I think he has php accelerator installed.

    it was actually a typo, we do not have php3 running.
    So what I am reading in this thread is everything is running just fine code wise, but you still have this problem. As I mentioned before perhaps you should check whats running it instead of just the script.

    Silly

  15. #15
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and that is what I have been telling the admin guy. there are 3 sites on this box, 2 run this code I have and the other does not. not sure what code it uses.

    but the server was just fine 2 weeks ago then the cpu and memory got to 100%. so they added another gig to the memory and now it is just the cpu maxing out.

    that is what leads me to believe it can't be my script.

    thanks for your thoughts
    success is not by chance, it is by choice.

  16. #16
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is an excert from the profiler. total time was 503 millisec. seems pretty fast to me. does it look good to you guys, it is the catelog part of the site so it should be the one getting used a lot.
    Attached Images Attached Images
    Last edited by Sahajin; Jun 4, 2004 at 17:00.
    success is not by chance, it is by choice.

  17. #17
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well I really need to get a resolution to this.

    matter have become worse and it seems that the site is getting DDos'ed. it seems that once the attack starts it loads the page but never releases php.

    what is happening is that there are so many hits, from attack sources, coming in that the servers
    dont have enough resources, and for some reason when it connects to php, it does not release the connection. as far as php and my code goes is there a way to slow it down or not make it do this?
    success is not by chance, it is by choice.

  18. #18
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How many hits per second do you get now?

    I guess there is no way to prevent your server from a DOS attack php-side. Do you know the IPs from which the attack comes from? If the attacking server does not use proxies, writing an e-mail to the admin's of the IP zone should solve the problem. Talk to your lawyer (or directly with law enforcement agencies) about the problem.
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  19. #19
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well that is where it gets tricky. it is being done form over seas and the law knows it but can't do anything as it is legal over there. but tha tis a different matter all by itself.

    it is a proxy as the IP's are all different
    success is not by chance, it is by choice.

  20. #20
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get a list of proxies and disable them from accessing your site.
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  21. #21
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and how do you do that? we don't want to disable the average user
    success is not by chance, it is by choice.

  22. #22
    SitePoint Addict
    Join Date
    May 2004
    Location
    .
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will not disable the average user by disabling the IPs of anonymous proxies.

    Here is a good list of proxies you may want to disable:
    http://kiev-security.org.ua/box/a1/proxy1.pl?F=2

    As a way of blocking I'd try .htaccess's "deny from".

    http://httpd.apache.org/docs/mod/mod_access.html
    http://www.javascriptkit.com/howto/htaccess5.shtml
    Ilya Devyatovsky
    ThinkHost, Inc.
    Wind/solar powered web hosting - 6 months free!

  23. #23
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it was actually an attack that was brand new, no filters has been setup to block it. we have now setup some cache so at least the server stays up. all in all my code was ok. the attack was using up all the resources.

    we just need to filter it better.
    success is not by chance, it is by choice.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •