SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    vancouver
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tracking Resource Use

    Hi all,

    I was just wondering if there is any easy way of tracking a script's memory use, preferably to a level where I can track the memory use of specific objects/arrays for performance tuning.

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Memory usage is tricky as you would need some form of direct hardware access and/or access to a software application with direct access. In short I don't think you can get this level of benchmarking from PHP. You can measure execution time and in some cases track CPU cycles.......

  3. #3
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can allocate shared memory via [url=http://www.php.net/manual/en/ref.shmop.php]]shmop functions which is not quite what you want though by allocating memory then checking the amount actually used you may possibly be able to get some useful data

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why can't people just be happy when a script actually works ? LoL

    Optimisation ? Some of us don't actually have the time to do this and besides, if the script is properly though out and well designed, optimisation shouldn't really be an issue.

    Not in today's high powered Internet anyway. 8)

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try setting a really low memory_limit in php.ini and see when you get "memory exhausted" errors.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  6. #6
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Optimisation ? Some of us don't actually have the time to do this ...
    Obviously, the original poster does. Nobody's forcing you to optimizie anything.
    TuitionFree a free library for the self-taught
    Anode Says... Blogging For Your Pleasure

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    right. optimization is why my forum will hopefully spank everything. everything counts when you're designing for 1 million+ page views per day.

    optimization is something missing from most scripts i've seen. but maybe that's because i'm the optimizing nut.

  8. #8
    SitePoint Member
    Join Date
    Feb 2003
    Location
    Groningen, Netherlands
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at GNU time. It doesn't work good on Linux, but it is OK on FreeBSD.

    Some samples:

    PHP Code:
    <?
    // an empty file
    ?>

    Command being timed: "/usr/local/bin/php test.php"
    User time (seconds): 0.00
    System time (seconds): 0.00
    Percent of CPU this job got: 93%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.01
    Average shared text size (kbytes): 9760
    Average unshared data size (kbytes): 2608
    Average stack size (kbytes): 1024
    Average total size (kbytes): 12368
    Maximum resident set size (kbytes): 14960
    Average resident set size (kbytes): 1584
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 313
    Voluntary context switches: 0
    Involuntary context switches: 0
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
    PHP Code:
    <?
    // reading a big file (1000000 bytes)
    $fp fopen('test.ppm''r');
    $str fread($fp1000000);
    fclose($fp);
    ?>

    Command being timed: "/usr/local/bin/php test.php"
    User time (seconds): 0.00
    System time (seconds): 0.01
    Percent of CPU this job got: 94%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.01
    Average shared text size (kbytes): 9808
    Average unshared data size (kbytes): 7888
    Average stack size (kbytes): 1536
    Average total size (kbytes): 17696
    Maximum resident set size (kbytes): 17584
    Average resident set size (kbytes): 6352
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 562
    Voluntary context switches: 0
    Involuntary context switches: 1
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
    PHP Code:
    <?
    // using a regular expression
    $fp fopen('test.ppm''r');
    $str fread($fp1000000);
    fclose($fp);

    $str preg_replace('/(a+)/i''b'$str);
    ?>

    Command being timed: "/usr/local/bin/php test.php"
    User time (seconds): 0.03
    System time (seconds): 0.00
    Percent of CPU this job got: 100%
    Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.03
    Average shared text size (kbytes): 6506
    Average unshared data size (kbytes): 12863
    Average stack size (kbytes): 682
    Average total size (kbytes): 19369
    Maximum resident set size (kbytes): 26256
    Average resident set size (kbytes): 12181
    Major (requiring I/O) page faults: 0
    Minor (reclaiming a frame) page faults: 1054
    Voluntary context switches: 0
    Involuntary context switches: 1
    Swaps: 0
    File system inputs: 0
    File system outputs: 0
    Socket messages sent: 0
    Socket messages received: 0
    Signals delivered: 0
    Page size (bytes): 4096
    Exit status: 0
    These are tests with the CLI-version of PHP. So most of the resources are taken to start up the binary. For that reason i also tested it with an empty php-file. Ofcourse the results will vary each time you test, but they can give a good indication.

    I used this command:
    /usr/bin/time -v /usr/local/bin/php test.php
    Last edited by sjokki; Mar 23, 2003 at 00:01.

  9. #9
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't php accelerator provide information about memory usage?

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryF
    Doesn't php accelerator provide information about memory usage?
    maybe about the memory PHPA's using, but not about scripts themselves that i'm aware of.

  11. #11
    SitePoint Enthusiast BDKR's Avatar
    Join Date
    Sep 2002
    Location
    Clearwater, Florida
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Unknown_Relic
    Hi all,

    I was just wondering if there is any easy way of tracking a script's memory use, preferably to a level where I can track the memory use of specific objects/arrays for performance tuning.
    Check out APD.

    http://apd.communityconnect.com/

    There is also one called onstatement I believe. Well, just check it out here...

    http://members.ozemail.com.au/~djf01/body.html

    I've not had too much time to mess about with either of these yet but they're both on my list.

    As for optimizing, I think it's the kind of thing you'd better think about, but not when you initially create the code. It's something that should be part of the review/refactoring process.

    On the other hand, there are a lot of 'faster' options that may be exploited while initially creating the script. Pre-incrementing and concatenation over string evaluation are just two examples. Let's not also forget to develop with error reporting turned all the way up. Those warnings that you don't see still cause delays in script execution. I learned this hard way!

    You may also want to check out some of the stuff that John Lim has written. It's php.weblogs.com. He's done a lot of tweaking with the nuances of php and has written some good stuff about it.

    Cheers,
    BDKR
    If you're not on the gas, you're off the gas!

  12. #12
    SitePoint Enthusiast BDKR's Avatar
    Join Date
    Sep 2002
    Location
    Clearwater, Florida
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another note on doing things to help speed up script execution. Try to be aware of the size of your classes or function libraries. In an email exchange with "someone out there on the net", he told me that it took php 50 ms to parse one of the larger db abstraction libraries out there and that that accounted for about 1/3 of the total request.

    This may not seem like much of a big deal, but when you got a site that's really bangin' away or in the throes of a /.'ing, you may wish that you had given this more thought sooner. Classes and function libraries shouldn't contain more than what is needed to get the job done or has a high likelyhood of being used.

    Cheers,
    BDKR
    If you're not on the gas, you're off the gas!

  13. #13
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you compile PHP with the --enable-memory-limit configuration option, php will calculate the peak allocated memory and store that in an apache note called mod_php_memory_usage.

    You can access this by creating a special log. Add something like this to your httpd.conf:

    CustomLog "/path/to/your/custom/log/file" "%h %l %u %t \"%r\" %>s %b %{mod_php_memory_usage}n"

    You might want to read mod_log_config

    You can see if your version of PHP was combiled with --enable-memory-limit using <?php phpinfo(); ?> and looking at the configure command line at the top.

    If you are using mod_php, I recommend using apache bench (ab) for your benchmarking. It will correctly include file parsing time in your benchmark, which microtime() based benchmarks will not.

    If you use ab, make sure you have a quiet network and that you have the server all to yourself. You can run ab on the same machine that you are running httpd on and still get accurate results.

    you can use the -c option of ab to simulate multiple concurant users. This is the situation where lower memory usage will influence actual performance.

    (You can indirectly see the impact of memory usage using the -c option even if you don't have a version of PHP compiled with --enable-memory-limit)

    If you use the -c option, then you cannot run ab and httpd on the same machine and get accurate results. You need two machines connected by a quiet (preferably local) network.

    Again, nothing else should be running on either machine when you do your benchmarks. (preferably using a test server, not a live server.)

  14. #14
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Location
    vancouver
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Optimisation ? Some of us don't actually have the time to do this and besides, if the script is properly though out and well designed, optimisation shouldn't really be an issue.
    For the most part I don't worry too much about formal performance testing and optimization of scripts, but in this case I'm in the process of building some core (for my needs) library functions and classes. Because they will likely be used on dozens of sites, time spent ensuring that they are efficient at this point will be well spent.


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
  •