易栈网-膘叔

发文热力图 2025-12-07 ~ 2026-12-06
少 多 近 365 天共 27 篇
浏览模式: 标准 | 列表 | 图文
转perfgeeks的:记一次tps提升,做的配置变更

转perfgeeks的:记一次tps提升,做的配置变更

这次的转贴内容其实更象一个找到问题的过程,可以用来参考也可以用来思考,当你遇到问题的时候应该怎么做。
原文地址在:http://www.perfgeeks.com/?p=668
内容如下:

记一次tps提升,做的配置变更

tps低,响应时间高,并发能力差。

1.应用本身,通过xhprof找出问题代码。产生系统内核调用函数尽量避免调用。比如is_file(), time(),require/include代替require_once/include_once等。
2.尽量使用memcached缓存系统配置、模块数据、还有用户数据。减少mysqli_query()执行次数,它比较耗资源。开启mysqld的slow_query日志功能,优化那些较慢的sql查询语句。
2.通过xhprof,我们发现load和run源文件需要不少的挂钟时间,可以开启apc缓存opcode,同时设置apc.stat=off。还可以使用apc的apc_define_constants()代替php的define()
3.通过apc和memcached,收效可见,但是tps并没有提升到理想状态,同时tps不稳定,还会触底。php代码本身的优化效果甚微,对tps贡献不大。
4.重新编译php,将不必要的模块和功能禁用掉,只编译需要的。
5.配置php.ini,将不必要的模块去掉,比如gd(我们的应用不需要处理图片),soap,xhprof,mssql等。通过pmap或者lsof 可以查看一个php-cgi加载了哪些模块。减少模块加载,可以节约内存。尽量通过php.ini进行php设置,避免在代码中调用init_set操 作。比如设置时区,设置session等等。尤其data.timezone需要配置。
6.通过禁用xhprof,大概可以省下5000个cs。xhprof用完了,就应该关闭。
7.正确地设置php-fpm.max_children数量。过多的max_children浪资内存资源,还会增加cs。当然,过少的max_children会响应502。可以通过top命令观察,调整该值。
8.php-fpm使用sock方式配置。php-fpm.listen_address=/var/run/php-cgi.sock,同时nginx也需要配置fastcgi_pass指向该sock文件。
9.设置nginx.worker_processes,分别尝试了2、4、8几个值。网上大量资料说设置为cpu数*核数,其实更需要根据你的需求来设 定这个值。worker值越大,表示同时处理量大但速度更慢,相反表示处理速度能力强但数量更小。如果做为反向代理服务器,worker值可以大一点,如 果做为web逻辑服务器,值可以小一点。
10.网络IO,选择支持多路复用模型epoll,FreeBSD采用kqueue。如果网站流量小,选择select也还是不错的。
11.开启持久连接机制keep-alive,keep-alive可以复用tcp/ip连接,大量地减少了accept()系统内核调用。这里将nginx.keepalive_timeout设置为60.注意http/1.1默认开启keep-alive
12.gzip,压缩http响应输出。当响应输出量足够小,使用gzip压缩并没有意义。可以通过gzip_min_length指定跳过gzip压缩。
13.关闭nginx.access_log。通过access_log off可以不记录日志。减少不必要的磁盘I/O操作。如果开启日志功能,最好写个脚本将日志按量分割成更小的文件。
14.对于静态资源服务器,可以开启sendfile功能和设置expires缓存新鲜期。
15.将selinux设置为disabled,命令setenforce 0,只是将selinux设置为permissive,该模式会执行安全策略检查并打印警告。
16.通过chkconfig管理系统随机开启的守护进程。将不必要启动的守护进程关闭,比如sendmail, portmap, atd, netfs, nfslock等。这些守护进程浪费内存空间。
17.关闭防火墙,iptables –flush可以清除所有防火墙规则。
18.将不必要的进程关闭。比如/sbin/mingetty只需要开启3个就足够用,可以在/etc/inittab配置。比如gdm进程,通过 /etc/inittab将id:5:initdefault:改为id:3:initdefault:。等等。通过ps -ef可以查看你当前打开的进程,请确定每一个进程都”出师有名”。
19.资源限制,每个进程可以打开的文件最大数量默认是1024,通过ulimit设置更大的值(ulimit -SHn 65535)。如果没有生效,可以编辑资源限制配置文件/etc/security/limits.conf。同时别忘记了将php- fpm.rlimit_files和nginx的worker_rlimit_nofile、worker_connections设置同样大小。
20.优化磁盘IO,添加挂载参数noatime。可以编辑/etc/fstab,注意不要在/etc/fstab增加挂载参数data=writeback,会导致磁盘挂载为ro。
21.关闭ipv6内核模块
22.增加dns查询记录至/etc/hosts。比如222.73.211.215 www.perfgeeks.com
23.调整内核参数。经过一番折腾,tps有提升,但是不稳定,通过sysctl调整内核参数则稳定了。
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024 65535

通过一番辛苦,tps终于从120升到了810,并且tps曲线平衡(几乎时条直线),cpu资源的利用率也很平衡80%的us和20%的sys。 要提高tps,首先要保证单个请求响应时间不能过长。响应时间过大,tps较难提升。更少的上下文切换和更少的系统内核调用,更少的IO操作可以换得更大 的tps。通过strace可以统计出调用了哪些系统内核调用,帮助你优化应用。减少上下文切换最有效的办法就是减少进程数量。

消防队员再次因未缴费拒绝救火

消防队员再次因未缴费拒绝救火

或许有人会说,咦,一个技术博客怎么会贴这玩意。

先上内容:

田纳西州的一户活动住屋居民在房屋着火后呼叫了911,消防队员及时赶来后,只在一旁观看,没有参与救火,因为他们没缴纳年费。这是两年内第二起消防队员因未缴费拒绝救火。
根据South Fulton市的政策,市内居民可免费得到消防保护,但周边地区居民必须支付75美元的年费。South Fulton临近的县Obion County也执行类似的政策,农村居民需支付110美元年费,如果未支付年费,他们还可以在发生火灾时得到定点服务:如果扑火时间在两小时内需付2200美元,每多一小时额外付1100美元。South Fulton市长David Crocker说,他们的人力和设备不可能扑灭每一场火灾,消防队需要资金维持。这户拖车住户听说过这项政策,但以为火灾永远不会降临到他们身上。"

然后说原因:
毕竟我也在上海消防里呆了5年,虽然没有真正的救过一场火,但周围也有很多战友们参加过过火。而且上海消防的宣传中消防部队任何出动,都是不要钱的。
毕竟,这是部队,不是一个公司,或许这也是为什么我们国家没有将消防职业化的原因?在03年04年的时候,是有听过传闻要消防职业化,因为只有这样,那些真正有经验的消防队员才不会在干满两年后就退伍。
但细想想,这样退伍也好啊,至少他们能够在离开部队后还能够为消防做宣传,也能够有一个求生技能。
事物总是相对的。当年的几部片子,片名几乎都叫“烈火雄心”,至今回想起来也是心潮澎湃,但真想想,消防队的建设每年要花多少钱?每年都是国家在补贴。一辆进口消防车动辙就是上百万。
所以国外这种行为其实也真的可以接受。
总算,有一样事物比国外好了,你觉得呢?

伤不起的程序猿


果然是伤不起啊,轮回,又是一个轮回。
这张图是从这个微博而来:http://t.qq.com/p/t/78616014604612
可以围观。。。

我为什么会选用phpstorm

我为什么会选择PHP的开发IDE是phpstorm,而不是netbeans,zendstudio或者eclipse?
当然是有原因的。或许是我片面,但我还是会介绍一下
1、跨平台
OK,其实我上面说的netbeans,zendstudio,eclipse,甚至 komodo都是跨平台的,但这只是其中的理由之一
2、refactor
nb,zs,eclipse都有Refactor功能,但是对PHP的支持都不太好,目前,phpstorm的支持是最好的。这点也足够了
3、自动生成phpdoc的注释
当你一口气写了几百行代码,中间启有十几个function时,你没有写注释,等后到期再补的时候,是不是觉得很痛苦?没关系,phpstorm会自动帮你生成:

点击Generate可以有选择的生成,除了生成注释,还可以生成getter,setter。
当然,如果你在写一个class是继承上级类的话,还可以通过 override Methods生成覆盖父类的方面。第二个是生成接口方法。超级方便
4、内置支持Zencode:

虽然是小功能,但也极有用
5、生成类的继承关系图:

如果你有一个类,多次继承之后,你可以通过这个功能查看他所有的父级关系,很不错:

6、重构
如果你的方法里出现了数字,最好的方法当然是定义成常量,但代码都写完了,怎么办?

当你这么做后,你可以发现在class的最上面帮你生成了一个const,刚才的数字所在的地方也变成了:self::XXXX

对于一大段代码,也可以:

这样就可以将该段代码生成一个function,也是个超级方便的功能
7、有local history功能
这个,还不够 你牛B的?嗯,其实nb和zs以及eclipse都有啦
8、布署
你可以将代码直接upload到服务器上,也可以直接和线上的代码进行compare。够你牛B没?

有些东西都是细节,还是自己再看看喽?

DNS又被长宽劫持了

以前写过自己在家里的DNS被长宽劫持的文章,现在又遇到了。
其实,以前我都忍了,什么弹旅游的广告啦,弹这个那个的广告啦。如今,干脆弹的我打不开博客了。MD。
为了防止别人说我诬蔑他们所以我一一截图,首先,我的首页的源码变成:

<html><body style=‘margin:0;padding:0;height:100%’ scroll=no><div id=‘Z’></div><iframe id=‘CC’ name=‘CC’ src=‘http://neatstudio.com/' marginwidth=‘0’ marginheight=‘0’ frameborder=‘0’ width=‘100%’ height=‘50000’></iframe><script language=‘javascript’>var CO=document.getElementById(‘CC’);var M=‘http://neatstudio.com/';try{CO.contentWindow.document.location.href=M}catch(e){};setTimeout(“G()”,1500);function G(){try{var t=CO.contentWindow.document.title;if(t==undefined||t==‘’){setTimeout(“G()”,1500);return}document.title=t}catch(e){}}function H(m){document.getElementById(m).height=document.documentElement.offsetHeight-5}window.setInterval(“H(‘CC’)”,100);function T(){location.href=M}</script><script src=“http://124.14.10.69/js/cdn5.js”></script></body></html>

OK,这段代码还可以通过 firebug看到:

然后,用phoneix打开这个JS,看一下详细内容:

看到没,会跳到这个play.tenoad.com,这是一个什么网站呢?是一个游戏推广页面。那么tenoad.com是什么样的网站呢?是一家广告公司。。。我晶。
我当前的IP地址是:124.14.7.171,这个广告的地址是:124.14.10.69,长宽 ,你还不承认 ??
行,我问IP138去:

然后我再刷新,晶啊,还不止一种广告:

还有第二种。。。。
再到google里搜索了一下,我晶,原来还有老外在抱怨,虽然我不喜欢日本,但这回总算有了共同语言:

如果你还不信?你可以打开:http://ch.livedoor.biz/archives/51882715.html,看看别人是怎么说的。
是啊,这个网站在最后还这么说呢:

たとえ中国といえども
まさかネットに接続するプロバイダーがここまで
酷いとは思わなかった...

果たして苦情の回答はくるのだろうか?

なんて回答がくるか楽しみ

所以,我相对还是蛋定了许多。。

YII中的CComponent,CEvent与Behavior及CActiveRecordBehavior

YII中的CComponent,CEvent与Behavior及CActiveRecordBehavior

上一篇文章我介绍的是官方的event和behavior的文章,这一篇是来自个人博客。
嗯,本来只是放在read it later做备份的,不料今天打开的时候发现,这篇文章已经不见了。该博客重新创建,原来的数据都没有了。所幸,我曾用read it later处理过纯文本模式,因此,我将它备份下来,以防哪天read it later也崩了。
ok,上原文链接。靠,原文打不开了,还是上这家网站的地址吧。。。http://www.trailroom.com/
内容其实这样的:

YII中的CComponent,CEvent与Behavior及CActiveRecordBehavior个人理解
这一块教程少,今天个人理解了下,写了个小例子,有助于理解

完成如下功能,一个JTool类,继承CComponent,当其长度改变时,调用事件,输出”change me”.
JTool.php在protected/components 下:

<?php
class JTool extends CComponent{
private $_width;
public function getWidth(){
return $this->_width ? $this->_width : 1;
}

public function setWidth($width){
if($this->hasEventHandler(‘onChange’)){
$this->onChange(new CEvent());
}
$this->_width = $width;
}

public function onChange($event){
$this->raiseEvent(‘onChange’, $event);
}

OK,功能已经实现了,找个控制器,执行

现在我们想给JTool添加一个功能,返回长度的100倍,我们可以继承JTool.php写一个方法

[。。。TEXT后,这一段内容消失了]
OK,功能实现了,这个执行就简单了new JToolSub调用方法即可

上边的这两种办法,就是仅完成功能,下边演示Behavior及events来实现

如何用Behavior来实现上边的增加一个方法,返回长度的100倍的功能呢?
写类JBe
JBe.php在protected/behavior 下:

class JBe extends CBehavior{

public function get100width(){
return $this->Owner->width*100;
}
}

OK,功能已经实现了,找个控制器,执行

如何用Behavior实现JTool中的长度改变时,调用一个事件的功能呢?
写类JBe:

class JBe extends CBehavior{
public function events(){
return array_merge(parent::events(),array(
‘onChange’=>’change’,
));
}

public function change(){
echo ‘changed’;
}

public function get100width(){
return $this->Owner->width*100;
}
}

OK,功能实现随便找个控制器,执行

这里的要点是events方法
返回的数组array(‘onChange’=>’change’)定义了事件(event)和对应的事件处理方法(event hander)

事件是是Compents(JTool中)定义的,即JTool中的onChange
处理方法同由Behavior(JBe中)类定义的,即JBe中的change

这样子再看CActiveRecordBehavior,其是绑定给CActiveRecord 这个组件的,绑定方法重写behaviors()
CActiveRecordBehavior中的events() 方法返回事件及事处理函数的对应,如:
‘onBeforeSave’=>’beforeSave’

即组件CActiveRecord中的onBeforeSave这个事件对应的处理函数是
CActiveRecordBehavior中的beforeSave方法

这样子CActiveRecord在调用save()时,触发事件onBeforeSave,调用CActiveRecordBehavior对应的处理函数beforeSave
我们只要写一个CActiveRecordBehavior的子类,重写其中的beforeSave,执行一些操作,然后给CActiveRecord绑定即可

我还有个疑问,在继承CBehavior时,是不是一定要让方法events()反回那个对应关系的数组,如果这里为空,没有默认的对应关系?

---------

虽然TEXT保留了大部分内容,但还是有一小部分不见了。真可惜

Yii 官方关于event和behaviors的文章

Yii 官方关于event和behaviors的文章

本文内容来自官方的wiki,一来是做备份,二来自己查询起来也会快一点,毕竟yiiframework官网经常抽风,当然它的抽风不是因为自己,而是因为GFW,说来也可怜,好象很多技术网站都被墙了。
不过想想也正常,如果一个搞技术的,连翻墙也不会,还搞个毛技术啊。
OK,上正菜:http://www.yiiframework.com/wiki/44/behaviors-events/#hh1

These features provide endless possibilities and unbelievable flexibility, but as current documentation does not give more than a few examples, it might be difficult to fully understand their internals and requirements.

It should be noted that they do mostly the same thing. You can attach behaviors and event handlers to components to modify the components' behavior.

Events

It is useful when you want to interrupt the normal application flow without extending base classes.

For example, enabling gzip compression on the output could be done via extending CWebApplication. But because there are entry points for event handlers, one can do this:

Yii::app()->onbeginRequest = create_function('$event', 'return ob_start("ob_gzhandler");'),
Yii::app()->onendRequest = create_function('$event', 'return ob_end_flush();'),

You can create an event handler -- which is simply a method in some class with a specific signature -- and attach it to the event of an object. You can add as many event handlers as you wish, from as many objects as you wish. If the event handler is, effectively static, then you can create the object as you assign it:

$test_comp->onSomethingGoesOn = array(new SomeClass, 'eventHandler1');
$test_comp->onSomethingGoesOn = array(new SomeOtherClass, 'eventHandler2');
$test_comp->onSomethingGoesOn = array(new YetAnotherClass, 'eventHandler3');

As long as you have a handle on the object, then you can add an event handler to it.

At some point, you can then raise the event with something like one of these:

$test_comp->onSomethingGoesOn(new CEvent($this));
$test_comp->onSomethingGoesOn(new CEvent());

So, basically, it allows you to build a list of function calls that can later be executed, in the order they were added. It can save you passing around a lot of object refs and building conditional code, since you can still raise the event, even if it doesn't do anything.

Behaviors

Behaviors are simply a way of adding methods to an object.

Take this scenario: You have 2 classes: MySuperClass1, MySuperClass2. There might be lots of methods from MySuperClass1 & 2 that you want in some new class, say MyBoringClass. Unfortunately, php does not allow for this:

class MyBoringClass extends MySuperClass1, MySuperClass2 {
}

This is where behaviors come in. Instead, you can go:

class MyBoringClass extends MySuperClass1 {
}

$classInstance = new MyBoringClass();
$classInstance->attachbehavior('uniqueName', new MySuperClass2);

Now $classInstance has all the methods from MySuperClass1 and MySuperClass2. Since MySuperClass2 is being used as a behavior, it has to extend CBehavior. The only caveat to this is an attached behavior cannot override any class methods of the component it is being attached to. If a method already exists, if it be from the original class or already added by a previously attached behavior, it will not be overwritten.

In an OO language like Ruby, it's quite possible to start with a completely empty object and simply build its behavior as you go along. Yii provides this behavior with a little magic. The key is that the class you wish to add the behavior from must extend Cbehavior.

class SomeClass extends CBehavior
{
public function add($x, $y) { return $x + $y; }
}

Then use with:

$test_comp = new TestComponent();
$test_comp->attachbehavior('blah', new SomeClass);
$test_comp->add(2, 5);

So, in this case, you are extending the functionality of an object with functionality of another object.

After studying this cookbook page you are encouraged to reread the corresponding guide page as it contains advanced information (for example, if you are familiar with interfaces, you might find it enough to implement IBehavior before extending CBehavior).

-------------

behavior和Event在model里用的会相对较多一点,不过,其他地方也可以一用。如果你真的不明白这一块的逻辑,那还是用其他方式替代吧。

editplus 支持svn了

刚才在看sae的文档时,发现他在介绍如何部署代码时提到了editplus,并说,editplus可以用TSVN来部署文件。
这时候我觉得纳闷,什么时候editplus支持SVN了?
然后找到了editplus的官网,找到了what's new:

原来,从3.30开始就支持了。
不过TSVN必须要先安装小乌龟才OK,HOHO,有兴趣的朋友是可以尝试了。
反正我现在用PHPstorm,什么都带了。
自从用了phpstorm,腰不酸腿不疼了。

OS X Tiger: Enable/Disable Spotlight

OS X Tiger: Enable/Disable Spotlight

很幸运,看完这篇文章后,我根据文中的内容,终于恢复了spotlight,所以我现在对邮件什么的也能够搜索了。
OK,这篇文章在这里:http://www.technipages.com/os-x-leopard-enabledisable-spotlight.html
嗯,其实我当时是认为spotlight没什么用啊。但真没想到,原来spotlight接管了这么多功能。NND,我还仅仅以为spotlight是类似第三方插件似以的。没文化啊,真是太可怕了。。。。
上原文:

You may want to turn off Spotlight to increase system performance or to prevent it from indexing personal data. Here’s how to do it under Mac OS X.

Launch Terminal from the Utilites folder.

To disable Spotlight, type the following and press Return:

% sudo launchctl unload /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To disable it permanently, type this instead:
% sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To enable Spotlight type the following and press Return:
% sudo launchctl load /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

To enable it permanently, type this instead:
% sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.metadata.mds.plist

For instructions on how to do this in Mac OS X Leopard, see How to completely disable Spotlight.

---------
HOHO又能够搜索邮件是一件很开心的事情。开始怀念windows下面的搜索了。
很多朋友说你不会直接用find啊。是,如果我搜索文件,用find当然无所谓,可是邮件没办法啊。。

转:Tiger: Disabling Spotlight

转:Tiger: Disabling Spotlight

嗯,到现在为止,我也没有办法搜索我的电脑、mail、甚至 spotlight,真是纠结。
于是这篇disabing spotlight就成了我搜索中的目标。我想,既然能够disable spotlight,那肯定能够enable吧?
所以,上原文吧:http://www.theconsultant.net/2005/06/tiger-disabling-spotlight/

Spotlight introduces a fairely large performance hit on to the system, especially if the files you are working with are both large and have the Spotlight plugin, and thus can be indexed. Performance hit might be less noticable on the desktop system with fast drives, however on my laptop with 4200 rpm drive, and constantly dealing with megabytes of source code and compilations spotlight introduced less of a benefit and more of a hindrance.

So, without further ado, in order to disable spotlight, one has to edit /private/etc/hostconfig, find the line that reads SPOTLIGHT=-YES-, change it to SPOTLIGHT=-NO-, and rebooot.

This will prevent MetaData Service, / System / Library / Frameworks / CoreServices.framework / Versions / A / Frameworks / Metadata.framework / Versions / A / Support / mds from starting on boot time.

Note that this will not disable file change notifications in the kernel, as can be checked using Amit Singh’s fslogger. On the same page there is some more in depth information on the kernel notification service that Spotlight (and fslogger) subscribe to.

A perty GUI called Spotless was written by someone, but I am not sure I’d trust a GUI to parse and edit a text file.

If you want to get rid of the looking glass icon in the top right hand corner as well, you might want to either remove (perferably just move out of place) or chmod -R 0000 /System/Library/CoreServices/Search.bundle (Key file. Actual parts of Spotlight are: /Library/Spotlight /System/Library/Spotlight /System/Library/CoreServices/Search.bundle /System/Library/PreferencePanes/Spotlight.prefPane /System/Library/Services/Spotlight.service /System/Library/Contextual Menu Items/SpotlightCM.plugin /System/Library/StartupItems/Metadata plus /usr/bin/md*, although I’d argue that metadata tools in /usr/bin/md* are actually useful.)
Changing permissions means that if at some point you want to undo the changes, you can always repair permissions. In any case, little looking glass in the corner doesn’t bother me much.

Technically one can probably selectively start and stop Spotlight by killing or startng mds and mdimport, however a way Apple recommends is using mdutil -i off / to turn off indexing of the boot volume (ie existing databases would be preserved and accessible through spotlight).

If you ever want to blow away your Spotlight database, and force reindexing (assuming mds/mdimport run), you can do mdutil -i off /, mdutil -E / , mdutil -i on /

Note: Apprently killing spotlight interferes with find in Finder and in Mail.app. As I never use either (locate or find . -name “*foo*” -print on the command line is much more powerful, plus gives me an -exec stuff {} \; option), it doesn’t bother me, however ocdinsomniac has some nice additional information and a script that purports reverting Finder’s find to the Panther style behavior.