tideaways可视化性能跟踪

安装 mongodb扩展

pecl install mongodb

[mongodb]
extension=mongodb.so

安装 tideaways扩展

https://github.com/tideways/php-xhprof-extension

git clone

phpize

./configure

make

make install

[tideways]
extension=tideways_xhprof.so
;不需要自动加载,在程序中控制就行
tideways.auto_prepend_library=0
;频率设置为100,在程序调用时能改
tideways.sample_rate=100

重启php

/etc/init.d/php-fpm restart

安装 mongodb

vim /etc/yum.repos.d/mongodb-org-3.2.repo

[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc

yum -y install mongodb-org

/etc/init.d/mongod start

安装 xhgui

git clone https://github.com/laynefyc/xhgui-branch.git

cd xhgui-branch

composer install –prefer-dist

# 配置文件
config/config.default.php

‘extension’ => ‘tideways_xhprof’,

# 入口文件增加时区设置
date_default_timezone_set(“Asia/Shanghai”);

测试 MongoDB连接情况并优化索引

use xhprof

db.results.ensureIndex( { ‘meta.SERVER.REQUEST_TIME’ : -1 } )
db.results.ensureIndex( { ‘profile.main().wt’ : -1 } )
db.results.ensureIndex( { ‘profile.main().mu’ : -1 } )
db.results.ensureIndex( { ‘profile.main().cpu’ : -1 } )
db.results.ensureIndex( { ‘meta.url’ : 1 } )

配置nginx

# UI访问地址
server {
listen 80;
server_name x.fengfeng.com;
root /data/www/xhgui-branch/webroot;

location / {
index index.php;
if (!-e $request_filename) {
rewrite . /index.php last;
}
}

location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /data/logs/x.fengfeng.com.log access;
}

# 在 fastcgi_param 中添加 auto_prepend_file
location ~ [^/]\.php(/|$)
{
fastcgi_param PHP_VALUE “auto_prepend_file=/data/www/xhgui-branch/external/header.php”;
}

Charles抓包

Charles作用
Charles 通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问请求都通过它来完成,从而实现了网络封包的截取和分析

Charles主要的功能包括
截取 Http 和 Https 网络封包
支持重发网络请求,方便后端调试
支持修改网络请求参数
支持网络请求的截获并动态修改
支持模拟慢速网络
授权抓取电脑的请求包
Proxy -> Mac OS X Proxy -> 输入电脑密码

默认端口
127.0.0.1:8888

设置浏览器代理(可忽略,一般只抓手机包)
在 Chrome 中设置成使用系统的代理服务器设置即可,或者直接将代理服务器设置成 127.0.0.1:8888 也可达到相同效果

界面视图
Structure 视图将网络请求按访问的域名分类
Sequence 视图将网络请求按访问的时间排序

过滤
Sequence -> Filter
Proxy -> Include -> Add
右键 -> Focus & 勾选 Focused

手机抓包
Proxy -> Proxy Settings -> 勾选 HTTP proxying

手机设置
设置->无线局域网->当前连接WIFI->详情->配置代理->手动
填入电脑的IP和端口

Charles确认
iphone请求连接,Charles点击Allow后即完成设置

https
电脑https
Help -> SSL Proxying -> Install Charles Root Certificate
选中请求地址 -> 右键 -> Enable SSL Proxying
钥匙串 -> 系统 -> 将证书设置为始终信任

手机https
Help -> SSL Proxying -> Install Charles Root Certificate On Mobile Device or Remote Browser
手机打开安装证书地址 -> http://charlesproxy.com/getssl -> 安装
设置 -> 通用 -> 描述文件能查看证书 设置 -> 通用 -> 关于本机 -> 证书信任设置 -> 完全信任

模拟慢网络
Proxy -> Throttle Setting -> Enable Throttling

模拟并发
右键 -> Repeat Advanced

修改网络请求内容
右键 -> Breakpoints
右键 -> Repeat 重新发起请求 -> Edit Request -> 修改参数 -> Execute

参考
https://blog.csdn.net/lmmilove/article/details/50244537

MySQL5.7参数优化

innodb_buffer_pool_size 约物理内存的50%-70%

innodb_data_file_path 初始化大小至1G

innodb_log_file_size 1G以上

innodb_flush_log_at_trx_commit 0:最快数据最不完全 1:最慢最安全 2:折中

innodb_max_dirty_pages_pct 25%-50%为宜

innodb_io_capacity 普通机械键盘:1000左右 SSD:10000左右 PCIE-SSD:20000以上

key_buffer_size 32M以下

sync_binlog 0:最快数据最不完全,系统自己决定刷新binlog的频率 1:最慢最安全,每个event刷新一次 N:每N个事务刷新一次

long_query_time 建议设置小于0.5秒

open_files_limit & innodb_open_files 建议65535

max_connections 突发最大连接数的80%为宜,过大容易导致全部卡死

thread_handling = “pool-of-thread” 启动线程池

query_cache_size & query_cache_type 绝大多数情况下是鸡肋,最好关闭 QC锁是全局锁,每次更新QC的内存块锁代价高,很容易出现 waiting for query cache lock状态

ibdata1文件都存储了些什么内容
Data dictionary
Double write buffer
Insert buffer
Rollback segments
UNDO space
Foreign key constraint system tables

ibdata1文件暴增的原因
大量事务,产生大量的undo log
有旧事务长时间未提交,产生大量旧undo log
file i/o性能差,purge进度慢
32bit系统下有bug

ibdata1文件暴增解决
设置独立undo表空间
增加purge线程数,innodb_purge_threads
提高file i/o能力
事务及时提交,不要积压
默认打开autocommit = 1
检查开发框架,确认autocommit=0的地方,事务结束后都有提交和回滚

连接数过高 too many connections
常规的做法:想办法杀掉多余的连接,加大连接数
其实应该是这样:限制连接数,设定max_user_connections 设定额外端口extra-port(便于DBA在数据库最大连接数占满后仍然能连接数据库进行操作)
建议:定时检查,干掉慢查询,避免阻塞,自我保护

MySQL5.7 sys系统数据库

查看是否安装 (5.7+默认安装)
select * from sys.version;

便于人类阅读 结果集类似:background | 2143 | 266.48 ms
select * from host_summary_by_file_io;

便于工具处理 加上”x$” 结果集类似:background | 2148 | 266558291670
如果需要使用”order by”之类的操作的话不能显示 如:266.48 ms
select * from x$host_summary_by_file_io;

谁使用了最多的资源
select * from user_summary;

大部分连接来自哪里
select * from host_summary;

在哪个文件产生了最多的IO
select * from io_global_by_file_by_bytes

哪张表被访问的最多
select * from sys.schema_table_statistics;

哪些语句延迟较大,这些延迟较大的语句有哪些特征
select * from statement_analysis

哪些SQL语句使用了磁盘临时表
select * from statements_with_temp_tables

哪张表占用了最多的buffer pool空间
select * from innodb_buffer_stats_by_table

索引的好处
通过索引过滤,减少需要扫􏰁的记录数量
索引可以帮助服务器避免排序和临时表
索引可以将随机IO变为顺序IO
通过索引覆盖,加快查询

索引的坏处
占用磁盘空间
增加了记录的修改(插入、删除、修改)代价

快速查看每张表的索引情况
select * from schema_index_statistics;

索引的使用需要恰到好处,充分利用索引的优势,避免无用索引、冗余索引等

查看重复索引
select * from schema_redundant_indexes;

查看无用索引
SELECT * FROM schema_unused_indexes;

案例
a字段无索引 全表扫描
select a from t1 where a = 1;

给a字段添加索引
alter table t1 add index idx_a(a);

存在综合排序 不能命中索引
SELECT a,b FROM t1 WHERE a > 666 ORDER BY a, b;

添加组合索引
alter table t1 add index idx_a_b(a, b);

a索引重复 删除a索引
ALTER TABLE t1 DROP INDEX idx_a;

面向对象与调试

 

函数与模块