<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>某人的栖息地</title>
	<atom:link href="http://www.ooso.net/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ooso.net</link>
	<description>Linux + Apache + Mysql + Php + Flash</description>
	<lastBuildDate>Thu, 11 Mar 2010 17:54:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LightCloud的设计原理</title>
		<link>http://www.ooso.net/archives/549</link>
		<comments>http://www.ooso.net/archives/549#comments</comments>
		<pubDate>Thu, 11 Mar 2010 17:37:16 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tokyocabinet]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=549</guid>
		<description><![CDATA[LightCloud是最近看到的一个比较轻巧的分布式key-value数据库，尽管这类软件已经让人觉得审美疲劳，但我仍然觉得它的设计思路值得一提。
特色
除开其项目主页上列出来的特点不提，我觉得还能数得上的特色有：


				<span class="readmore"><a href="http://www.ooso.net/archives/549" title="LightCloud的设计原理">阅读全文（1472字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>LightCloud是最近看到的一个比较轻巧的分布式key-value数据库，尽管这类软件已经让人觉得审美疲劳，但我仍然觉得它的设计思路值得一提。</p>
<h2>特色</h2>
<p>除开其项目主页上列出来的特点不提，我觉得还能数得上的特色有：</p>
<ul>
<ol>
<li>理论上可以用任意key-value数据库做为底层存储，现在支持以<a href="http://1978th.net/tokyotyrant/">tokyo tyrant</a>或者<a href="http://code.google.com/p/redis/">redis</a>作为底层的存储，如果使用redis可以获得更好的性能（大概提升30%~50%）</li>
<li>没有定制服务器端，基本上靠客户端语言来实现键值查找。优点是部署起来比较简单，缺点也是显而易见的，效率会有损失。</li>
<li>可以很方便的移植到其它语言上，我已经在github上找到一个ruby版本，甚至还有个php版本的实现。</li>
<li>可以方便的增加节点。</li>
<li>结构简单，方便hack</li>
</ol>
</ul>
<h2>LightCloud的设计原理</h2>
<h3>Hash ring</h3>
<p>LightCloud不能免俗的使用了一致性hash算法（Consistent Hashing），这是为了避免新增数据节点时发生集体拆迁事件。Consistent Hashing算法的原理请参考<a href="http://www.yeeach.com/2009/10/02/consistent-hashing%E7%AE%97%E6"%B3%95/>这里</a>。</p>
<p>last.fm的工作人员写的ketama算法算是比较常见的一致性算法，在libmemcached里大量使用。而LightCloud的作者当时还没发现合适的ketama python版，所以干脆自己捋起袖子写了个python版本的hash_ring，不到50行。这个是量身定制的，所以效率也还过得去，但是兼容ketama就别想了。</p>
<p>献上hash圈圈一个以明志：<br />
<img alt="" src="http://static1.139js.com/system/picture/10528302/600x600/memcached000404-thumb.jpg" title="一致性hash算法原理示意图" class="alignnone" width="600" height="472" /></p>
<h3>LightCloud的hash环有什么与众不同？</h3>
<p>其它分布式key-value数据库采用的办法是复制数据到多个节点上，例如<a href="http://s3.amazonaws.com/AllThingsDistributed/sosp/amazon-dynamo-sosp2007.pdf">Amazon Dynamo</a>的复制策略图：<br />
<a href="http://www.ooso.net/wp-content/uploads/2010/03/dynamo_replication.png"><img src="http://www.ooso.net/wp-content/uploads/2010/03/dynamo_replication.png" alt="" title="dynamo_replication" class="alignnone" /></a></p>
<p>Dynamo用了许多办法解决consistent问题，系统相当复杂。而LightCloud直接使用tokyo tyrant的master-master复制功能，大幅简化了这部分的逻辑。所以在它的hash环上，单个节点其实是一对master-master的tokyo tyrant，焦不离孟不离焦。</p>
<p><a href="http://www.ooso.net/wp-content/uploads/2010/03/lc_replc.png"><img src="http://www.ooso.net/wp-content/uploads/2010/03/lc_replc.png" alt="" title="lc_replc" class="alignnone" /></a></p>
<p>在新增数据节点时，如果没有路由服务找到正确的服务器，可能会损失数据。那么LightCloud继续采用流氓手段解决这个问题，他又给上了个环，保证不会发生意外。这两个hash环里的节点仍然是之前提到的tokyo tyrant双人组，一个环叫<strong>lookup</strong>，记录了每一个key保存在哪个storage节点上；另外一个环叫<strong>storage</strong>，这是真正存放数据的地方。于是它的结构图变成了下面这个样子：</p>
<p><a href="http://www.ooso.net/wp-content/uploads/2010/03/storage_lookup.png"><img src="http://www.ooso.net/wp-content/uploads/2010/03/storage_lookup.png" alt="" title="storage_lookup" class="alignnone" /></a></p>
<p>这部分比较难以理解，试着就上图阐述一下：</p>
<ul>
<ol>
<li>一个名叫A的家伙，住在storage_SB地区(storage ring)。同时，我们还告诉记性好的lookup_B(lookup ring)，A君的地址为storage_SB。</li>
<li>很不幸或很幸运，咱们的数据膨胀到需要扩容了，于是新增了一个违章建筑storge_X，这个该死的建筑正好影响了我们找到A君。这时候，我们还可以问起记性好的lookup_B，A君住在哪个角落里啊？ —— lookup_B日道：他就住在sotrage_SB一带~</li>
<li>lookup这群家伙记性虽然好，但是也架不住人多，再也记不住这么多人的住址，所以又新来了几个记性好的lookup。这个会影响咱们找到storage住哪吗？答案是不会，因为没有新增别的违章storage建筑，咱们不需要问路也能找着人。</li>
</ol>
</ul>
<p>按照以上推论，一定要避免出现同时增加lookup和storage节点，这很可能会损失数据。</p>
<h2>参考网页</h2>
<ul>
<li><a href="http://opensource.plurk.com/LightCloud/">http://opensource.plurk.com/LightCloud/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/549/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>使用nginx做为hiphop-php的前端服务器</title>
		<link>http://www.ooso.net/archives/545</link>
		<comments>http://www.ooso.net/archives/545#comments</comments>
		<pubDate>Sat, 27 Feb 2010 23:55:30 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[nginx]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=545</guid>
		<description><![CDATA[在邮件组里看到有人问能不能把多个hiphop-php编译后的程序跑在同一个端口上，想想也是合理的要求。如果一个服务器上跑了多个站点，那肯定都得用80端口，当大家共同租用服务器的时候，这个需求更为强烈。当时我所想到的解决办法是在前面搭个nginx之类的做代理，实际编译后的程序跑在别的端口，然后没过几天就看到了这份wiki - Using nginx as front server to HipHop。
简单的nginx配置示例
/etc/nginx/conf.d/ooso.conf:
PLAIN TEXT

				<span class="readmore"><a href="http://www.ooso.net/archives/545" title="使用nginx做为hiphop-php的前端服务器">阅读全文（839字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在邮件组里看到有人问能不能把多个hiphop-php编译后的程序跑在同一个端口上，想想也是合理的要求。如果一个服务器上跑了多个站点，那肯定都得用80端口，当大家共同租用服务器的时候，这个需求更为强烈。当时我所想到的解决办法是在前面搭个<a href="/?tag=nginx">nginx</a>之类的做代理，实际编译后的程序跑在别的端口，然后没过几天就看到了这份wiki - <a href="http://wiki.github.com/facebook/hiphop-php/using-nginx-as-front-server-to-hiphop">Using nginx as front server to HipHop</a>。</p>
<h2>简单的nginx配置示例</h2>
<p>/etc/nginx/conf.d/ooso.conf:</p>
<div class="igBar"><span id="lcode-3"><a href="#" onclick="javascript:showCodeTxt('code-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-3">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">server <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; listen *:<span style="color:#800000;color:#800000;">80</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; server_name *.<span style="">ooso</span>.<span style="">net</span> ooso.<span style="">net</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;location / <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;root&nbsp; &nbsp;__SERVER_ROOT__;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;index&nbsp; index.<span style="">html</span> index.<span style="">php</span> index.<span style="">htm</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;location ~ \.<span style="">php</span>$ <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header&nbsp; &nbsp;X-Real-IP&nbsp; &nbsp; &nbsp; &nbsp; $remote_addr;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_set_header Host www.<span style="">ooso</span>.<span style="">net</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; proxy_pass&nbsp; &nbsp;http:<span style="color:#FF9933; font-style:italic;">//localhost:4247;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>把hiphop-php编译后的代码跑在4247端口，然后通过nginx把所有对php的请求转发到这个端口，看上去就像我们平常配置的php fastcgi，不是吗？</p>
<h2>这样做有什么好处</h2>
<ul>
<li>支持负载均衡</li>
<li>支持ssl</li>
<li>支持gzip压缩</li>
<li>用nginx来挡住DoS攻击</li>
<li>因为我们的代码需要经过编译才能上线，代码多起来这个时间还真不短，不能像之前单纯的php那样爽快覆盖就完事。把经过编译的最新代码部署在别的端口上，用nginx快速切换，应该是一个比较实际的用法。</li>
</ul>
<h2>额外的技巧</h2>
<p>看wiki学到的额外技巧。以下配置段可以防止某些人把别的垃圾域名指向你的主机，结果被搜索引擎认为你用多个域名搞了一堆重复的内容建设，降低搜索权重。</p>
<div class="igBar"><span id="lcode-4"><a href="#" onclick="javascript:showCodeTxt('code-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-4">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">server <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; listen *:<span style="color:#800000;color:#800000;">80</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; server_name _;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; location / <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; deny all;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/545/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>快速安装hiphop-php的捷径</title>
		<link>http://www.ooso.net/archives/543</link>
		<comments>http://www.ooso.net/archives/543#comments</comments>
		<pubDate>Sun, 21 Feb 2010 10:55:15 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[rpm]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=543</guid>
		<description><![CDATA[不得不说，现在安装hiphop-php实在是太麻烦了，如果有rpm包一次搞定那该多好？就说周边那些零散的依赖库，也有不少安装比较繁琐的硬骨头。
Centos用户的好消息
Update: 在centos 64位机上完全通过rpm安装hiphop-php的步骤也已经提供了。
在邮件组上看到有人提供了centos下安装hiphop相关的rpm列表，把这些rpm装好，再按照wiki上专心编译hiphop即可。也许再过一阵，就会有人直接提供hiphop的rpm包，那就彻底方便了。

				<span class="readmore"><a href="http://www.ooso.net/archives/543" title="快速安装hiphop-php的捷径">阅读全文（818字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>不得不说，现在安装hiphop-php实在是太麻烦了，如果有rpm包一次搞定那该多好？就说周边那些零散的依赖库，也有不少安装比较繁琐的硬骨头。</p>
<h2>Centos用户的好消息</h2>
<p><strong>Update</strong>: <a href="http://wiki.github.com/facebook/hiphop-php/installing-or-building-hiphop-php-via-rpm-on-centos-5">在centos 64位机上完全通过rpm安装hiphop-php的步骤</a>也已经提供了。</p>
<p>在邮件组上看到有人提供了<a href="http://github.com/johnwyles/hiphop-rpm-centos/downloads">centos下安装hiphop相关的rpm列表</a>，把这些<a href="/?tag=rpm">rpm</a>装好，再按照wiki上专心编译hiphop即可。也许再过一阵，就会有人直接提供hiphop的rpm包，那就彻底方便了。</p>
<p>rpm列表</p>
<ol>
<ul>
<li>libicu42-4.2.1-1.x86_64.rpm</li>
<li>libicu-devel-4.2.1-1.x86_64.rpm </li>
<li>icu-debuginfo-4.2.1-1.x86_64.rpm </li>
<li>icu-4.2.1-1.x86_64.rpm 		</li>
<li>libcurl4-devel-7.20.0-1.x86_64.rpm </li>
<li>libcurl4-7.20.0-1.x86_64.rpm 	</li>
<li>curl-debuginfo-7.20.0-1.x86_64.rpm</li>
<li>curl-7.20.0-1.x86_64.rpm 	</li>
<li>libevent-1.4.13-1.x86_64.rpm 	</li>
<li>boost-devel-1_37_0-1.x86_64.rpm </li>
<li>boost-debuginfo-1_37_0-1.x86_64.rpm</li>
<li>boost-1_37_0-1.x86_64.rpm 	</li>
<li>cmake-2.6.4-7.el5.x86_64.rpm</li>
</ul>
</ol>
<h2>Ubuntu 9.10用户的安装指南</h2>
<p><a href="http://wiki.github.com/facebook/hiphop-php/building-and-installing-on-ubuntu-910">Building and Installing on Ubuntu 9.10</a></p>
<p>看起来还是ubuntu用户最清爽。</p>
<p><strong>注：如果使用了32bit的ubuntu，请使用<a href="http://github.com/metagoto/hiphop-php">patch过的hiphop-php</a>进行编译。</strong></p>
<h2>Fedora用户的编译步骤</h2>
<p><a href="http://www.ioncannon.net/programming/918/building-hiphop-php-for-fedora-12-on-64-bit-and-32-bit-systems/">Building HipHop PHP for Fedora 12 on 64 bit and 32 bit Systems</a></p>
<p><del datetime="2010-02-24T13:02:52+00:00">BTW: 目前hiphop-php仅支持64位操作系统。</del></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/543/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>初次体验hiphop-php</title>
		<link>http://www.ooso.net/archives/541</link>
		<comments>http://www.ooso.net/archives/541#comments</comments>
		<pubDate>Sun, 21 Feb 2010 02:19:22 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=541</guid>
		<description><![CDATA[昨天facebook在github上发布了hiphop-php的源代码。之前听说这玩意能把php代码翻译成c++代码，然后带来巨大的性能提升，所以第一时间编译了一份hiphop-php。
我的机器环境是

Centos 5.3 x86_64

				<span class="readmore"><a href="http://www.ooso.net/archives/541" title="初次体验hiphop-php">阅读全文（1059字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>昨天facebook在<a href="http://github.com/facebook/hiphop-php">github</a>上发布了hiphop-php的源代码。之前听说这玩意能把php代码翻译成c++代码，然后带来巨大的性能提升，所以第一时间编译了一份hiphop-php。</p>
<h2>我的机器环境是</h2>
<ul>
<li>Centos 5.3 x86_64</li>
<li>8G内存</li>
<li>Intel(R) Xeon(R) CPU E5420  @ 2.50GHz</li>
</ul>
<h2>安装注意事项</h2>
<p>编译的时候碰到的问题很多，但是基本上都是按照<a href="http://wiki.github.com/facebook/hiphop-php/building-and-installing">wiki上的步骤</a>进行的。我觉得比较重要的几点：</p>
<ul>
<li>wiki上的Required Packages包包列表都要检查一遍，比如版本号，是否安装过，像binutils-dev这种就很容易忽略</li>
<li>版本符合的话，直接用yum安装这些包就可以了</li>
<li>wiki上有类似Boost 1.37 is the minimum version字样，说明开发者可能就是在这个版本下开发的，我试了下最新版本的boost，编译到后来反而出错</li>
<li>如果yum上没有符合版本的lib库，可以手动编译，但是编译时建议就放在自己的home下，比如：<br />
<code>./configure --prefix=/home/user</code></li>
<li>tbb Intel’s Thread Building Blocks这个包有些麻烦，记得按照wiki上说的步骤安装</li>
</ul>
<h2>测试hiphop-php</h2>
<p>安装完成之后，时间也不是太多，所以我仅仅是简单的测试了一个<a href="/?tag=php">php</a>文件，代码如下：</p>
<div class="igBar"><span id="lphp-10"><a href="#" onclick="javascript:showCodeTxt('php-10'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-10">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$i</span> = <span style="color:#CC66CC;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">for</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$j</span> = <span style="color:#CC66CC;color:#800000;">0</span>; <span style="color:#0000FF;">$j</span> &lt;<span style="color:#CC66CC;color:#800000;">1000000</span>; <span style="color:#0000FF;">$j</span>++<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0000FF;">$i</span> += <span style="color:#0000FF;">$j</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000066;">echo</span> <span style="color:#0000FF;">$i</span>, <span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>用hphp进行编译:</p>
<div class="igBar"><span id="lcode-11"><a href="#" onclick="javascript:showCodeTxt('code-11'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-11">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hphp/hphp test.<span style="">php</span> --keep-tempdir=<span style="color:#800000;color:#800000;">1</span> --log=<span style="color:#800000;color:#800000;">3</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>提示生成新的可执行文件</p>
<div class="igBar"><span id="lcode-12"><a href="#" onclick="javascript:showCodeTxt('code-12'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-12">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">/tmp/hphp_c9sbnG/program </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>做一下运行时间对比：</p>
<div class="igBar"><span id="lcode-13"><a href="#" onclick="javascript:showCodeTxt('code-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-13">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time php test.<span style="">php</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.307s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.299s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.007s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time /tmp/hphp_c9sbnG/program</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.259s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.194s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.008s </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>没看出来编译成c++代码之后有太大的性能提升，估计是俺的使用手法问题？在邮件组里观察几天再说。</p>
<h2>Update</h2>
<p>facebook将优化之后的编译参数提交到了github，于是我重新编译并测试一遍这段相同的代码：</p>
<div class="igBar"><span id="lcode-14"><a href="#" onclick="javascript:showCodeTxt('code-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-14">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$ time /tmp/hphp_c9sbnG/program</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#800000;color:#800000;">499999500000</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">real&nbsp; &nbsp; 0m0.140s</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">user&nbsp; &nbsp; 0m0.076s</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sys&nbsp; &nbsp; &nbsp;0m0.006s </div>
</li>
</ol>
</div>
</div>
</div>
<p>
可以看到，经hiphop编译后的php，执行时间几乎快了一倍。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/541/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>git的代码review工具</title>
		<link>http://www.ooso.net/archives/539</link>
		<comments>http://www.ooso.net/archives/539#comments</comments>
		<pubDate>Thu, 11 Feb 2010 15:34:19 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[git]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=539</guid>
		<description><![CDATA[facebook在GitHub上托管了大量的开源项目，足足有26个。其中hiphop-php以及xhp在这阵子炒的比较热，的确是让人印象深刻的东西。顺手把别的项目翻出来看，也有很实用的工具，比如git-review。
git-review为git新增了一个很方便的代码review途径，利用这个命令，可以调用别的工具比如vimdiff来review代码的改动。下面简单记录一下使用的过程：
下载并安装
首先确认已经装好了git，剩下的事情比较简单。

				<span class="readmore"><a href="http://www.ooso.net/archives/539" title="git的代码review工具">阅读全文（750字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/facebook">facebook在GitHub上</a>托管了大量的开源项目，足足有26个。其中hiphop-php以及xhp在这阵子炒的比较热，的确是让人印象深刻的东西。顺手把别的项目翻出来看，也有很实用的工具，比如<a href="http://github.com/facebook/git-review">git-review</a>。</p>
<p>git-review为git新增了一个很方便的代码review途径，利用这个命令，可以调用别的工具比如vimdiff来review代码的改动。下面简单记录一下使用的过程：</p>
<h2>下载并安装</h2>
<p>首先确认已经装好了<a href="/?tag=git">git</a>，剩下的事情比较简单。</p>
<div class="igBar"><span id="lcode-18"><a href="#" onclick="javascript:showCodeTxt('code-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-18">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">git clone git:<span style="color:#FF9933; font-style:italic;">//github.com/facebook/git-review.git</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">cd git-review</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">python setup.<span style="">py</span> install </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这几步为git新增了一个review命令。</p>
<h2>git-review的使用</h2>
<h3>查看指定版本的改动</h3>
<div class="igBar"><span id="lcode-19"><a href="#" onclick="javascript:showCodeTxt('code-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-19">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">git review 58e2fb834793f5c6c1fdd900a1c0224a44735962 </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>出现提示</p>
<blockquote><p>
Now processing modified file foo.php<br />
foo.php [diff]&gt;
</p></blockquote>
<p>由于我配置了diff工具为vimdiff，所以接下来就可以用vimdiff查看foo.php在58e2fb834793f5c6c1fdd900a1c0224a44735962这个版本与最新版本之间有什么不同。</p>
<h3>查看两个版本之间的改动</h3>
<div class="igBar"><span id="lcode-20"><a href="#" onclick="javascript:showCodeTxt('code-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-20">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">git review 5b744bdc5f5bcbcfd6bb65f815aebe6bdce8c427 58e2fb834793f5c6c1fdd900a1c0224a44735962 </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在review每个代码之前，都可以使用help查看git review命令的帮助，如果放弃review，那么直接敲退出就可以了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/539/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>快速创建pear/pecl的rpm</title>
		<link>http://www.ooso.net/archives/535</link>
		<comments>http://www.ooso.net/archives/535#comments</comments>
		<pubDate>Mon, 04 Jan 2010 04:32:41 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[rpm]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=535</guid>
		<description><![CDATA[目前使用的服务器为centos，使用yum以及rpm来维护系统好处多多：


安装卸载，升级rpm软件包只需一条命令即可

				<span class="readmore"><a href="http://www.ooso.net/archives/535" title="快速创建pear/pecl的rpm">阅读全文（535字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>目前使用的服务器为centos，使用yum以及rpm来维护系统好处多多：</p>
<ol>
<ul>
<li>安装卸载，升级rpm软件包只需一条命令即可</li>
<li>统一部署，便于维护</li>
<li>比手工编译的方式要快</li>
</ul>
</ol>
<p>于是我需要把日常使用到的一些php扩展做成rpm包，但是手工写spec文件比较繁琐，因此<a href="/?tag=pear">pear</a>的这个小工具<a href="http://pear.php.net/package/PEAR_Command_Packaging">PEAR_Command_Packaging</a>帮了不少忙。它会给pear工具新增一个选项：<br />
<code>pear make-rpm-spec [options]
<package-file></code></p>
<p>这个命令行将会创建一个rpm的.spec文件，包含指定pear包的定义，而且也适用于<a href="http://pecl.php.net">pecl</a>的php扩展。</p>
<h2>创建pear rpm包的例子</h2>
<p><code>$ cd /path/to/rpm-build-tree/SPECS<br />
$ pear make-rpm-spec ../SOURCES/Net_Socket-1.0.tgz<br />
Wrote RPM spec file PEAR::Net_Socket-1.0.spec<br />
$ rpm -bb PEAR::Net_Socket-1.0.spec<br />
...<br />
Wrote: /path/to/rpm-build-tree/RPMS/noarch/PEAR::Net_Socket-1.0-1.noarch.rpm<br />
</code></p>
<h2>关于make-rpm-spec的帮助</h2>
<p><code>pear help make-rpm-spec</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/535/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>memcache连接慢又一例</title>
		<link>http://www.ooso.net/archives/524</link>
		<comments>http://www.ooso.net/archives/524#comments</comments>
		<pubDate>Tue, 22 Dec 2009 13:56:53 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=524</guid>
		<description><![CDATA[继上次解决memcache连接慢问题以来，好长一段时间没在这个问题上翻过跟头。这一次我又在生产环境观察到php和memcache的连接时间经常会在50ms以上。
作为一个cache，占用了这么长的执行时间，天理何在？
实际的运行环境如下：


				<span class="readmore"><a href="http://www.ooso.net/archives/524" title="memcache连接慢又一例">阅读全文（699字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>继<a href="http://www.ooso.net/archives/479">上次解决memcache连接慢问题</a>以来，好长一段时间没在这个问题上翻过跟头。这一次我又在生产环境观察到php和memcache的连接时间经常会在50ms以上。</p>
<p>作为一个cache，占用了这么长的执行时间，天理何在？</p>
<p>实际的运行环境如下：</p>
<ol>
<ul>
<li>apache + mod_php</li>
<li><a href="http://pecl.php.net/memcache">php-memcache扩展</a>版本为2.2.5</li>
<li>memcache的并发连接数在400左右，相当少</li>
</ul>
</ol>
<p>这次memcache扩展用的是最新的稳定版，无可挑剔。所以刚开始我认为是网络环境的问题，于是直接采用telnet工具直连<a href="/?tag=memcache">memcache</a>进行测试，发现速度飞快！一点便秘感都没有！所以把目光仍然放回到memcache扩展上来，集中对比较慢的addServer方法各项参数进行排查。</p>
<h2>Memcache::addServer方法</h2>
<p><code>bool Memcache::addServer  ( string $host  [, int $port = 11211  [, bool $persistent  [, int $weight  [, int $timeout  [, int $retry_interval  [, bool $status  [, callback $failure_callback  [, int $timeoutms  ]]]]]]]] )</code></p>
<p>比对结果表明，$weight参数对memcache的连接时间有显著的影响，$weight的默认值为1，一旦设置为别的数值，连接时间便会由毫秒级变成50ms左右，立竿见影。</p>
<p>鉴于php-memcache扩展一贯恶劣的表现，俺不得不痛下决心迁移到新的<a href="http://pecl.php.net/memcached">memcached扩展</a>上。memcached扩展基于libmemcached开发，而且提供了丰富的接口方法，应该是更好的选择。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/524/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>goo.gl shortener plugin for vimperator</title>
		<link>http://www.ooso.net/archives/504</link>
		<comments>http://www.ooso.net/archives/504#comments</comments>
		<pubDate>Mon, 21 Dec 2009 14:18:45 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[firefox]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[vimperator]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=504</guid>
		<description><![CDATA[基于goo.gl短url服务制作的vimperator插件，这个服务本来只有chrome上面可以玩玩，但是实际上有很简单的api可以调用，因此我改造成vimperator插件倒也没费太多的功夫。
goo.gl shortener plugin使用方法



				<span class="readmore"><a href="http://www.ooso.net/archives/504" title="goo.gl shortener plugin for vimperator">阅读全文（372字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>基于<a href="http://goo.gl">goo.gl</a>短url服务制作的<a href="/?tag=vimperator">vimperator</a>插件，这个服务本来只有chrome上面可以玩玩，但是实际上有很简单的api可以调用，因此我改造成vimperator插件倒也没费太多的功夫。</p>
<h2>goo.gl shortener plugin使用方法</h2>
<ol>
<ul>
<li>你必须是firefox用户，安装了<a href="http://www.vimperator.org/">vimperator</a>插件</li>
<li>下载<a href="http://customcode.googlecode.com/files/shorten.js">goo.gl shortener</a>插件，并放置在vimperator的plugin目录下，对于windows用户来说，通常应该在 x:\Documents and Settings\YourName\vimperator\plugin目录下。如果你是linux用户，那么你应该知道吧？</li>
<li>在vimperator输入框里输入  <code>:shorten</code>就能够获得当前url的短链接，并复制到剪贴板</li>
</ul>
</ol>
<p>话说那个客官，要觉得这工具好使，在转载的时候千万留下原地址，谢谢您了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/504/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql 5.5.0已经可以下载</title>
		<link>http://www.ooso.net/archives/433</link>
		<comments>http://www.ooso.net/archives/433#comments</comments>
		<pubDate>Tue, 15 Dec 2009 23:32:50 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=433</guid>
		<description><![CDATA[很高兴能够看到这个时候mysql还能出个新版本，mysql 5.5.0是所谓的milestone version，这次带来了一些有趣的东西，按照重要程度（我主观上的）列一下:


支持Semisynchronous Replication，这个是从早期google的semi-sync patch转过来的，能够做到master-slave的半同步复制

				<span class="readmore"><a href="http://www.ooso.net/archives/433" title="mysql 5.5.0已经可以下载">阅读全文（276字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>很高兴能够看到这个时候<a href="/?tag=mysql">mysql</a>还能出个新版本，mysql 5.5.0是所谓的milestone version，这次带来了一些有趣的东西，按照重要程度（我主观上的）列一下:</p>
<ol>
<ul>
<li>支持<a href="http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html">Semisynchronous Replication</a>，这个是从早期<a href="http://code.google.com/p/google-mysql-tools/wiki/SemiSyncReplicationDesign">google的semi-sync patch</a>转过来的，能够做到master-slave的半同步复制</li>
<li>集成Innodb plugin 1.0.6</li>
<li><a href="http://dev.mysql.com/doc/refman/5.5/en/partitioning-list.html">新的partition语法</a></li>
</ul>
</ol>
<p>这个版本还不适合用在正式环境。想想以后也许可以用mysql mmm + mysql 5.5提供可用性更好的服务，前提是它已经足够稳定，或者是percona版本？</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/433/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>php的echo为什么这么慢</title>
		<link>http://www.ooso.net/archives/517</link>
		<comments>http://www.ooso.net/archives/517#comments</comments>
		<pubDate>Tue, 15 Dec 2009 13:38:09 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=517</guid>
		<description><![CDATA[作为一个行走江湖多年的老中医，今天受命去解决一例前端页面展现缓慢的问题。问题页的情况如下：


apache + php

				<span class="readmore"><a href="http://www.ooso.net/archives/517" title="php的echo为什么这么慢">阅读全文（595字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>作为一个行走江湖多年的老中医，今天受命去解决一例前端页面展现缓慢的问题。问题页的情况如下：</p>
<ol>
<ul>
<li>apache + php</li>
<li>使用smarty模板输出内容</li>
<li>页面最终输出内容较大，80k+</li>
<li>页面执行时间在500ms以上</li>
</ul>
</ol>
<p>祭出法宝<a href="/archives/522">xhprof</a>对问题页面做了细致检查，发现页面的瓶颈竟然是模板（编译后的）中的一个echo语句，这个echo语句输出的字符串比较大，大概是50k+字节，花费时间为400多毫秒，占整个页面执行时间的80%。这样的echo输出在站点首页中其实是很常见的事情，没有数据库操作，按道理执行时间不应该这么长。</p>
<p>于是猛力使用搜索技能，最终在<a href="http://www.php.net/manual/en/function.echo.php#28843" href="_blank">php手册的echo部分</a>找到了一些蛛丝马迹，早在2003年就有前辈认为通过echo输出大字符串到客户端会引起服务器的性能问题，据我测试，在这个场景下使用print其实也是一样的慢。建议的解决办法是把字符串切割成更小的字符串输出，展现速度会有提升，输出函数如下：</p>
<div class="igBar"><span id="lcode-22"><a href="#" onclick="javascript:showCodeTxt('code-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-22">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">function echobig<span style="color:#006600; font-weight:bold;">&#40;</span>$string, $bufferSize = <span style="color:#800000;color:#800000;">8192</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $splitString = str_split<span style="color:#006600; font-weight:bold;">&#40;</span>$string, $bufferSize<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; foreach<span style="color:#006600; font-weight:bold;">&#40;</span>$splitString as $chunk<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; echo $chunk;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">?&gt; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>但是上面的处方不太对症，整个echobig的输出时间仍然在400毫秒左右，没有太大改善。</p>
<p>考虑到是输出大量内容到客户端比较慢，于是检查了apache的配置，原来还没打开deflate进行压缩，遂启用之。再次使用xhprof进行检查，这条echo的输出时间降低到5ms左右。</p>
<p>400ms到5ms，一个配置问题会产生80倍的差距，还真是省老钱了。这个故事告诉我们，<strong>压缩输出真的很重要。</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/517/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>用git从svn里clone最后几个版本</title>
		<link>http://www.ooso.net/archives/523</link>
		<comments>http://www.ooso.net/archives/523#comments</comments>
		<pubDate>Sun, 13 Dec 2009 02:44:03 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=523</guid>
		<description><![CDATA[一般情况下git svn clone这个操作会从第一个版本开始同步，如果版本号已经到了好几万（或更高？），这个操作会相当的费时。
当时还想着能不能hack一下git-svn脚本，其实后来看看文档，clone操作可以使用参数-r$REVNUMBER:HEAD检出指定版本后的代码，因此，更好的步骤应该是这样：



				<span class="readmore"><a href="http://www.ooso.net/archives/523" title="用git从svn里clone最后几个版本">阅读全文（302字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>一般情况下<a href="http://www.ooso.net/archives/520#comment-29022">git svn clone这个操作</a>会从第一个版本开始同步，如果版本号已经到了好几万（或更高？），这个操作会相当的费时。</p>
<p>当时还想着能不能hack一下git-svn脚本，其实后来看看文档，clone操作可以使用参数-r$REVNUMBER:HEAD检出指定版本后的代码，因此，更好的步骤应该是这样：</p>
<ol>
<ul>
<li>svn info http://your-svn, 并记录最后的版本号，假设是260</li>
<li>假设要检出最后10个版本，做个简单的减法： 260 - 10 = 250</li>
<li>开始clone操作了<br />
		<code>git svn clone -r250:HEAD --prefix=svn/ http://your-svn</code>
	</li>
</ul>
</ol>
<p>按这个办法，clone的时间的确是减少了许多。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/523/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql的partition与auto_increment</title>
		<link>http://www.ooso.net/archives/519</link>
		<comments>http://www.ooso.net/archives/519#comments</comments>
		<pubDate>Fri, 11 Dec 2009 14:18:34 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=519</guid>
		<description><![CDATA[mysql 5.1之后，开始新增了partition功能，可以做到对代码透明的情况下进行数据分区。但是从实际使用中来看，限制颇多：


如果用来分区的字段和主键不是同一个，则不能分区

				<span class="readmore"><a href="http://www.ooso.net/archives/519" title="mysql的partition与auto_increment">阅读全文（891字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="/?tag=mysql">mysql</a> 5.1之后，开始新增了partition功能，可以做到对代码透明的情况下进行数据分区。但是从实际使用中来看，限制颇多：</p>
<ol>
<ul>
<li>如果用来分区的字段和主键不是同一个，则不能分区</li>
<li>见很早前的一篇译文<a href="http://www.ooso.net/archives/217">mysql 5.1新功能 －－ 按日期分区</a>，如果查询条件中没有正确利用到分区的字段，那么partition的效果为零</li>
<li>mysql 5.1.31之前的partition功能有较大的bug，会导致自增字段不能正常自增，怎么个不正常法？auto_increment值会非正常增长，突然比当前的最大id小很多，插入数据时导致duplicate key错误。</li>
</ul>
</ol>
<p>这些问题都能凑合着对付过去，但是partition功能和auto_increment似乎是天生的敌人。从使用习惯上来说，我们会把auto_increment字段设置为主键，这个字段要是unique的才比较稳妥，这个情况下如果希望用其它字段来进行分区的话，几乎就成了不可能的任务。以这张表为例：</p>
<div class="igBar"><span id="lcode-25"><a href="#" onclick="javascript:showCodeTxt('code-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-25">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CREATE TABLE user <span style="color:#006600; font-weight:bold;">&#40;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; id int auto_increment PRIMARY KEY,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; uid int,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; foo varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">32</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> engine=myisam; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这个时候试图以uid进行分区会怎么样？是以怎样的惨淡结局收场？变通的办法是对自增字段做一些调整，从mysql的手册来看，自增字段不一定要设置成主键，仅仅是普通的index也可以，那么把表结构换成下面这个样子，就可以分区成功。</p>
<div class="igBar"><span id="lcode-26"><a href="#" onclick="javascript:showCodeTxt('code-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-26">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">CREATE TABLE user <span style="color:#006600; font-weight:bold;">&#40;</span>&nbsp; </div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; id int auto_increment,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; uid int,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; foo varchar<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">32</span><span style="color:#006600; font-weight:bold;">&#41;</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; index <span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#41;</span> engine=innodb PARTITION BY HASH <span style="color:#006600; font-weight:bold;">&#40;</span>uid<span style="color:#006600; font-weight:bold;">&#41;</span> PARTITIONS <span style="color:#800000;color:#800000;">4</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>我在实际环境中有一张表达到了几亿条数据，如果要用时下流行的sharding策略，必须在代码上做一些改动，当时偷了点懒直接使用partition功能，用了大半年倒也相安无事。只是分区表占用内存相当的大，如果有其它的表经常出现慢查询耗费服务器资源，那么雪崩效应就出现了，所有的查询都会堵塞变得非常慢。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/519/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>subcon使用笔记</title>
		<link>http://www.ooso.net/archives/530</link>
		<comments>http://www.ooso.net/archives/530#comments</comments>
		<pubDate>Thu, 10 Dec 2009 00:24:25 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=530</guid>
		<description><![CDATA[在温习flickr的ppt时，看到里面提到了一个叫subcon的工具。由于这个工具已经在google code上开源，所以我毫不客气的下载回来细细端详：


这工具用python写的

				<span class="readmore"><a href="http://www.ooso.net/archives/530" title="subcon使用笔记">阅读全文（745字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在温习flickr的ppt时，看到里面提到了一个叫<a href="http://code.google.com/p/subcon/">subcon</a>的工具。由于这个工具已经在google code上开源，所以我毫不客气的下载回来细细端详：</p>
<ol>
<ul>
<li>这工具用python写的</li>
<li>用svn保存配置文件，用subcon比较容易的部署到多台服务器上</li>
<li>既然是用svn保存，那么回滚到指定版本也是支持的</li>
<li>SystemImager工具的集成是做为添头附送的，理论上你可以利用这个工具做到一步安装服务器</li>
</ul>
</ol>
<p>初印象就是如此，实际的使用时，你首先要在svn上创建两个目录</p>
<ol>
<ul>
<li>base</li>
<li>roles</li>
</ul>
</ol>
<p>然后可以试着提交一些服务器配置文件例如/etc/hosts到base下,再通过简单命令把配置同步到服务器上<br />
<code>subcon -n</code></p>
<p>也可以指定别的类型的配置文件例如www或者memcache，以服务器apache配置文件/etc/httpd为例：</p>
<ol>
<ul>
<li>创建目录roles/www</li>
<li>提交/etc/httpd目录到roles/www目录下</li>
<li>十来个字符就可以部署这些文件到服务器上
<div class="igBar"><span id="lcode-28"><a href="#" onclick="javascript:showCodeTxt('code-28'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-28">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">subcon -n -owww </div>
</li>
</ol>
</div>
</div>
</div>
<p></li>
</ul>
</ol>
<p>同样的道理，只要在roles路径下新增各种类型的配置文件，就能比较快捷的部署到服务器上。还有些小功能，需要使用中自己挖掘了:</p>
<ol>
<ul>
<li>更改某个配置文件之后，自动执行指定的命令行，参看/etc/subcon.conf</li>
<li>回滚配置文件到指定版本的功能，这功能一般是在悲剧发生时才会使用吧？所以这个功能也很悲剧的有bug，手工hack一下才能使用。提示：在代码中找到revision字样，用int转换一下类型，命令行参数到了python里面估计都算string了。</li>
<li>如果有多个服务器共用部分配置文件，可以利用svn:externals属性创建一个链接</li>
<li>subcon实际上是python的svn客户端 + rsync</li>
</ul>
</ol>
<p>工具很简单，也有些bug,但是足够用了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/530/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何让gmail快速的自动检查pop3邮件</title>
		<link>http://www.ooso.net/archives/528</link>
		<comments>http://www.ooso.net/archives/528#comments</comments>
		<pubDate>Thu, 12 Nov 2009 09:54:35 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[firefox]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[greasemonkey]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=528</guid>
		<description><![CDATA[这是上一篇的升级版，上一次虽然可以手工检查pop3邮件，但是没有自动化始终是个缺憾。更新后的猴子脚本完全解决了这个问题，见下面的截图。

这个脚本做了两件事情：


				<span class="readmore"><a href="http://www.ooso.net/archives/528" title="如何让gmail快速的自动检查pop3邮件">阅读全文（240字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>这是上一篇的升级版，<a href="/archives/526">上一次虽然可以手工检查pop3邮件</a>，但是没有自动化始终是个缺憾。更新后的猴子脚本完全解决了这个问题，见下面的截图。</p>
<p><img src="http://www.ooso.net/wp-content/uploads/2009/11/aaa.jpg" alt="gmail quick checker" title="gmail quick checker" width="600" height="56" class="alignnone size-full wp-image-529" /></p>
<p>这个脚本做了两件事情：</p>
<ul>
<ol>
<li>左侧新增了一个 check now 按钮</li>
<li>右边新增了一个检查频率</li>
</ol>
</ul>
<p>声明，这个脚本只针对firefox用户。</p>
<p>使用方法：</p>
<ul>
<ol>
<li>安装greasemonkey插件</li>
<li>安装<a href="http://customcode.googlecode.com/files/GmailPop3Checker.user.js">我改写过的邮件检查脚本</a></li>
</ol>
</ul>
<p>我也不是这工具的原创，改写了一些而已，原版的<a href="http://greasefire.userscripts.org/scripts/show/60594">在这里</a></p>
<p>如果你不幸安装了上一个版本的脚本，那么记得卸载掉</p>
<p><strong>Update</strong>: 原来的脚本已不可用，请移步使用<a href="http://www.danielslaughter.com/projects/gmail-pop3-checker-for-greasemonkey/">另外一个更新脚本</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/528/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>gmail的pop3快速刷新脚本</title>
		<link>http://www.ooso.net/archives/526</link>
		<comments>http://www.ooso.net/archives/526#comments</comments>
		<pubDate>Fri, 30 Oct 2009 13:34:20 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[firefox]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[gmail]]></category>
		<category><![CDATA[greasemonkey]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=526</guid>
		<description><![CDATA[gmail接收pop3邮件的周期挺长的，如果邮件不是太频繁的话，差不多1小时才会刷新一次。如果你是firefox用户的话，解决办法是：


安装greasemonkey插件

				<span class="readmore"><a href="http://www.ooso.net/archives/526" title="gmail的pop3快速刷新脚本">阅读全文（242字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>gmail接收pop3邮件的周期挺长的，如果邮件不是太频繁的话，差不多1小时才会刷新一次。如果你是<a href="/?tag=firefox">firefox</a>用户的话，解决办法是：</p>
<ol>
<ul>
<li>安装<a href="/?tag=greasemonkey">greasemonkey</a>插件</li>
<li>安装<a href="http://userscripts.org/scripts/show/56812">猴子脚本 </a></li>
<li>安装<a href="http://customcode.googlecode.com/files/gmail_pop3_quick_checker.user.js">我改写过的邮件检查脚本</a></li>
</ul>
</ol>
<p>这个工具的用途是在页面上方的“刷新”旁边新增一个链接“刷新POP3邮件”，点击之后会马上检查所有的POP3邮箱。至于自动刷新功能，还等着看官你自己搞定呢。</p>
<p>BTW:如果是英文版的gmail，请手工修改gmail_pop3_quick_checker.user.js，我都提取出来了。 </p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/526/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在生产环境中使用php性能测试工具xhprof</title>
		<link>http://www.ooso.net/archives/522</link>
		<comments>http://www.ooso.net/archives/522#comments</comments>
		<pubDate>Wed, 05 Aug 2009 14:01:00 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[优化]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=522</guid>
		<description><![CDATA[xhprof是facebook开源出来的一个php性能测试工具，也可以称之为profile工具，这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比，有很多类似之处。以前对xdebug有一些记录还可以供参考，但是它的缺点是对性能影响太大，即便是开启了profiler_enable_trigger参数，用在生产环境中也是惨不忍睹，cpu立刻就飙到high。
而xhprof就显得很轻量，是否记录profile可以由程序控制，因此，用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法：

				<span class="readmore"><a href="http://www.ooso.net/archives/522" title="在生产环境中使用php性能测试工具xhprof">阅读全文（815字）</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="http://mirror.facebook.net/facebook/xhprof/doc.html">xhprof</a>是facebook开源出来的一个php性能测试工具，也可以称之为profile工具，这个词不知道怎么翻译才比较达意。跟之前一直使用的xdebug相比，有很多类似之处。以前<a href="http://www.ooso.net/archives/359">对xdebug有一些记录还可以供参考</a>，但是它的缺点是对性能影响太大，即便是开启了profiler_enable_trigger参数，用在生产环境中也是惨不忍睹，cpu立刻就飙到high。</p>
<p>而xhprof就显得很轻量，是否记录profile可以由程序控制，因此，用在生产环境中也就成为一种可能。在它的文档上可以看到这样一种用法：</p>
<p>以万分之一的几率启用xhprof，平时悄悄的不打枪。</p>
<div class="igBar"><span id="lphp-32"><a href="#" onclick="javascript:showCodeTxt('php-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-32">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">mt_rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span>, <span style="color:#CC66CC;color:#800000;">10000</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;xhprof_enable<span style="color:#006600; font-weight:bold;">&#40;</span>XHPROF_FLAGS_MEMORY<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#0000FF;">$xhprof_on</span> = <span style="color:#000000; font-weight:bold;">true</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在程序结尾处调用方法保存profile</p>
<div class="igBar"><span id="lphp-33"><a href="#" onclick="javascript:showCodeTxt('php-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-33">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$xhprof_on</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#FF9933; font-style:italic;">// stop profiler</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#0000FF;">$xhprof_data</span> = xhprof_disable<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#FF9933; font-style:italic;">// save $xhprof_data somewhere (say a central DB)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;...</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>也可以用register_shutdown_function方法指定在程序结束时保存xhprof信息，这样就免去了结尾处判断，给个改写的不完整例子：</p>
<div class="igBar"><span id="lphp-34"><a href="#" onclick="javascript:showCodeTxt('php-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-34">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">mt_rand</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">1</span>, <span style="color:#CC66CC;color:#800000;">10000</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;xhprof_enable<span style="color:#006600; font-weight:bold;">&#40;</span>XHPROF_FLAGS_MEMORY<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;<span style="color:#000066;">register_shutdown_function</span><span style="color:#006600; font-weight:bold;">&#40;</span>create_funcion<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">''</span>, <span style="color:#FF0000;">"$xhprof_data = xhprof_disable(); save $xhprof_data;"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>至于日志，我暂时用的是最土的文件形式保存，定期清除即可。</p>
<p>BTW:xhprof生成的图形方式profile真是酷毙了，哪段代码成为瓶颈，一目了然。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/522/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>启用memcached压缩注意事项</title>
		<link>http://www.ooso.net/archives/475</link>
		<comments>http://www.ooso.net/archives/475#comments</comments>
		<pubDate>Wed, 13 May 2009 23:56:05 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=475</guid>
		<description><![CDATA[在php开发中，开启memcache的数据压缩存储是一件很简单的事情。在多数情况下，压缩数据不仅不会降低程序的执行效率，反倒会因为网络传输的开销降低，带来速度提升。看看最常用的Memcache::set方法:
bool Memcache::set  ( string $key  , mixed $var  [, int $flag  [, int $expire  ]] )
在这个方法中，将$flag设置为MEMCACHE_COMPRESSED即可启用memcache压缩存储。
这样做有什么弊端？

				<span class="readmore"><a href="http://www.ooso.net/archives/475" title="启用memcached压缩注意事项">阅读全文（1179字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在php开发中，开启<a href="/?tag=memcache">memcache</a>的数据压缩存储是一件很简单的事情。在多数情况下，压缩数据不仅不会降低程序的执行效率，反倒会因为网络传输的开销降低，带来速度提升。看看最常用的Memcache::set方法:<br />
<code>bool Memcache::set  ( string $key  , mixed $var  [, int $flag  [, int $expire  ]] )</code></p>
<p>在这个方法中，将$flag设置为MEMCACHE_COMPRESSED即可启用memcache压缩存储。</p>
<h3>这样做有什么弊端？</h3>
<p>如果没有做额外判断，每一次写入memcache都会启用压缩，不管数据的大小。对应的，每次获得数据都需要做一次解压缩的操作，这是典型的一刀切手法。实际上在数据很小的情况下，不需要压缩，在这个基础上压缩省不了多少空间。</p>
<h3>更好的压缩策略？</h3>
<p>好了，我的想法是在数据超过一定大小（比如2k）的情况下，才开启压缩。这个好办，捋起袖子就干，在调用Memcache::set方法之前，首先判断一下数据的大小，一个strlen就搞定了，再简单不过了。</p>
<div class="igBar"><span id="lphp-38"><a href="#" onclick="javascript:showCodeTxt('php-38'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-38">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span> = <span style="color:#000000; font-weight:bold;">new</span> Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">connect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'localhost'</span>, <span style="color:#CC66CC;color:#800000;">11211</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$flag</span> = <span style="color:#000066;">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#CC66CC;color:#800000;">2048</span> ? MEMCACHE_COMPRESSED : <span style="color:#CC66CC;color:#800000;">0</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#FF0000;">'mykey'</span>, <span style="color:#0000FF;">$data</span>, <span style="color:#0000FF;">$flag</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>有人可能会问了，array和object怎么办，这玩意可不能用strlen判断长度。</p>
<p>这还真能难住我一阵子，要知道把array/object写入memcache的时候，php会自动做serialize，再把它当作字符串插入memcache。</p>
<div class="igBar"><span id="lphp-39"><a href="#" onclick="javascript:showCodeTxt('php-39'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-39">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$flag</span> = <span style="color:#000066;">strlen</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">serialize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$data</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>&gt; <span style="color:#CC66CC;color:#800000;">2048</span> ? MEMCACHE_COMPRESSED : <span style="color:#CC66CC;color:#800000;">0</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>谁会采用这段代码？看起来非常山寨，而且serialize也不快，赔本买卖。</p>
<h3>更好的办法！</h3>
<p>上面的文字都是废话，直接看这段就好。<a href="http://cn2.php.net/manual/en/function.memcache-setcompressthreshold.php">Memcache::setCompressThreshold方法</a>可以包办之前所有的逻辑。</p>
<blockquote><p>Memcache::setCompressThreshold — Enable automatic compression of large values</p></blockquote>
<p><code>bool Memcache::setCompressThreshold  ( int $threshold  [, float $min_savings  ] )</code></p>
<p>举个例子，下面这段会自动启用压缩策略，当数据大于2k时，以0.2的压缩比进行zlib。</p>
<div class="igBar"><span id="lphp-40"><a href="#" onclick="javascript:showCodeTxt('php-40'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-40">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$memcache</span>-&gt;<span style="color:#006600;">setCompressThreshold</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC66CC;color:#800000;">2000</span>, <span style="color:#CC66CC;color:#800000;">0</span>.<span style="color:#CC66CC;color:#800000;">2</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>根据我的测试结果，<strong>setCompressThreshold方法会忽略Memcache::set的flag参数</strong>。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/475/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Git-svn on cygwin</title>
		<link>http://www.ooso.net/archives/520</link>
		<comments>http://www.ooso.net/archives/520#comments</comments>
		<pubDate>Thu, 09 Apr 2009 22:55:21 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[common]]></category>
		<category><![CDATA[cygwin]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=520</guid>
		<description><![CDATA[开始试用最近比较红的git，我看中的是它的本地版本库功能，即便网络比较烂的时候，也可以在本地提交，等到了合适的时候一并传上去。由于以前的代码版本控制使用的是svn，所以我用git-svn过渡一下。
目前在windows下，最好的git客户端恐怕就是装一个cygwin。鼓捣了一个时辰，整理好一些可用的配置文件，陈列一下以备下次使用：
~/.bash_profile
偶尔还会使用svn验证一下check in的情况，刚转过来不放心啊，下面的配置是为了防止svn命令行乱码。命令行git-svn在/usr/sbin/git-core/路径下，是一个perl脚本，为了方便，我把这个路径加入了PATH环境变量。

				<span class="readmore"><a href="http://www.ooso.net/archives/520" title="Git-svn on cygwin">阅读全文（867字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>开始试用最近比较红的<a href="/?tag=git">git</a>，我看中的是它的本地版本库功能，即便网络比较烂的时候，也可以在本地提交，等到了合适的时候一并传上去。由于以前的代码版本控制使用的是svn，所以我用git-svn过渡一下。</p>
<p>目前在windows下，最好的git客户端恐怕就是装一个cygwin。鼓捣了一个时辰，整理好一些可用的配置文件，陈列一下以备下次使用：</p>
<h2>~/.bash_profile</h2>
<p>偶尔还会使用svn验证一下check in的情况，刚转过来不放心啊，下面的配置是为了防止svn命令行乱码。命令行git-svn在/usr/sbin/git-core/路径下，是一个perl脚本，为了方便，我把这个路径加入了PATH环境变量。</p>
<div class="igBar"><span id="lcode-44"><a href="#" onclick="javascript:showCodeTxt('code-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-44">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export PATH=$PATH:/usr/sbin/git-core/</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export SVN_EDITOR=vim</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export LC_ALL=en_US.<span style="">UTF</span>-<span style="color:#800000;color:#800000;">16</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export LC_CTYPE=en_US.<span style="">UTF</span>-<span style="color:#800000;color:#800000;">16</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export LANG=en_US.<span style="">UTF</span>-<span style="color:#800000;color:#800000;">16</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export XMODIFIERS=@im=Chinput3</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stty cs8 -istrip</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stty pass8</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">export LESSCHARSET=latin1 </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>~/.inputrc</h2>
<p>去掉注释即可</p>
<div class="igBar"><span id="lcode-45"><a href="#" onclick="javascript:showCodeTxt('code-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-45">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">set meta-flag on</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">set convert-meta off</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">set input-meta on</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">set output-meta on </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>~/.gitconfig</h2>
<div class="igBar"><span id="lcode-46"><a href="#" onclick="javascript:showCodeTxt('code-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-46">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>user<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">name = muhaha</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">email = aa@bb.<span style="">cc</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>color<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">diff = auto</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">status = auto</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">branch = auto</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#91;</span>alias<span style="color:#006600; font-weight:bold;">&#93;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">st = status</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">rb = svn rebase</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ci = commit -a</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">co = checkout </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>常用操作</h2>
<table border="1">
<tr>
<td>clone一个svn</td>
<td>git svn clone -s svn://xxx.xxx.com</td>
</tr>
<tr>
<td>类svn up操作？</td>
<td>git svn rebase</td>
</tr>
<tr>
<td>添加文件</td>
<td>git add xfile</td>
</tr>
<tr>
<td>check in</td>
<td>git ci</td>
</tr>
<tr>
<td>往svn库提交</td>
<td>git svn dcommit</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/520/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>关于mysql proxy 0.7.0</title>
		<link>http://www.ooso.net/archives/501</link>
		<comments>http://www.ooso.net/archives/501#comments</comments>
		<pubDate>Sun, 05 Apr 2009 09:12:40 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=501</guid>
		<description><![CDATA[听说mysql proxy 0.7.0即将发布，正好前些日子从bzr上获取mysql proxy的代码编译过，看看当时bzr的版本号，和现在也差不了多少。在这期间，我又花了一些时间把mysql配置成读写分离，出了不少妖蛾子，由于使用的人不太多，解决问题超费劲——搜索不到有帮助的内容啊。但是我可能是比较幸运的，最后成功的实现了读写分离，目前在开发环境运行的比较稳定，所以有必要做个笔记分享一下。
读写分离脚本的问题
刚启动mysql proxy的时候，经常报错 -- "Mysql server has gone away"。我进一步缩小了可能出问题的范围(把环境简化是很重要的查错手段哦！)，比如只连接一个mysql，或者只连接本机的mysql，没有太大帮助，最后是在mysql proxy的日志文件中看到些蛛丝马迹：

				<span class="readmore"><a href="http://www.ooso.net/archives/501" title="关于mysql proxy 0.7.0">阅读全文（1334字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>听说<a href="http://jan.kneschke.de/2009/4/4/mysql-proxy-0-7-0-pre-release">mysql proxy 0.7.0即将发布</a>，正好<a href="http://www.ooso.net/archives/495">前些日子从bzr上获取mysql proxy的代码编译过</a>，看看当时bzr的版本号，和现在也差不了多少。在这期间，我又花了一些时间把mysql配置成读写分离，出了不少妖蛾子，由于使用的人不太多，解决问题超费劲——搜索不到有帮助的内容啊。但是我可能是比较幸运的，最后成功的实现了读写分离，目前在开发环境运行的比较稳定，所以有必要做个笔记分享一下。</p>
<h2>读写分离脚本的问题</h2>
<p>刚启动mysql proxy的时候，经常报错 -- "Mysql server has gone away"。我进一步缩小了可能出问题的范围(把环境简化是很重要的查错手段哦！)，比如只连接一个mysql，或者只连接本机的mysql，没有太大帮助，最后是在mysql proxy的日志文件中看到些蛛丝马迹：</p>
<blockquote><p>
(critical) proxy-plugin.c:1367: (connect_server) [string "/usr/local/share/mysql-proxy/r..."]:69: .address is deprecated. Use .src.name or .dst.name instead<br />
(critical) (read_query) [string "/usr/local/share/mysql-proxy/r..."]:179: .address is deprecated. Use .src.name or .dst.name instead<br />
(critical) proxy-plugin.c.1115: I have no server backend, closing connection
</p></blockquote>
<p>在<a href="http://forums.mysql.com/list.php?146">mysql proxy的论坛</a>上看到有人碰到类似的问题，很简单，读写分离的lua脚本还是旧的，0.6.1时代的产物了，更糟糕的是，即便是即将发布的0.7.0，rw-splitting.lua也是旧版本的。lua脚本中的<b>.address</b>需要替换成<b>.src.name</b>或者<b>.dst.name</b>。</p>
<p><b>解决办法</b> —— 下载<a href="http://bazaar.launchpad.net/%7Ediego-fmpwizard/mysql-proxy/bug-43424/download/head%3A/rwsplitting.lua-20090112150705-l9v35osiopsn0nz0-10/rw-splitting.lua">更新之后的rw-splitting.lua</a>，情况会好转。</p>
<h2>使用prepare方法无法获得结果</h2>
<p>我在测试代码中采用php的pdo_mysql，单独连接mysql是毫无问题的，然而配合mysql proxy使用则是屡屡受挫，查询经常没有结果返回，比较随机，从日志中也找不到有帮助的内容。这次没有找到解决办法，所以我绕了过去，在连接mysql的时候使用伪prepare的方式：</p>
<div class="igBar"><span id="lphp-48"><a href="#" onclick="javascript:showCodeTxt('php-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-48">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#0000FF;">$option</span> = <span style="color:#000066;">array</span><span style="color:#006600; font-weight:bold;">&#40;</span>PDO::<span style="color:#006600;">ATTR_EMULATE_PREPARES</span> =&gt; <span style="color:#CC66CC;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>据称emulate方式的性能比prepare要好，所以这也算安慰奖了。</p>
<h2>专门的lua脚本分支</h2>
<p>lua脚本开发滞后，是一个比较严重的问题，所以在邮件组上看到有个新的lua脚本分支出来 —— <a href="https://launchpad.net/mysql-proxy-lua-scripts">https://launchpad.net/mysql-proxy-lua-scripts</a>。希望开发速度能跟上来。</p>
<h2>keepalive参数</h2>
<p>mysql proxy还不算太稳定，偶尔crash我也不觉得惊讶，所以新增的keepalive参数很有用。在proxy启动的时候，加上--keepalive参数，它便会努力保持proxy的运行状态，停止了也会自动重启。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/501/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>apache的RewriteMap使用心得</title>
		<link>http://www.ooso.net/archives/493</link>
		<comments>http://www.ooso.net/archives/493#comments</comments>
		<pubDate>Thu, 02 Apr 2009 13:57:47 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[apache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=493</guid>
		<description><![CDATA[在apache的环境下，rewrite还真是生活之友啊，时不时就得用上。前些日子有个需求，要将url重新转一转。
什么情况？
原来的url
http://www.xxx.com/demo/oldpage.php?param1=1&#038;param2=2

				<span class="readmore"><a href="http://www.ooso.net/archives/493" title="apache的RewriteMap使用心得">阅读全文（1250字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在apache的环境下，rewrite还真是生活之友啊，时不时就得用上。前些日子有个需求，要将url重新转一转。</p>
<h2>什么情况？</h2>
<p>原来的url</p>
<p>http://www.xxx.com<b>/demo/oldpage.php?param1=1&#038;param2=2</b></p>
<p>转换后的url</p>
<p>http://www.xxx.com/newpage.php?url=<b>%2Fdemo%2Fmypage.php%3Fparam1%3D1&#038;param2%3D2</b></p>
<p>需要把粗体部分的url进行urlencode，能看出上面的字符"?&#038;="都分别转义过，作为参数发给另外一个url。那么这时候请出rewrite还真是最合适不过了。</p>
<h2>坎坷的Rewrite经历</h2>
<p>查查rewrite手册，俺这才知道，转义这活，非得派出<a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#mapfunc">RewriteMap的map function</a>才能做的比较漂亮。现在只有四个内部map function可供差遣： </p>
<ul>
<li>toupper: Converts the key to all upper case.</li>
<li>tolower: Converts the key to all lower case.</li>
<li>escape: Translates special characters in the key to hex-encodings.</li>
<li>unescape: Translates hex-encodings in the key back to special characters.</li>
</ul>
<p>那么很快就有了第一个rewrite出现：<br />
<code><br />
RewriteMap escape int:escape<br />
RewriteRule ^/([^/]*)$ /newpage.php?mi_url_suffix=${escape:$1?%{QUERY_STRING}} [L,PT]<br />
</code></p>
<p>注：这里的int不是intger的意思，它是internal的缩写，表示调用内部函数。</p>
<p>看上去非常简单，跑起来貌似也正....常？且慢，俺打开RewriteLog一瞅，形式不容乐观啊，"&#038;"字符通通没有转义。看来是失败了，爬到狗狗上翻了一下，貌似escape对"?="之类的特殊字符是不做转义的，晕。</p>
<h2>RewriteMap到底</h2>
<p>接着细看apache的rewrite手册，发现RewriteMap还支持自定义脚本，那么还得使出俺的看家绝技——php了。首先弄一个能转义的php，必须非常简单，复杂了apache容易挂掉，写出来发现想复杂都挺难啊：</p>
<p>/usr/local/bin/escape.php</p>
<div class="igBar"><span id="lphp-50"><a href="#" onclick="javascript:showCodeTxt('php-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">PHP:</span>
<div id="php-50">
<div class="php">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#008000; font-style:italic;">#!/usr/bin/php -f</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">&lt;?php</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#616100;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$in</span> = <span style="color:#000066;">trim</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#000066;">fgets</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDIN<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#000066;">fputs</span><span style="color:#006600; font-weight:bold;">&#40;</span>STDOUT, <span style="color:#000066;">urlencode</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF;">$in</span><span style="color:#006600; font-weight:bold;">&#41;</span> . <span style="color:#FF0000;">"<span style="color:#000099; font-weight:bold;">\r</span><span style="color:#000099; font-weight:bold;">\n</span>"</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#000000; font-weight:bold;">?&gt;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>在这个脚本里可别使用php:://stdin之类的，具体原因查<a href="/category/php">php</a>手册。相应的，rewrite规则如下：<br />
<code><br />
RewriteMap escape prg:/usr/local/bin/escape.php<br />
RewriteRule ^/([^/]*)$ /newpage.php?mi_url_suffix=${escape:$1?%{QUERY_STRING}} [L,PT]<br />
</code></p>
<p>rewrite规则没有太大的改变，prg表示使用自定义脚本。现在这个版本总算正常运作了。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/493/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>wordpress plugin &#8220;search engine related posts&#8221; 1.2发布</title>
		<link>http://www.ooso.net/archives/516</link>
		<comments>http://www.ooso.net/archives/516#comments</comments>
		<pubDate>Sun, 29 Mar 2009 02:19:45 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[google]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=516</guid>
		<description><![CDATA[去年写了个wordpress插件"search engine related posts"，不知不觉也有将近一年没有更新过了。这个插件的作用：当用户从google搜索到你的博客上，自动显示站内搜索相关的主题。这里没有用到数据库来做相关的搜索工作，而是利用google公开的一些api进行查询，因此对个人博客来说，不会增加服务器负载，完全只是一段js代码而已。
本次1.2相对第一个版本的改动

将javascript提取出来放到单独的js文件里，这是为了减小每个url的大小，浏览器也会对外部js进行缓存，有效的减少带宽输出

				<span class="readmore"><a href="http://www.ooso.net/archives/516" title="wordpress plugin &#8220;search engine related posts&#8221; 1.2发布">阅读全文（479字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>去年写了个wordpress插件"<a href="http://www.ooso.net/archives/392">search engine related posts</a>"，不知不觉也有将近一年没有更新过了。这个插件的作用：当用户从google搜索到你的博客上，自动显示站内搜索相关的主题。这里没有用到数据库来做相关的搜索工作，而是利用google公开的一些api进行查询，因此对个人博客来说，不会增加服务器负载，完全只是一段js代码而已。</p>
<h2>本次1.2相对第一个版本的改动</h2>
<ul>
<li>将javascript提取出来放到单独的js文件里，这是为了减小每个url的大小，浏览器也会对外部js进行缓存，有效的减少带宽输出</li>
<li>js文件已经用<a href="http://developer.yahoo.com/yui/compressor/">yui compressor</a>压缩过，只有900多个字节</li>
<li>支持多语种搜索，修改文件中的$related_posts_lang变量即可，中文用户需要将这个值修改为zh-CN(默认是en-US)</li>
<li>可以去除网页标题中的站名以及部分连接字符，让标题更简短且有意义</li>
</ul>
<h2>下载search engine related posts 1.2</h2>
<p><a href="http://wordpress.org/extend/plugins/search-engine-related-posts/">http://wordpress.org/extend/plugins/search-engine-related-posts/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/516/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>starling试用手记</title>
		<link>http://www.ooso.net/archives/506</link>
		<comments>http://www.ooso.net/archives/506#comments</comments>
		<pubDate>Sat, 21 Mar 2009 10:48:53 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>
		<category><![CDATA[queue]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=506</guid>
		<description><![CDATA[twitter最近将ruby实现的消息队列服务器starling开源了，这是一个支持memcache协议的轻量级持久化服务器，因此使用php/perl/ruby/java等多种客户端都没问题，可以将较慢的处理逻辑通过消息队列放在后台处理，同时也支持多点分布式处理。周末找了个闲置的centos 5机器搭了一套starling试用，配合php的memcache扩展测试一番，有点意思。
starling安装步骤
centos默认不带ruby，得重新装，以下安装步骤都是以root身份跑的。


				<span class="readmore"><a href="http://www.ooso.net/archives/506" title="starling试用手记">阅读全文（745字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>twitter最近将ruby实现的<a href="http://github.com/starling/starling/tree/master">消息队列服务器starling</a>开源了，这是一个支持<a href="/?tag=memcache">memcache</a>协议的轻量级持久化服务器，因此使用php/perl/ruby/java等多种客户端都没问题，可以将较慢的处理逻辑通过消息队列放在后台处理，同时也支持多点分布式处理。周末找了个闲置的centos 5机器搭了一套starling试用，配合<a href="/?tag=php">php</a>的memcache扩展测试一番，有点意思。</p>
<h2>starling安装步骤</h2>
<p>centos默认不带ruby，得重新装，以下安装步骤都是以root身份跑的。<br />
<code><br />
yum install ruby ruby-devel rubygems<br />
gem install memcache-client starling<br />
</code></p>
<p>如果你使用yum找不到ruby的相关包，请记得<a href="http://www.ooso.net/archives/495">添加epel repository</a>。安装之后新增的程序：</p>
<ul>
<li>/usr/bin/starling   	#一个ruby脚本，starling的服务程序</li>
<li>/usr/bin/starling_top 	#starling的状态检查程序，可以查看starling的运行状态，类似memcache的状态显示，不同的是能够显示每个key的状态</li>
</ul>
<p>starling启动后默认会在22122端口蹲点守候。</p>
<p>为了使用方便，我修改了一个starling在centos下的启动脚本，放在/etc/init.d/starling，下载地址：<a href="http://customcode.googlecode.com/files/starling">http://customcode.googlecode.com/files/starling</a>。使用方法：<br />
<code>/etc/init.d/starling start|stop|restart</code></p>
<h2>测试程序</h2>
<p>以下是在测试中用的php脚本，说实话php在循环比较大的时候没啥优势，但是关键是简单，几行就搞定了。</p>
<p><strong>写入的测试程序</strong></p>
<div class="igBar"><span id="lcode-53"><a href="#" onclick="javascript:showCodeTxt('code-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-53">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#!/usr/bin/php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m = new Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m-&gt;<span style="">addServer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'127.0.0.1'</span>, <span style="color:#CC0000;">'22122'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$start = microtime<span style="color:#006600; font-weight:bold;">&#40;</span>true<span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">for<span style="color:#006600; font-weight:bold;">&#40;</span>$i = <span style="color:#800000;color:#800000;">0</span>; $i &lt;<span style="color:#800000;color:#800000;">10000</span>; ++$i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color:#FF9933; font-style:italic;">//echo $i, &quot;\n&quot;;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; $m-&gt;<span style="">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'testtesttesttest'</span>, <span style="color:#CC0000;">'中文测试中文测试中文测试中文测试中文测试中文测试中中文测试中中文中a'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">echo <span style="color:#CC0000;">"time:"</span> . <span style="color:#006600; font-weight:bold;">&#40;</span>microtime<span style="color:#006600; font-weight:bold;">&#40;</span>true<span style="color:#006600; font-weight:bold;">&#41;</span> - $start<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#CC0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>读出来的测试程序</strong></p>
<div class="igBar"><span id="lcode-54"><a href="#" onclick="javascript:showCodeTxt('code-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-54">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">#!/usr/bin/php</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&lt;?php</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m = new Memcache;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$m-&gt;<span style="">addServer</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'127.0.0.1'</span>, <span style="color:#CC0000;">'22122'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">while<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; echo $m-&gt;<span style="">get</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'test'</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#CC0000;">"<span style="color:#000099; font-weight:bold;">\n</span>"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; usleep<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#800000;color:#800000;">100</span><span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#FF9933; font-style:italic;">// 休息一下，否则容易cpu 100%</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color:#006600; font-weight:bold;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h2>性能测试</h2>
<p>测试条件:</p>
<ul>
<li>key的长度16B</li>
<li>value的长度100B,</li>
<li>8个并发写入进程</li>
<li>每个进程插入10,000条记录</li>
</ul>
<p>平均每个进程花了7秒完成写入操作，那么照这样计算：</p>
<p><strong>10000 * 8 / 7 = 每秒写入11428次</strong></p>
<p>以上测试进行的比较随意，而且我懒得插入大量数据来测试了，这个比较花时间，所以测试结果仅供参考。由于starling是目前twitter在生产环境中运行的，经过实践检验过，稳定性应该不成问题。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/506/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>在centos 5.2下安装最新的mysql proxy</title>
		<link>http://www.ooso.net/archives/495</link>
		<comments>http://www.ooso.net/archives/495#comments</comments>
		<pubDate>Sat, 14 Mar 2009 14:09:16 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[centos]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=495</guid>
		<description><![CDATA[mysql proxy的代码树已经迁移到lauchpad，采用bazaar进行版本管理。参考了一些文档，在centos 5.2下编译安装最新mysql proxy成功。步骤记录如下（在centos 5下应该也适用）：
首先让EPEL (Extra Packages for Enterprise Linux) repository 生效
 # rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-2.noarch.rpm 

				<span class="readmore"><a href="http://www.ooso.net/archives/495" title="在centos 5.2下安装最新的mysql proxy">阅读全文（1677字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>mysql proxy的代码树已经迁移到lauchpad，采用bazaar进行版本管理。参考了一些文档，在centos 5.2下编译安装最新<a href="/?tag=mysql">mysql</a> proxy成功。步骤记录如下（在centos 5下应该也适用）：</p>
<p>首先让EPEL (Extra Packages for Enterprise Linux) repository 生效<br />
<code> # rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-2.noarch.rpm </code></p>
<p>确定这些包已经安装：GNU Autotools, flex, pkg-config, bazaar, MySQL client libraries<br />
<code> # yum install autoconf automake libtool flex pkgconfig bzr mysql-devel </code></p>
<p>centos下自带的libevent版本超老，这个没有别的办法，只能自己重新编译，版本需要在1.4.0以上，越高越好<br />
<code><br />
$ wget http://monkey.org/~provos/libevent-1.4.9-stable.tar.gz<br />
$ tar zvfx libevent-1.4.9-stable.tar.gz<br />
$ cd libevent-1.4.9-stable<br />
$ ./configure<br />
$ make<br />
# make install<br />
</code></p>
<p>centos自带的glib版本也比较老，mysql proxy 0.7.0以上需要glib2 2.16.0以上才能编译成功，因此不得不重新编译glib<br />
<code><br />
$ wget http://ftp.gnome.org/pub/gnome/sources/glib/2.18/glib-2.18.4.tar.gz<br />
$ tar zvfx glib-2.18.4.tar.gz<br />
$ cd glib-2.18.4<br />
$ ./configure<br />
$ make<br />
# make install<br />
</code></p>
<p>编译安装lua 5.1<br />
<code><br />
$ wget http://www.lua.org/ftp/lua-5.1.4.tar.gz<br />
$ tar zvfx lua-5.1.4.tar.gz<br />
$ cd lua-5.1.4<br />
$ vi src/Makefile<br />
</code><br />
在CFLAGS里加上-fPIC，因为我在64位机上编译出现了“relocations”错误</p>
<p><code><br />
$ make linux<br />
# make install<br />
# cp etc/lua.pc /usr/local/lib/pkgconfig/<br />
</code></p>
<p><strong>重要：让pkg-config找到自己编译的库在哪里 </strong><br />
<code><br />
$ export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig<br />
</code></p>
<p>最后，从bazaar中检出最新的mysql-proxy源文件进行编译：<br />
<code><br />
$ bzr branch lp:mysql-proxy<br />
$ cd mysql-proxy<br />
$ ./autogen.sh<br />
$ ./configure<br />
$ make<br />
# make install<br />
</code></p>
<p>编译完成，可以检查一下最终结果：<br />
<code><br />
# mysql-proxy -V<br />
</code></p>
<p><code><br />
mysql-proxy 0.7.0<br />
  glib2: 2.18.4<br />
  libevent: 1.4.9-stable<br />
  admin: 0.7.0<br />
  proxy: 0.7.0<br />
</code></p>
<p>配置启动脚本，在系统启动时运行mysql-proxy<br />
<code><br />
cd /etc/init.d<br />
wget http://customcode.googlecode.com/files/mysql-proxy<br />
chmod 0755 /etc/init.d/mysql-proxy<br />
chkconfig mysql-proxy on<br />
</code></p>
<p>增加运行参数<br />
<code><br />
cat >/etc/sysconfig/mysql-proxy<br />
</code></p>
<p><code><br />
# Options to mysql-proxy<br />
# do not remove --daemon<br />
PROXY_OPTIONS="--daemon"<br />
</code></p>
<p>CTRL+D保存，然后就可以使用以下命令启动|停止mysql-proxy<br />
<code><br />
/etc/init.d/mysql-proxy start|stop<br />
</code></p>
<p>参考文档：<br />
<a href="http://blog.zhuzhaoyuan.com/2009/02/how-to-compile-and-install-mysql-proxy-from-bazaar-on-centos-52/">How to Compile and Install MySQL Proxy from Bazaar on CentOS 5.2</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/495/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>131个字符的php framework</title>
		<link>http://www.ooso.net/archives/415</link>
		<comments>http://www.ooso.net/archives/415#comments</comments>
		<pubDate>Tue, 10 Mar 2009 06:40:03 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[framework]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=415</guid>
		<description><![CDATA[在friendfeed上看到这个链接 —— The 140 Characters Webapp Challenge!，这个比赛要求用140个字符的代码造就一个web应用。
里头有36个程序可供投票，基本上全是脚本语言大杂烩：php,perl,ruby,javascript。实现的应用也是五花八门，有相册，类twitter，小游戏，甚至还有php代码框架？摘录如下：
PLAIN TEXT
CODE:

				<span class="readmore"><a href="http://www.ooso.net/archives/415" title="131个字符的php framework">阅读全文（632字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>在<a href="https://friendfeed.com">friendfeed</a>上看到这个链接 —— <a href="http://f055.net/article/final-wrap-up-of-the-140-characters-webapp-challenge/">The 140 Characters Webapp Challenge!</a>，这个比赛要求用140个字符的代码造就一个web应用。</p>
<p>里头有36个程序可供投票，基本上全是脚本语言大杂烩：php,perl,ruby,javascript。实现的应用也是五花八门，有相册，类twitter，小游戏，甚至还有php代码框架？摘录如下：</p>
<div class="igBar"><span id="lcode-57"><a href="#" onclick="javascript:showCodeTxt('code-57'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-57">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">require __DIR__.<span style="color:#CC0000;">'/c.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">if <span style="color:#006600; font-weight:bold;">&#40;</span>!is_callable<span style="color:#006600; font-weight:bold;">&#40;</span>$c = @$_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ?: function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> echo <span style="color:#CC0000;">'Woah!'</span>; <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Error'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>这段代码利用了php5.3的一些新特性：</p>
<ul>
<li>__DIR__</li>
<li>Anonymous functions</li>
<li>?:运算符</li>
</ul>
<p>代码只有131个字符，由于代码极为简陋，安全性也是没得保障的，只能算一个程序的统一入口罢了。</p>
<p>如果用php 5.2来写这段代码，大概就是：</p>
<div class="igBar"><span id="lcode-58"><a href="#" onclick="javascript:showCodeTxt('code-58'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">CODE:</span>
<div id="code-58">
<div class="code">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">require dirname<span style="color:#006600; font-weight:bold;">&#40;</span>__FILE__<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#CC0000;">'/c.php'</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">if <span style="color:#006600; font-weight:bold;">&#40;</span>!is_callable<span style="color:#006600; font-weight:bold;">&#40;</span>$c = @$_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> ? $_GET<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC0000;">'c'</span><span style="color:#006600; font-weight:bold;">&#93;</span> : create_function<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">''</span>, <span style="color:#CC0000;">"echo 'Woah!';"</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; throw new Exception<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0000;">'Error'</span><span style="color:#006600; font-weight:bold;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$c<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>如果要让这段代码变得实用点，可以在$c前面加上一个前缀，这样安全性会有进一步提升，代码也会相应的增加若干字节。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/415/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>解决memcache连接奇慢问题一例</title>
		<link>http://www.ooso.net/archives/479</link>
		<comments>http://www.ooso.net/archives/479#comments</comments>
		<pubDate>Sat, 07 Mar 2009 03:19:44 +0000</pubDate>
		<dc:creator>Volcano</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[memcache]]></category>

		<guid isPermaLink="false">http://www.ooso.net/?p=479</guid>
		<description><![CDATA[最近用xdebug观察线上程序的运行时间统计，发现往日里跑起来像飞的memcache居然是系统中拖后腿的耗时大户，连接时间特长。
运行环境

webserver是apache + php

				<span class="readmore"><a href="http://www.ooso.net/archives/479" title="解决memcache连接奇慢问题一例">阅读全文（788字）</a></span>]]></description>
			<content:encoded><![CDATA[<p>最近用xdebug观察线上程序的运行时间统计，发现往日里跑起来像飞的<a href="/?tag=memcache">memcache</a>居然是系统中拖后腿的耗时大户，连接时间特长。</p>
<h2>运行环境</h2>
<ul>
<li>webserver是apache + php</li>
<li>php memcache extension版本是3.0.2，当时是最新的beta版...</li>
<li>有4个memcache server可供使用</li>
<li>代码中会利用php的Memcache::addServer依次连接四个memcache，长连接方式</li>
</ul>
<h2>现象</h2>
<p>完成四次addServer一共需要300ms以上，但是一旦连接上，获取单个item飞快，时间在3ms以下。<br />
更可恶的问题在于，虽然执行了四次Memcache::addServer，但是实际使用的始终是最后一个memcache，这实在让人崩溃。</p>
<h2>问题解决</h2>
<p>使用了一点搜索技巧，在pecl.php.net上找到了类似的bug: <a href="http://pecl.php.net/bugs/bug.php?id=13483">First get slow when using multiple memcached servers</a></p>
<p>这个bug的描述如下：</p>
<blockquote><p>
We are monitoring memcached performance and noticed that when we added a second memcached via Memcache::addServer the first get request is always slower than the subsequent ones although we are using persitent memcached connections. Switching from crc32 to fnv hashing didn't help either. Is that delay explainable
</p></blockquote>
<p>看起来是最新的memcache extension有一些问题，尝试将这个扩展降级成最新的稳定版2.2.6，然后重启apache看看，memcache连接过慢的问题果然已经解决。</p>
<h2>结论</h2>
<p>吃螃蟹果然是要付出代价的。。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ooso.net/archives/479/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 2.995 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2010-03-14 02:00:24 -->
