• PHP


    一、基准测试

    php网站优化最佳实践:优化前端(压缩js/css/images--->程序优化(编码最佳实践、opcode缓存、变量/数据缓存)--->数据库、服务器调优-->操作系统调优

     

     

    1、基准测试实用工具

    定义请求/响应生命周期

    典型的http请求包含正在尝试访问的主机信息、浏览器信息以及对web服务器有用的其他信息。

     

     

    1Apache Benchmark

    实例:

    D:Webdevinapacheapache2.2.22in>ab -n 10000 -c 50 http://localhost/phpinf

    .php

    This is ApacheBench, Version 2.3 <$Revision: 655654 $>

    Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

    Licensed to The Apache Software Foundation, http://www.apache.org/

     

    Benchmarking localhost (be patient)

    Completed 1000 requests

    Completed 2000 requests

    Completed 3000 requests

    Completed 4000 requests

    Completed 5000 requests

    Completed 6000 requests

    Completed 7000 requests

    Completed 8000 requests

    Completed 9000 requests

    Completed 10000 requests

    Finished 10000 requests

     

     

    Server Software:        Apache/2.2.8

    Server Hostname:        localhost

    Server Port:            80

     

    Document Path:          /phpinfo.php

    Document Length:        52477 bytes      

    Concurrency Level:      50

    Time taken for tests:   22.932 seconds

    Complete requests:      10000

    Failed requests:        2158

       (Connect: 0, Receive: 0, Length: 2158, Exceptions: 0)

    Write errors:           0

    Total transferred:      526424140 bytes    #传输的总数据大小(以字节为单位);

    HTML transferred:       524774140 bytes

    Requests per second:    436.07 [#/sec] (mean)   #web服务器在模拟流量下每秒可以支持的请求总数;

    Time per request:       114.660 [ms] (mean)   #完成一个请求所花费的最长时间(以毫秒为单位)

    Time per request:       2.293 [ms] (mean, across all concurrent requests) #完成一个请求所花费的最长时间(以毫秒为单位)

    Transfer rate:          22417.81 [Kbytes/sec] received

     

    Connection Times (ms)

                  min  mean[+/-sd] median   max

    Connect:        0    0   1.9      0      16

    Processing:    16  114  32.4    109     468

    Waiting:        0  109  26.6    109     281

    Total:         16  114  32.4    109     468

     

    Percentage of the requests served within a certain time (ms)

      50%    109

      66%    109

      75%    125

      80%    125

      90%    140

      95%    172

      98%    203

      99%    218

     100%    468 (longest request)

     

    安装

     

     

    ②运行Apache Benchmark

     

     

    ③弄清响应的含义

     

     

    关键字段:HTML transferred(整个模拟传输的内容正文的总大小)Request per second(每秒支持的请求总数)、Time per request(满足一个请求需要花费的总时间)

    目标:减少HTML transferred、提高Request per second并且降低Time per request

    连接指标细目分类:

     

     

    ab选项标记

     

     

     

     

     

     

     

     

     

    ⑤并发测试

     -c的值必须小于等于n的值

     

     

    ⑥时间测试

     

    模拟10个用户在20秒的时间内同时访问网站

     

     

     

     

     

     

     

    7 ab陷阱

    ab - n http://localhost/而不是ab -n http://localhost,加上/

    设置用户代理:

     

     

    ⑧、修改apache并发数

    apache处理并发是通过MPM模块完成的,MPM有三种处理模式(performworkerwinnt)首先查看MPM的处理模式

     

    然后查看httpd.conf,加载MPM模块

     

     

    修改httpd-mpm.conf

     

     

     

    2Siege

    ①安装Siege

     

     

     

     

     

    ②运行Siege

     

     

     

     

    测试指标

    几个关键的指标:

    Data transerred:响应数据的总大小(不包含头数据)

    Transaction rate:每秒要响应的事务总数

    Longest transaction:满足一个请求所需的最长时间

    Shortest transaction:满足一个请求所需的最短时间   9.85 trans/sec

    ③测试多个url

    /usr/local/etc/urls.txt

     

    测试

     

     

    测试结果:

     

     

    二、提高客户端下载和呈现性能

    工具:

    1.分析响应(FirebugYSlowPage Speed

    2.优化响应(YUI compressor,Closure CompilerSmush.it),利用这些工具压缩JavaScriptCSS以及网页所需的图像。

     

     

    三、PHP代码优化

    php相关性能说明的列表:

    http://talks.php.net/index.php/PHP

    (1) requirerequire_once

    代码测试:

     

    require_once_test.php内容:

     

    几个类文件只是定义了一个空类

     

     

    每秒处理 1077.93个请求,处理时间为9.277ms

     

    测试require:

    require_test.php内容:

     

    其他文件还是定义一个空类

    测试结果:

     

     

    可以看出,每秒的请求数从1077个到1612个请求,响应时间从9秒降到6秒。

     

    (2)提前计算循环长度

    for1.php

     

    for2.php:

     

     

    连续运行这两个文件,发现后者比前者快好多

     

     

      





  • 相关阅读:
    web通信浅析(上B/S交互)转载
    tomcat内部运行原理浅析转载
    oracle集合运算
    Oracle 游标使用全解
    oracle 一些基本概念
    1.搭建项目环境之TestDirector 8.0
    修改Win7远程桌面端口
    从第二份工作开始
    2.搭建项目环境之源代码管理SVN
    How to Get IIS Web Sites Information Programmatically
  • 原文地址:https://www.cnblogs.com/luoyunshu/p/4035854.html
Copyright © 2020-2023  润新知