About PHP mall system performance optimization

    Recent research in the mall system source code, thinking about how to better optimization, in general, performance optimization can start with the general direction began to consider the impact on the performance of the larger factors to consider, for example, now use PHP5.7, performance can be doubled, these are objective factors, similar to the existing open-source online store, similar DSmall mall open source systems such basic PHP can support a higher version, the final consideration should be the PHP syntax details.

  1. PHP deployment environment
    single server commonly used apache + php and nginx + php-fpm deployed, we have been using apache + php way, is said by nginx + php-fpm deployment performance is good php performance than apache +, can be considered a try. The other is like nginx + swoole, etc., are also options.
    Based on this cluster is used nginx / lvs / lbs like the cloud as a load balancing reverse proxy front end. PHP cluster deployment on the basis of reliability, PHP processing performance of the cluster has N times higher than a single server (but as the overall performance of the services do not necessarily have N-fold lift). It simply can be considered, by extension cluster server, PHP can have on improving service performance.
    2.PHP expand the use of
    PHP extensions in addition to ease of use, or to enhance the performance of an intimate partner. There are three main applications:
    1) open opcode cache, to avoid duplication of compilation. You can use APC, eAccelerator, XCache and other PHP extensions, we use xcache. Just install this kind of thing can be.
    2) Method (PHP standard library or method) using an extended supplied, to achieve higher efficiency than PHP extension code. But in fact the way we meet limited expansion projects, many basic method step package if necessary, unless there is the ability to develop their own extensions. PHP extension frame may be considered to achieve such phalcon, yaf.
    3) The local cache, also commonly extended to support such xcache. Local cache may be extended use, some configuration data cache, main data or metadata, do not always read from a database or a file.
    Also, the PHP version, we can now consider the rise PHP7 ......
  2. File to load and operate
    it is very important optimization suggestions to minimize the reading and writing files, file operations include: file read, to determine whether a file exists, determine file size, particularly for disk, file operations to reduce a reduction of seek time, read take time. A file operation to reduce the ratio of N to optimize CPU instructions (request / request_once, echo / print , double or single primer primer), much better memory performance.
    Practical application, the following places:
    1) write the contents of the .htaccess apache configuration. We are usually placed in the root directory of the file PHP project through as .htaccess as URL rewriting configurations. This has resulted in each HTTP request, you have to read .htaccess, a multi-file operations. .Htaccess file contents usually do not need to be modified, it can be considered in the apache configuration and disable .htaccess file.
    2). PHP procedures, reduce the use of file operations such file_exists function. In the routing framework, to determine whether the referenced file exists, if there is an error, there is the executable file in the class.
    Why not request $ invoke [ 'path'] it? In fact, the path (file) we visited, normal words are there, it is not necessary to use file_exists determine whether there is. But there is no access to the file how it? Global treatment with set_error_handler way. But I just want to make reference to the current file special handling error, the error handling to stay in their framework, do not use the global user error handling it?
    Before require re-set an error handling method A, and returns an error processing method previously set; when require file does not exist, it performs A, the normally require re-setting the user's back error_handler.
    There is a process log, you may have to determine what each operation log log exists (does not exist is created) and access log size (separate log file), in general, the possibility of the existence of the log file, so a direct you can get the file size, the log file size by acquiring a method filesize, at the same time to determine whether a file exists, you can create a file does not exist. This reduces a file using the method of operation. How to use the filesize can get the size, but also to determine whether a file exists, without leaving the current process, the normal execution go on it? We can think about, because the filesize a non-existent file will report wrong oh.
    In short, as long as there is the possibility of a file or hit rate, you can consider not using file_exists.
    3) Load class, using the exact load and cache, do not file directory traversal. If a request, only the class file is loaded and loaded once only it needs to be the best.
    4) The file cache is turned into memory cache.
  3. Select the frame
    in addition to the above said PHP extension framework, there are many other non-PHP framework to achieve the expansion, like thinkphp, laravel, these frameworks are generic, packaged good, full-featured, but their performance will be a certain loss, the main reason I considered to be too many files loaded method defined variables, too many things to detect, process execution is too long. But since spent, we have to accept this, can do is to optimize the details on the syntax, the code logic. Far code execution (no external calls, database connections, etc.), the execution time frame is far greater than the application code execution time, if you spend smarty module engine performance will be greatly reduced. But in addition to performance considerations, there are many other factors to take advantage of these mature frameworks. If you think the performance aspect to be considered, you can select a point, the coupling of small lightweight frame, or select just the components you need.
    The use of functional components
         used in combination, there is a need to control and understanding of the entire frame.
    1) If the component can be selected, the first option is the routing component, and can be easily routed to the designated controller method, but do not do the extra things, less regular match, convention over configuration.
    2) Class loading function. The above said, demand precise load and cache, there is a request and require only once. We kind of item it is always original, require_once and new.
    3) and the security processing parameter calibrating. In fact this is relatively complex process performance, but for safety or necessary. Preferably the processing parameters can be done on demand, but sometimes for convenience or safety filter will be processed, the parameter check processing in the controller can process the global entrance.
    4). Session, arranged to be preserved by simply Redis / memcache cache the like, or stored in a cookie. But according to the default mechanism, session initialization (read) and the end of the request (write back), making two network operation. According to our scenario analysis, session content is seldom changed, and in the same situation can be considered read-only instead of the write-back (and therefore can not update the session will have to modify a matter of time), it is necessary to achieve their own PHP session interfaces, and only when a session modify, alter session when it is written back to memory, so that a small network operation can save much time ah. Poorly some implementations, multiple connection request or a multiple operation of the memory, it is a desirable. Further, binding cookieSession, i.e., the encrypted session stored in a cookie, the processing of the session will be reduced much consuming.
    5) The view template engine. If the API service class interface, direct return data, if a web page, you would need to use a template engine. Template engine is consuming the main performance, of course, like the original ecological way, it is the most efficient, smarty like this, large and inefficient performance, and a lot of features we do not have access. We use a template engine, the core is the html and PHP separation, and then the process variables and syntax. Therefore, to enjoy the benefits of separate template engine, but also the pursuit of performance, you can refer to tmd_tpl. Only achieve separation, using PHP syntax grammar, because if you want to implement their own set of grammar, requires a lot of find and replace. Also in the template engine, the file may be further combined to a plurality of views, etc. to reduce the file operation optimization.
    6). Databases. Use PDO, ORM easy to use but there are certain time-consuming, as the array of the most efficient data object, usually with a short connection, or connection objects using a single connection pool embodiments (some extended support).

Guess you like

Origin blog.51cto.com/13938514/2402494