gpt4 book ai didi

php - Flash Builder数据向导编写的巨大CPU负载/性能不佳的php zend系统

转载 作者:行者123 更新时间:2023-11-30 23:24:18 25 4
gpt4 key购买 nike

我是一名自学成才的 AS3/Flex/AIR 开发人员,我构建了一个在 AIR 上运行的触摸屏信息亭系统,并通过在线 Flex 应用程序进行远程内容管理。

这是我第一次做服务器端的事情,所以如果需要请随时纠正我的问题。

AIR 应用程序和 Flex 应用程序都连接到 mysql 数据库,以便使用 php 脚本和 Zend 框架进行简单的 CRUD 操作。信息亭每 30 秒调用一次服务器进行更新。

所有这些简单的服务器端 php 脚本都是由 Flash Builder 的数据向导自动生成的。我做了简单的调整(gateway.php、amf_config.ini、mysqli 连接参数)并将所有内容部署到客户端的服务器。这是一种共享服务器类型。

现在,系统可以正常工作了。但它工作缓慢。每个创建/读取/更新/删除操作都有效,但我认为它应该快得多。此外,这些操作会给服务器的 cpu 带来很大的负载。根据托管人员的说法,该系统打开的 php-cgi 进程占用了服务器 40% 的 CPU 功率,这样做会减慢自身速度并降低该服务器上托管的其他站点的速度。

我的问题是:

首先,是否存在问题或者这种性能是从 Zend 框架中扩展出来的?Flash Builder 写的自动生成的脚本是不是写的不好,可以优化?这种系统可以留在共享托管服务器上吗?我们应该迁移到 VPS 服务器以获得更好的性能吗?如果我使用 phpmyadmin 在 mysql 数据库上创建的表不是优化构建的,是否会对性能产生这种影响?

我不知道 php 或 mysql。我们将不胜感激任何形式的帮助。

萨尔

这是 Flash Builder 编写的脚本:我没有添加任何东西 - 只是更改了连接参数。

<?php

class KiosksService {

var $username = "--------";
var $password = "--------";
var $server = "--------";
var $port = "3306";
var $databasename = "--------";
var $tablename = "kiosks";

var $connection;

/**
* The constructor initializes the connection to database. Everytime a request is
* received by Zend AMF, an instance of the service class is created and then the
* requested method is invoked.
*/
public function __construct() {
$this->connection = mysqli_connect(
$this->server,
$this->username,
$this->password,
$this->databasename,
$this->port
);

$this->throwExceptionOnError($this->connection);
}

/**
* Returns all the rows from the table.
*
* Add authroization or any logical checks for secure access to your data
*
* @return array
*/
public function getAllKiosks() {

$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename");
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

$rows = array();

mysqli_stmt_bind_result($stmt, $row->KioskID, $row->Station, $row->Branch, $row->Chain, $row->Pingdate, $row->Layout, $row->Online, $row->Clips, $row->Extra);

while (mysqli_stmt_fetch($stmt)) {
$row->Pingdate = new DateTime($row->Pingdate);
$rows[] = $row;
$row = new stdClass();
mysqli_stmt_bind_result($stmt, $row->KioskID, $row->Station, $row->Branch, $row->Chain, $row->Pingdate, $row->Layout, $row->Online, $row->Clips, $row->Extra);
}

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);

return $rows;
}

/**
* Returns the item corresponding to the value specified for the primary key.
*
* Add authorization or any logical checks for secure access to your data
*
*
* @return stdClass
*/
public function getKiosksByID($itemID) {

$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename where KioskID=?");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'i', $itemID);
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

mysqli_stmt_bind_result($stmt, $row->KioskID, $row->Station, $row->Branch, $row->Chain, $row->Pingdate, $row->Layout, $row->Online, $row->Clips, $row->Extra);

if(mysqli_stmt_fetch($stmt)) {
$row->Pingdate = new DateTime($row->Pingdate);
return $row;
} else {
return null;
}
}

/**
* Returns the item corresponding to the value specified for the primary key.
*
* Add authorization or any logical checks for secure access to your data
*
*
* @return stdClass
*/
public function createKiosks($item) {

$stmt = mysqli_prepare($this->connection, "INSERT INTO $this->tablename (Station, Branch, Chain, Pingdate, Layout, Online, Clips, Extra) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'sssssisi', $item->Station, $item->Branch, $item->Chain, $item->Pingdate->toString('YYYY-MM-dd HH:mm:ss'), $item->Layout, $item->Online, $item->Clips, $item->Extra);
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

$autoid = mysqli_stmt_insert_id($stmt);

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);

return $autoid;
}

/**
* Updates the passed item in the table.
*
* Add authorization or any logical checks for secure access to your data
*
* @param stdClass $item
* @return void
*/
public function updateKiosks($item) {

$stmt = mysqli_prepare($this->connection, "UPDATE $this->tablename SET Station=?, Branch=?, Chain=?, Pingdate=?, Layout=?, Online=?, Clips=?, Extra=? WHERE KioskID=?");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'sssssisii', $item->Station, $item->Branch, $item->Chain, $item->Pingdate->toString('YYYY-MM-dd HH:mm:ss'), $item->Layout, $item->Online, $item->Clips, $item->Extra, $item->KioskID);
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}

/**
* Deletes the item corresponding to the passed primary key value from
* the table.
*
* Add authorization or any logical checks for secure access to your data
*
*
* @return void
*/
public function deleteKiosks($itemID) {

$stmt = mysqli_prepare($this->connection, "DELETE FROM $this->tablename WHERE KioskID = ?");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'i', $itemID);
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);
}


/**
* Returns the number of rows in the table.
*
* Add authorization or any logical checks for secure access to your data
*
*
*/
public function count() {
$stmt = mysqli_prepare($this->connection, "SELECT COUNT(*) AS COUNT FROM $this->tablename");
$this->throwExceptionOnError();

mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

mysqli_stmt_bind_result($stmt, $rec_count);
$this->throwExceptionOnError();

mysqli_stmt_fetch($stmt);
$this->throwExceptionOnError();

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);

return $rec_count;
}


/**
* Returns $numItems rows starting from the $startIndex row from the
* table.
*
* Add authorization or any logical checks for secure access to your data
*
*
*
* @return array
*/
public function getKiosks_paged($startIndex, $numItems) {

$stmt = mysqli_prepare($this->connection, "SELECT * FROM $this->tablename LIMIT ?, ?");
$this->throwExceptionOnError();

mysqli_stmt_bind_param($stmt, 'ii', $startIndex, $numItems);
mysqli_stmt_execute($stmt);
$this->throwExceptionOnError();

$rows = array();

mysqli_stmt_bind_result($stmt, $row->KioskID, $row->Station, $row->Branch, $row->Chain, $row->Pingdate, $row->Layout, $row->Online, $row->Clips, $row->Extra);

while (mysqli_stmt_fetch($stmt)) {
$row->Pingdate = new DateTime($row->Pingdate);
$rows[] = $row;
$row = new stdClass();
mysqli_stmt_bind_result($stmt, $row->KioskID, $row->Station, $row->Branch, $row->Chain, $row->Pingdate, $row->Layout, $row->Online, $row->Clips, $row->Extra);
}

mysqli_stmt_free_result($stmt);
mysqli_close($this->connection);

return $rows;
}


/**
* Utility function to throw an exception if an error occurs
* while running a mysql command.
*/
private function throwExceptionOnError($link = null) {
if($link == null) {
$link = $this->connection;
}
if(mysqli_error($link)) {
$msg = mysqli_errno($link) . ": " . mysqli_error($link);
throw new Exception('MySQL Error - '. $msg);
}
}
}

?>

最佳答案

虽然 Zend 框架的性能可能有点问题,但您的示例中没有 zend 框架代码

this system opens 6 php-cgi proccesses

对于每个操作?这似乎不太可能。如果您提供显示典型事务的访问日志样本以及他们提供的支持此断言的证据,将会很有帮助。

如果您对性能(或您的服务器机房有多热)有丝毫的担忧,那么通过 cgi 运行 php 是一个非常糟糕的主意。使用 mod_php 或 php-fpm 甚至通过 fcgi 的 php 以及操作码缓存将减少超过 75% 的执行时间。

查看代码,这里没有明显的问题 - 除了 getKiosks_paged - 在没有显式 ORDER BY 的情况下进行行范围选择非常草率。

Each create/read/update/delete operation works but I think it should be a lot faster

速度有多快? websrver 上的请求有多长,它执行什么查询?他们需要多长时间?你的数据库结构是什么?您的查询的解释计划是什么样的?

关于php - Flash Builder数据向导编写的巨大CPU负载/性能不佳的php zend系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14070860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com