我是一名數學老師,他爲我的學校構建了一個在線測試站點,可以使用它(又名,而不是專業版)。該網站運作良好,但隨着學校使用量的增加,我開始遇到內存問題(我認爲)。在我有大約50到60個用戶同時使用該網站後,整個網站開始崩潰,幾分鐘後它會恢復。我從來沒有使用過低的問題。學生參加測驗的頁面會在頁面上加載10個問題,每個選項包含4個廣播選項。 (並不是很多jquery正在進行)。每次用戶點擊答案時,我都會使用ajax將答案存儲在數據庫中。以下是在測驗中發送點擊的jQuery代碼。試圖減少腳本的內存使用
$('input:radio').click(function(){
var questionId = $(this).parent().parent().find('.qid').val();
var answer = $(this).val();
$.ajax({
type: "POST",
url: "insertqanswerajax.php",
data: {questionId: questionId, answer: answer},
});
});
當我加載我的cPanel我看到有運行,每個大約80兆5個不同的系統進程。我的php.ini中的最大值設置爲540MB。如果我使用memory_get_peak_usage()檢查頁面,它永遠不會超過大約半兆字節,但是在控制檯時間軸中,我可以看到一個用戶的內存使用量高達10兆(下圖)。我需要檢查什麼,或者排除差異的最佳方法是什麼?什麼可能導致問題?如果需要,我可以提供更多信息,但我不確定所有相關信息。
非常感謝您的幫助。
下面是PHP文件中的代碼通過AJAX訪問
<?php session_start();
include('../includes/startup.php');
$questionId = $_POST['questionId'];
$answer = $_POST['answer'];
insertQuizAnswer($questionId, $userId, $answer, 1);
?>
調用該文件的功能:
function insertQuizAnswer($questionId, $userId, $answer, $testId){
global $DB;
$standardsHandle = $DB->prepare("INSERT INTO quizanswers (questionid, userid,answer,testid)
VALUES (:questionId,:userId, :answer, :testId)
");
$standardsHandle->bindParam(':questionId', $questionId);
$standardsHandle->bindParam(':userId', $userId);
$standardsHandle->bindParam(':answer', $answer);
$standardsHandle->bindParam(':testId', $testId);
$standardsHandle->execute();
}
,並裝載在兩個啓動文件:
<?php
if(preg_match('/(?i)msie [2-7]/',$_SERVER['HTTP_USER_AGENT']))
{
// if IE < 8
echo "My Class Progress does not Work with this version of Internet Explorer</br>
<a href='https://www.google.com/intl/en/chrome/browser/'>Click Here to Download a more modern browser</a>";
exit;
}
else
{
}
if(isset($_POST['getGrade'])){
$_SESSION['gradeLevel'] = $_POST['getGrade'];
}
if(isset($_POST['getSubject'])){
$_SESSION['subject'] = $_POST['getSubject'];
}
include_once('../functions/userfunctions.php'); //all functions
include_once('../functions/goalfunctions.php'); //all functions
include_once('../functions/modulefunctions.php'); //all functions
include_once('../functions/globalfunctions.php'); //all functions
include_once('../functions/skillfunctions.php'); //all functions
include_once('../functions/quizfunctions.php'); //all functions
include_once('../functions/practicefunctions.php'); //all functions
include_once('../functions/benchmarkfunctions.php'); //all functions
include_once('../functions/dockfunctions.php'); //all functions
include_once('../functions/dashboardfunctions.php'); //all functions
include_once('../functions/notificationfunctions.php'); //all functions
include_once('../includes/connect.php'); //connect to database
$userSubject = $_SESSION['subject'];
$userGradeLevel = $_SESSION['gradeLevel'];
$userId = $_SESSION['userId'];
if ($_SESSION['loggedIn'] == 'true'){
}
else{
header('location: ../../index.php');
die();
}
?>
下面是被訪問connect.php文件:
try {
$DB = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
}
catch(PDOException $e) {
echo $e->getMessage();
}
它看起來像是混淆了服務器的內存和客戶端的內存,你實際上在「網站開始崩潰」的意思是什麼? –
一旦我點擊此網頁上的40到50個用戶進行測驗,網站就會關閉,如果您嘗試訪問任何頁面,瀏覽器只會引發一般性錯誤,指出網頁未找到。然後在幾分鐘後恢復。錯誤日誌中沒有錯誤。 – RyanY
好吧,我的理解是否正確:你的服務器在你的大學網絡之外,當有50-60名學生試圖在這臺服務器上打開同一頁面時 - 你看到消息「404文件未找到」,看起來像是大學網絡路由器切斷了請求,服務器端的DDoS防禦系統 –