The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

counting number of hits per second at run time

Hi All,
I am relatively new to PHP.
I need to develope a script that will count number of the hits it gets per second.
I also need to show it in graphs.
ALL THESE HAS TO BE DONE AT RUNTIME.
Any idea how to do it?

Thanks Austin,
The proposed script may experience a heavy load up to 100 hits per second.
Please correct me if I am wrong but I think it will not be feasible to do database activity for each hit, as it will create much overhead.
So I think it has to be done in the script itself and after each second it can be stored in the database.
So please propose something so that I can do it in the script level itself.

Thanks Austin,
The proposed script may experience a heavy load up to 100 hits per second.
Please correct me if I am wrong but I think it will not be feasible to do database activity for each hit, as it will create much overhead.
So I think it has to be done in the script itself and after each second it can be stored in the database.
So please propose something so that I can do it in the script level itself.

hi noddy,
thanks for the reply.
i have some doubts..
if the script is getting around 200 hits/second or more will it be able to keep the count of the hits/second properly at run time? is php capable of doing such things?
according to the proposed solution, for each hit i should read the corresponding record and then update it.will it be feasible to do such operations 200 times per second (or more) ?
one more question..
should synchronization be implemented for this purpose or can i do it safely without going for synchronization?

When you try and update a single text file at 200 connections per second you have to open lock the file write to it then close the connection. in order to do this 200 times a second you are going to need a very very fast cpu with plenty of ram.

a mysql database is built on the basis that multiple if not hundreds of transactions and instances will be run simultaneously

hi noddy,
actually i'm relatively new to php and i came to know that php is more than just dynamic web page designing. so i was trying to do this stuff using php.
this task is not for any site..actually the script will be hit by any load generator and i have to keep track of the hits so that it can judge whether the load generator is generating the proper load or not..
is php capable of doing such things??
can you propose some solution for this??
thanks for your co-operation..
waiting for your next response.

ya something like that.
the script will be hit directly by the load generators @ 100/200 hits/second.
and it has to count the correct number of hits it gets.
i have developed something. what i am doing is i have maintained 2 files namely time.count and hits.count.
each time a hit comes, it compares the current timestamp with the time.count value and if they are same, increments the hits.count value by 1.
when the current time increases than the time.count value i.e. next second, it writes the value in hits.count into database and resets the values of hits.count to 1.also it resets the value of time.count as the current time.
this works fine if i'm giving hits from browsers..around 20 hits/second (constatntly refreshing). but if a load generator is generating say 100/200 threads per second and hits it, it fails badly.
i need some help regrading this.
waiting for your response.

hi,
the latest developement is i'm now doin' database operation for each hit and it is working fine upto 150 hits/second.
is there any better way to accomplish the task.how can i improve the performance??

Hi,
As I mentioned in the previous posts, I'm trying to generate a tool that will calculate the no of hits/second at runtime.
I require it to judge the accuracy of the load generators.
Thus it is obvious that the load may be anything, it may be 100 hits/second or 200 hits/second or more.
Now I’m successful in showing 150 hits/second and trying to improve it further. That is the objective.
Thanks.

More importantly, what kind of MySQL query are you using? You mentioned something like selecting the value and then updating it, which is not recommended. You should directly update the value by using UPDATE table SET hits=hits+1 WHERE bla=bla .

Hi,
Reading the apache access log file is a great idea.
Initially I thought of doing that, but later on dropped the idea because the requirement was to show the graph at run time.
Still I will be trying to implement the same some time in near future.

Currently what I am doing is, just inserting one record per each hit (the timestamp).
Then another PHP script reads the mysql database, gets the no. of entries for each second and plots the graph accordingly. The same is done at 1-second delay (at 10:56:05 the graph is plotted for 10:56:04)

At the same time the cron job runs a script to delete the old records from the table in one minute.

I have some queries..
1. Does the insert statement take more time to be executed than the update statement? (I used insert statement because I thought it would be faster than update)
2. For each hit, I'm making the database connection, inserting the record and closing the connection. Is there any better method to the task?

Hi,
Reading the apache access log file is a great idea.
Initially I thought of doing that, but later on dropped the idea because the requirement was to show the graph at run time.
Still I will be trying to implement the same some time in near future.

Currently what I am doing is, just inserting one record per each hit (the timestamp).
Then another PHP script reads the mysql database, gets the no. of entries for each second and plots the graph accordingly. The same is done at 1-second delay (at 10:56:05 the graph is plotted for 10:56:04)

At the same time the cron job runs a script to delete the old records from the table in one minute.

I have some queries..
1. Does the insert statement take more time to be executed than the update statement? (I used insert statement because I thought it would be faster than update)
2. For each hit, I'm making the database connection, inserting the record and closing the connection. Is there any better method to the task?

Hi,
Obviously doing that thru some SQL or files will auto limit the max number of hits / second.
It could even be that the "counting/reporting" costs more as the script to study itself.
Everything should stay in memory, no disks.

PeterW idea of Apache module could certainly be the most efficient: Apache is in a central position there and do know the page it serves OK but how develop a module for apache ? BTW it's a good chance that allready exists.

I would try thru a socket where script under observation put a timestamp/ID and from which the analyzing script get the data.
Sure you can observe 10/100 times quicker.

OK, it's a great job to develop, but I could be interested too.
Should I go ahead, try to see how it can be done ?
à+

You're right to do that, but pehaps wait an hour or two before
OK for the poster script.
Could you also show the other side of the pipe, I mean this plotter, or at least how it reads and cumulate the data.
I like that, go on, we make it 100 times quicker.
à+