CPU使用率过高,由于阿贾克斯在线留言板 [英] High CPU usage due to Ajax Shoutbox

查看:160
本文介绍了CPU使用率过高,由于阿贾克斯在线留言板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以我做了一个在线留言板,我一直在使用了一段时间,但就在最近它已经到了我的注意,它吃的,因为很多调用脚本的CPU占用率,并且我想知道我怎么能去修复它的性能。

下面是上留言的JS脚本。

  VAR current_shouts = 0;
            功能$ shoutid(eleid){
                返回的document.getElementById(eleid);
            }
            功能urlen code(U){
                U = u.toString();
                VAR匹配= u.match(/ [\ x90- \ XFF] / G);
                如果(匹配){
                    对于(VAR中期= 0;中< matches.length;中++){
                        VAR char_ code =匹配[MID] .char $ C $猫(0);
                        U = u.replace(火柴[MID],'%U00'+(char_ code和;为0xFF)的ToString(16).toUpperCase());
                    }
                }
                返回逃生(U).replace(/ \ + /克,%2B);
            }
            功能长啸(){
                clearTimeout(getshout);
                VAR XMLHTTP =(window.XMLHtt prequest)?新XMLHtt prequest:新的ActiveXObject(Microsoft.XMLHTTP);
                xmlHttp.open(GET,在线留言板/ shouts.php我=?+的Math.random());
                xmlHttp.onreadystatechange =功能(){
                    如果(this.readyState == 4){
                        如果(parseInt函数(this.responseText)> current_shouts){
                            getshouts();
                            current_shouts = parseInt函数(this.responseText);
                        }
                        getshout = setTimeout的(喊声(),1000);
                    }
                }
                xmlHttp.send(空);
            }
            功能getshouts(){
                VAR XMLHTTP =(window.XMLHtt prequest)?新XMLHtt prequest:新的ActiveXObject(Microsoft.XMLHTTP);
                xmlHttp.open(GET,在线留言板/ getshouts.php我=?+的Math.random());
                xmlHttp.onreadystatechange =功能(){
                    如果(this.readyState == 4)$ shoutid(在线留言板)的innerHTML = this.responseText。
$ shoutid(在线留言板)scrollTop的= $ shoutid(在线留言板)scrollHeight属性。;
                }
                xmlHttp.send(空);
            }
            功能push_shout(){
                喊();
                返回false;
            }
            功能喊(){
                VAR XMLHTTP =(window.XMLHtt prequest)?新XMLHtt prequest:新的ActiveXObject(Microsoft.XMLHTTP);
                xmlHttp.open(POST,在线留言板/ shout.php);
                VAR数据=用户=+ urlen code($ shoutid(用户)值。)+&放大器; +喊=+ urlen code($ shoutid(呐喊)值。);
                xmlHttp.setRequestHeader(内容型,应用程序/ x-WWW的形式urlen codeD);
                xmlHttp.setRequestHeader(内容长度,data.length);
                xmlHttp.onreadystatechange =功能(){
                    如果(this.readyState == 4){
                        如果$ shoutid(呐喊)值=(this.responseText!)。
                        其他 {
                            警报(this.responseText);
                        }
                        getshouts();
                    }
                }
                xmlHttp.send(数据);
                返回true;
            }
            VAR getshout = setTimeout的(喊声(),1000);
 

我并不是最聪明的蜡笔,当谈到js的,所以我真的不知道如何去解决这个。因此,要求 shouts.php 是真的吃了我的CPU。

下面的脚本 shouts.php

 < PHP
$ DB =新的mysqli(本地主机,用户名,及格,数据库);

如果($ DB-> connect_errno大于0){
   死亡(无法连接到数据库['$ DB-> connect_error。']');
}
    $语句= $ DB->查询(SELECT COUNT(ID)FROM喊);
    而($喊= $ stmt-> FETCH_ASSOC()){
    回声破灭($喊);
    }
session_write_close();
?>
 

我读了会话锁定问题,所以我加了 session_write_close(); 但是这似乎并没有帮助我的问题在所有。

任何提示将不胜AP preciated!

解决方案

这是那些东西,其中调谐code不一定要去帮助一大堆之一。你基本上DDOS攻击自己的硬件。我在不久的将来的建议是撞了JavaScript的setTimeout的频率从 1000 2000 或更高。从长远来看:升级硬件,移动到一个更快/较轻的存储解决方案(Redis的是我个人最喜欢的),两者将是明智的。

Alright, so I have made a shoutbox, I have been using it for some time, but just recently it has come to my attention that it eats at the cpu usage because of so many calls to the script, and I am wondering how I can go about fixing its performance.

Here is the js script for the shoutbox.

var current_shouts = 0;
            function $shoutid(eleid) {
                return document.getElementById(eleid);
            }
            function urlencode(u) {
                u = u.toString();
                var matches = u.match(/[\x90-\xFF]/g);
                if (matches) {
                    for (var mid = 0; mid < matches.length; mid++) {
                        var char_code = matches[mid].charCodeAt(0);
                        u = u.replace(matches[mid], '%u00' + (char_code & 0xFF).toString(16).toUpperCase());
                    }
                }
                return escape(u).replace(/\+/g, "%2B");
            }
            function shouts() {
                clearTimeout(getshout);
                var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
                xmlHttp.open("GET", "shoutbox/shouts.php?i=" + Math.random());
                xmlHttp.onreadystatechange = function() {
                    if (this.readyState == 4) {
                        if (parseInt(this.responseText) > current_shouts) {
                            getshouts();
                            current_shouts = parseInt(this.responseText);
                        }
                        getshout = setTimeout("shouts()", 1000);
                    }
                }
                xmlHttp.send(null);
            }
            function getshouts() {
                var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
                xmlHttp.open("GET", "shoutbox/getshouts.php?i=" + Math.random());
                xmlHttp.onreadystatechange = function() {
                    if (this.readyState == 4) $shoutid("shoutbox").innerHTML = this.responseText;
$shoutid("shoutbox").scrollTop = $shoutid("shoutbox").scrollHeight;
                }
                xmlHttp.send(null);
            }
            function push_shout() {
                shout();
                return false;
            }
            function shout() {
                var xmlHttp = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
                xmlHttp.open("POST", "shoutbox/shout.php");
                var data = "user=" + urlencode($shoutid("user").value) + "&" + "shout=" + urlencode($shoutid("shout").value);
                xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                xmlHttp.setRequestHeader("Content-length", data.length);
                xmlHttp.onreadystatechange = function() {
                    if (this.readyState == 4) {
                        if (!this.responseText) $shoutid("shout").value = "";
                        else {
                            alert(this.responseText);
                        }
                        getshouts();
                    }
                }
                xmlHttp.send(data);
                return true;
            }
            var getshout = setTimeout("shouts()", 1000);

I'm not really the brightest crayon when it comes to js so I am not really sure how to go about fixing this. The call to shouts.php is what is really eating up at my cpu.

Here is the script for shouts.php

<?php
$db = new mysqli('localhost', 'username', 'pass', 'database');

if($db->connect_errno > 0){
   die('Unable to connect to database [' . $db->connect_error . ']');
}
    $stmt = $db->query("SELECT COUNT(id) FROM shout");
    while ($shout = $stmt->fetch_assoc()) {
    echo implode($shout);
    }
session_write_close();
?>

I read up on the session locking problem so I added in the session_write_close(); but this doesn't seem to help with my issue at all.

Any tips would be greatly appreciated!

解决方案

This is one of those things where tuning the code isn't necessarily going to help a whole bunch. You're essentially DDoS attacking your own hardware. My suggestion in the immediate future would be to bump up the javascript setTimeout frequency from 1000 to 2000 or higher. In the long run: upgrading hardware, moving to a faster/lighter storage solution (Redis is my personal favorite), both would be wise.

这篇关于CPU使用率过高,由于阿贾克斯在线留言板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆