views:

63

answers:

4

Is there a function in PHP to query the number of open files?

Sort of like memory_get_usage() but for open files.

I'm running the unit test suites for Zend Framework. The problem is that after it gets through the tests for Zend_Search_Lucene, subsequent tests start failing. But if I skip the Zend_Search_Lucene tests, all the test suites succeed.

I'd like to prove that Zend_Search_Lucene (or any other test suite) is opening too many files and not cleaning up after itself. I thought PHP might have a function to simply report how many files are open. But after 20 minutes of searching the PHP manual and Google, I can't find any such function.

+2  A: 

For open files : I'm using lsof

http://sial.org/howto/debug/unix/lsof/

racar
+1  A: 

What I'd do, is to unregister the default stream handler and then register my own in which I'd provide an implementation for stream_open that keeps an internal counter.

Ionuț G. Stan
+5  A: 

There is no such function in PHP.

But there are alternatives. If you are running linux/unix/OSX, then running lsof from command line can give you that information.

This could be combined with a custom Test listener for PHPUnit - I described the approach under a different question - http://stackoverflow.com/questions/3937046/how-to-wrap-phpunit-to-control-command-line-reporting/3939872#3939872

PHPUnit_Framework_TestListener interface has methods like startTest(), endTest(), startTestSuite(), endTestSuite(). You could execute a shell_exec call to lsof from those methods and print out the interesting numbers before and after every test/testsuite.

Anti Veeranna
Thanks for the suggestion, I'll keep it in mind for the future. This time, I just ran `system("lsof -p ".getmypid())` in the tearDown() of the test I thought was the culprit, and I found that it was.
Bill Karwin
A: 

These two might do what you are looking for:

Iit's a wild guess though because I never used APD (it's PECL), so it might be resources does not refer to the actual resource type. Unfortunately, I couldnt find any documentation showing output.

Gordon