Jump to content

Is there a way to approximately quantify traffic e.g. number of SQL queries or bandwidth via php or the PW API?


Violet
 Share

Recommended Posts

Overall, I'm trying to make the behavior of my PW sites dependent on traffic amount, where in high traffic situations I want slightly different behavior, which I have an implementation in mind for - my only problem is how to approximately quantify traffic rate from within my template code. Any ideas?

As background, I'm trying to do something vaguely similar (but only vaguely!) to Ryan Cramer's post of how he drove around a DDOS attack - you don't need to read the link if you're busy - I'm just trying to show where I'm coming from in case anyone wants the background.

I already have a solution for page requests for commonly requested non-existent pages like wp-admin (return server 404's instead of letting PW handle them).

So it's only the level of traffic to my legitimate pages that I'd like to get a feel for. I'd ideally I'd like to know things like rate of SQL queries or the rate of bandwidth - is there a way to get an idea of that from within a template? I already know about the PHP function sys_getloadavg() from Ryan Cramer's post linked above. That would be my default measure if there's nothing better I could use. The only downside is that sys_getloadavg() could be high for reasons other than either DDOS or legitimate traffic. For example, when my server is doing auto-backups on cron or doing other automated tasks, I'm assuming that it could contribute to a higher load average for reasons other than traffic. I don't plan to give up any of these essential tasks, so is there a way I can get an idea of traffic that is requesting normal/existing pages? In case it has a bearing, only 1 page template would be affected by this.

Thanks for any help anyone can provide. Even if you don't have a specific solution to this exact question, I'd love to hear any comments/input that you might have surrounding this overall topic.

Link to comment
Share on other sites

21 hours ago, Violet said:

Even if you don't have a specific solution to this exact question, I'd love to hear any comments/input that you might have surrounding this overall topic.

They way I've sort of done this is with New Relic, you install agents for php/apache/mysql in the server and does it's magic to monitor the relevant processes.

And I say "sort of done this" is because I didn't really plan for anything, just wanted to test it out and got easily overwhelmed by all the features it has and concepts I don't really understand. I did manage to get alerts on spikes in memory and load, but that's about it and I'd say thats like 0.01% of what newrelic does, but kinda solved what I needed at that moment. 

  • Like 2
Link to comment
Share on other sites

One approach might be to keep a log of a certain number of timestamps for the most recent hits on each page (dropping the oldest when adding a new one).

Calculating the average difference between the timestamps would give a rate at which the page is being hit.

It might also work to look at the age of the oldest timestamp, and if it's less than a certain age assume the page is receiving a lot of hits.

You might find some useful techniques in the Page Hit Counter module.

 

 

  • Like 1
Link to comment
Share on other sites

20 hours ago, elabx said:

They way I've sort of done this is with New Relic, you install agents for php/apache/mysql in the server and does it's magic to monitor the relevant processes.

Thank you for this idea! This sounds neat, definitely will be checking out New Relic. I hadn't heard of it before, so this is certainly helpful information.

16 hours ago, BillH said:

One approach might be to keep a log of a certain number of timestamps for the most recent hits on each page (dropping the oldest when adding a new one).

Thank you for your valuable suggestion of using timestamps via the Page Hit Counter module. At first I thought this might take more customizing/coding than I'd be comfortable with, but then I saw on the Page Hit Counter module page it says:

Quote

This module quickly provides simple information, e.g. for editors. Or, for example, to sort certain news by most page views. For example for "Trending Topics".

... so it seems a lot more flexible than I first thought! It's not just a "look at stats in the admin panel" thing, it's way more than that. I can query actual numbers of hits for a particular page, sort pages by number of hits, etc.

Link to comment
Share on other sites

2 hours ago, Violet said:

... so it seems a lot more flexible than I first thought! It's not just a "look at stats in the admin panel" thing, it's way more than that. I can query actual numbers of hits for a particular page, sort pages by number of hits, etc.

Note that the module only stores the current count and not all the timestamps of each visit. That means you can get "trending topics" in terms of most views from the very beginning but not trending topics in terms of most viewed in the last 2 days for example.

But you could certainly achieve something like this by hooking into the pageViewTracked event like I did with RockHitCounter: https://github.com/baumrock/RockHitCounter/blob/1bfe2531810d2e9e18177d9975fd4d868fda96a4/RockHitCounter.module.php#L34

  • Like 1
Link to comment
Share on other sites

6 hours ago, bernhard said:

Note that the module only stores the current count and not all the timestamps of each visit. That means you can get "trending topics" in terms of most views from the very beginning but not trending topics in terms of most viewed in the last 2 days for example.

But you could certainly achieve something like this by hooking into the pageViewTracked event

Oh thank you for this explanation. Much appreciated!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...