<?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>The Ji Village News</title> <atom:link href="http://www.haidongji.com/feed/" rel="self" type="application/rss+xml" /><link>http://www.haidongji.com</link> <description>季庄新闻--Haidong Ji's Blog</description> <lastBuildDate>Fri, 31 May 2013 05:20:11 +0000</lastBuildDate> <language>en-US</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.4.2</generator> <item><title>MOOC印象和推荐</title><link>http://www.haidongji.com/2013/05/31/mooc%e5%8d%b0%e8%b1%a1%e5%92%8c%e6%8e%a8%e8%8d%90/</link> <comments>http://www.haidongji.com/2013/05/31/mooc%e5%8d%b0%e8%b1%a1%e5%92%8c%e6%8e%a8%e8%8d%90/#comments</comments> <pubDate>Fri, 31 May 2013 05:20:11 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Technology]]></category> <category><![CDATA[ThoughtsOnTechnology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1459</guid> <description><![CDATA[MOOC（Massive Open Online Course）是新近起步的网上教育。我从去年年中开始关注，已经学成结业两门课，感觉很有收获，特写此文推荐。 MOOC算是我知道的80年代的电视大学的延伸。我记忆中的中国80年代是开放、蓬勃、纯真和好学的年代。那么多的人想读书并真正去读书。特别是80年代中后期，电视开始逐渐进入普通家庭，当时的电大教育给那时的小青年甚至中年人提供了接受高等教育的机会，其对个人和社会的整体效益不可低估。那时我只是个十几岁的孩子，没跟着电视上大学，但我知道有人听电视上的高等数学、物理、化学、文学等的授课，应当还有函授式的作业。 MOOC是在电大基础上的延伸和扩展，只不过由于是在网上以及互联网在全球的普及，其影响力更大。能上网的人都可以学，免费。以我有限的体会，我注意到的特点有： 目前我了解到的网上电大有Coursera，Udacity，和edX。都不错； 授课采用短小的录像。录像长度平均十来分钟，在线播放和下载都可以； 一般每段录像都以一个主题为中心，中间会穿插些选择或填空题。这些题目用来测试你对刚刚讲过的概念、方法等的理解； Coursera录像有字幕选项。如果理解英语有困难，按下字幕键后读字幕会有帮助； 一般每周会有家庭作业。我上的都是电脑课，所以自动化程度高一些。作业都是些编程和回答数据相关问题，提交后的反馈很快； 有的课程会有同学评分。由于学生来自全球各地，所以评分都是匿名，也很有意思； 每个科目都有网上论坛。在此潜水、提问、回答问题等都非常有帮助。了解来自世界各地的人都观点是很有意思的事情； 根据课程的不同，有的会有期中或期末考试。如上文所述，我选的都是和电脑有关，考试都是选择题，反馈很快； 课程介绍里会告诉你如何算通过该课程。通过后一般有一张认证。 深受世界人民欢迎的课程一般会重新上线。所以条件不允许而不能上（或不能坚持下去）某节课，以后很可能还有机会。 我个人以为这些课程对在职和在校的人士都很有帮助，这对大学里的老师也会有启发，也是学习、提高英语的好方法。如能抽出时间上这些课，应当很有收获。 我的建议： 确保有时间。家庭、工作第一。如上所述，如果目前时机不成熟，以后还有机会； 一般来讲上电脑课，有个虚拟机比较好。虚拟机给你除掉后顾之忧，在上面放心大胆地尝试，玩它个爽歪歪； 要做作业。光听、光看不动手当然不如甩开膀子干一场的效果好； 看录像时利用英文字幕。这对提高英语和了解专有名词术语有帮助。在Coursera的在线录像里，如有必要，你还可以放慢录像速度。如英语过关，也有加快速度功能； 有同党和狐朋狗友一起学效果会更好。大家互相激励、分享和解难答疑就能共同提高。 推荐课程： Introduction to Systematic Program Design &#8211; Part 1 这门课下周一正式开始，但今天老师已经公布了下两周的录像和作业。Gregor Kiczales老师来自加拿大的英属哥伦比亚大学，University of British Columbia。Kiczales是大牛，当然这不重要，因为大牛未必是好老师。但从我看过的录像和网站的准备工作来看，他是个好老师。力荐！ 如你对编程感兴趣，Dan Grossman的Programming Languages非常好。现在已下线，但重新上线的可能性非常大。这门课很花时间。另外，Scala的作者，Martin Odersky的Functional Programming Principles in Scala也很好。我跟着学了一段时间，但由于其他事情太多没时间而放弃。等以后有时间会重修。 好，就写到这儿。比较晚了，按说早该上床睡觉了。咳，谁让我那么爱我遍及五湖四海的读者呢]]></description> <content:encoded><![CDATA[<p>MOOC（Massive Open Online Course）是新近起步的网上教育。我从去年年中开始关注，已经学成结业两门课，感觉很有收获，特写此文推荐。</p><p>MOOC算是我知道的80年代的电视大学的延伸。我记忆中的中国80年代是开放、蓬勃、纯真和好学的年代。那么多的人想读书并真正去读书。特别是80年代中后期，电视开始逐渐进入普通家庭，当时的电大教育给那时的小青年甚至中年人提供了接受高等教育的机会，其对个人和社会的整体效益不可低估。那时我只是个十几岁的孩子，没跟着电视上大学，但我知道有人听电视上的高等数学、物理、化学、文学等的授课，应当还有函授式的作业。</p><p>MOOC是在电大基础上的延伸和扩展，只不过由于是在网上以及互联网在全球的普及，其影响力更大。能上网的人都可以学，免费。以我有限的体会，我注意到的特点有：</p><ol><li>目前我了解到的网上电大有Coursera，Udacity，和edX。都不错；</li><li>授课采用短小的录像。录像长度平均十来分钟，在线播放和下载都可以；</li><li>一般每段录像都以一个主题为中心，中间会穿插些选择或填空题。这些题目用来测试你对刚刚讲过的概念、方法等的理解；</li><li>Coursera录像有字幕选项。如果理解英语有困难，按下字幕键后读字幕会有帮助；</li><li>一般每周会有家庭作业。我上的都是电脑课，所以自动化程度高一些。作业都是些编程和回答数据相关问题，提交后的反馈很快；</li><li>有的课程会有同学评分。由于学生来自全球各地，所以评分都是匿名，也很有意思；</li><li>每个科目都有网上论坛。在此潜水、提问、回答问题等都非常有帮助。了解来自世界各地的人都观点是很有意思的事情；</li><li>根据课程的不同，有的会有期中或期末考试。如上文所述，我选的都是和电脑有关，考试都是选择题，反馈很快；</li><li>课程介绍里会告诉你如何算通过该课程。通过后一般有一张认证。</li><li>深受世界人民欢迎的课程一般会重新上线。所以条件不允许而不能上（或不能坚持下去）某节课，以后很可能还有机会。</li></ol><p>我个人以为这些课程对在职和在校的人士都很有帮助，这对大学里的老师也会有启发，也是学习、提高英语的好方法。如能抽出时间上这些课，应当很有收获。</p><p>我的建议：</p><ol><li>确保有时间。家庭、工作第一。如上所述，如果目前时机不成熟，以后还有机会；</li><li>一般来讲上电脑课，有个虚拟机比较好。虚拟机给你除掉后顾之忧，在上面放心大胆地尝试，玩它个爽歪歪；</li><li>要做作业。光听、光看不动手当然不如甩开膀子干一场的效果好；</li><li>看录像时利用英文字幕。这对提高英语和了解专有名词术语有帮助。在Coursera的在线录像里，如有必要，你还可以放慢录像速度。如英语过关，也有加快速度功能；</li><li>有同党和狐朋狗友一起学效果会更好。大家互相激励、分享和解难答疑就能共同提高。</li></ol><p>推荐课程：<br
/> <a
href="https://class.coursera.org/programdesign-001/class/index">Introduction to Systematic Program Design &#8211; Part 1</a><br
/> 这门课下周一正式开始，但今天老师已经公布了下两周的录像和作业。Gregor Kiczales老师来自加拿大的英属哥伦比亚大学，University of British Columbia。Kiczales是大牛，当然这不重要，因为大牛未必是好老师。但从我看过的录像和网站的准备工作来看，他是个好老师。力荐！</p><p>如你对编程感兴趣，Dan Grossman的Programming Languages非常好。现在已下线，但重新上线的可能性非常大。这门课很花时间。另外，Scala的作者，Martin Odersky的Functional Programming Principles in Scala也很好。我跟着学了一段时间，但由于其他事情太多没时间而放弃。等以后有时间会重修。</p><p>好，就写到这儿。比较晚了，按说早该上床睡觉了。咳，谁让我那么爱我遍及五湖四海的读者呢 <img
src='http://www.haidongji.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2013/05/31/mooc%e5%8d%b0%e8%b1%a1%e5%92%8c%e6%8e%a8%e8%8d%90/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Setting up Windows development environment with VirtualBox</title><link>http://www.haidongji.com/2013/04/01/setting-up-windows-development-environment-with-virtualbox/</link> <comments>http://www.haidongji.com/2013/04/01/setting-up-windows-development-environment-with-virtualbox/#comments</comments> <pubDate>Mon, 01 Apr 2013 19:55:17 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Linux]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1444</guid> <description><![CDATA[Over the years, I&#8217;ve used many virtualization software for testing, hacking, and development work. So far my favorite virtualization software is VirtualBox. I&#8217;ve written about using Vagrant and Veewee to make that process easier. I&#8217;ve since changed my strategy slightly: I don&#8217;t recommend using Veewee anymore, because silly Ruby compatibility issues. I like the idea [...]]]></description> <content:encoded><![CDATA[<p>Over the years, I&#8217;ve used many virtualization software for testing, hacking, and development work. So far my favorite virtualization software is VirtualBox.</p><p>I&#8217;ve written about <a
href="http://www.haidongji.com/2012/02/24/recommendation-vagrant-and-veewee/">using Vagrant and Veewee to make that process easier</a>.  I&#8217;ve since changed my strategy slightly:</p><ul><li>I don&#8217;t recommend using Veewee anymore, because silly Ruby compatibility issues. I like the idea of Veewee, but in my opinion jumping through hoops to get it to work with Vagrant is no longer worth it. It is often quicker to just build a VirtualBox vm and then create a Vagrant box out of it than taking the Veewee extra step and remembering those extra commands. I couldn&#8217;t find the link now, but I thought I read somewhere that Vagrant has plans to make VM box creation easier;</li><li>I don&#8217;t use Vagrant for managing Windows VMs anymore. I tried but my effort was not too successful so far. Again, I read it somewhere that Windows support will be provided in future release of Vagrant. We will see.</li></ul><p>My use cases in the past mostly involves single VM. For networking among Linux VMs, <a
href="http://www.haidongji.com/2012/02/24/recommendation-vagrant-and-veewee/">the instruction in my Vagrant post still works</a>. But what if you want to build a Windows VM environment with domain controller, database servers, web servers, and other application servers based on a single VM image. So far I&#8217;ve built an environment with a domain controller and a seperate application server (I will soon install SQL Server on it). Here is my notes for reference. The host machine is Lenovo W520 running Linux Mint 14.</p><ol><li><a
href="http://technet.microsoft.com/en-us/evalcenter/hh670538.aspx">Download Windows 2012 ISO here</a>;</li><li>Start creating a base Windows 2012 VM in VirtualBox. Use the vdi disk type to make it easier working with VirtualBox;</li><li>I could have installed the Data Center eval edition, but chose the standard instead. I didn&#8217;t choose the Core edition. Using the Core edition might make more sense in a real production environment, but I&#8217;d like to get something going quickly and also want to use the same image for all future VM needs, so the standard non-Core install works the best for now. I may come back and play with the Core edition down the road;<p>Note: There are sites that talks about the possibility of activating Windows with an official license key, so that future VMs based on this image no longer need activation. My advice is: don&#8217;t bother with it. Licensing issue aside, my reasoning is that if you decide the VMs are going to be part of a domain, they then need to have their unique SID, which requires a separate Windows key, I think. Plus, I&#8217;ve run eval edition of Windows that has past its validation date but it still functions, probably in some limited capacity, but so far it hasn&#8217;t affected me. So I am hopeful that for my purposes, the eval edition of Windows 2012 will satisfy my need.</li><li>Build the base VM with NAT networking. It is convienient for the VM to have access to the web directly. We will add additional netword card(s) when we clone this base image.<p>Once the base VM is built, copy that single vdi file somewhere else. Then remove/delete this base VM from your system all together;</li><li>Make the base vdi immutable by running this:<pre class="brush: text">
VBoxManage modifyhd pathTo/myBase.vdi --type immutable
</pre></li><li>Build a domain controller based on this image. When creating a VirtualBox hard disk, use the base vdi file. At this point, add in a second network adapter, which should be type &#8220;Internal Network&#8221;. Take the VirtualBox default value for the rest of the fields;</li><li>Finish building the VM, change the VM to a proper name, such as DCServer or whatever you feel is appropriate;</li><li>Shutdown the VM, then run this command against the vdi file in your new VM&#8217;s Snapshots folder:<pre class="brush: text">
VBoxManage modifyhd ~/Virtual Machines/myNewVM/Snapshots/{someName}.vdi --autoreset false
</pre><p>This step is necessary, otherwise VirtualBox resets the VM to the base image after every reboot;</li><li>Now install the appropriate software and set this VM as a domain controller. <a
href="https://social.technet.microsoft.com/wiki/contents/articles/12370.step-by-step-guide-for-setting-up-windows-server-2012-domain-controller.aspx">Follow instructions here</a>.</li><li>Configure the network card for the &#8220;Internal Network&#8221; by assigning a fixed IP address. If you&#8217;ve followed the instructions, the second network card should be called Ethernet 2. I didn&#8217;t bother with IPv6, and gave my VM the fixed IP address of 192.168.1.10. I left everything else to its default value. Note that after entering the IP address, the Subnet mask will be 255.255.255.0. I didn&#8217;t change that and didn&#8217;t touch the rest of the fields;</li><li>Turn off all Windows firewalls. In addition, open up &#8220;Group Policy Management&#8221;, expand to Domains->MyDomain->Default Domain Policy. Right click and choose &#8220;Edit&#8230;&#8221;. Group Policy Management Editor opens up. Under &#8220;Computer Configuration&#8221;, expand to Policies->Windows Settings->Security Settings->Account Policies. Change password policy accordingly.</li><li>Take a snapshot of the machine. Mark every disk file under the Snapshot folder so its autoreset is false.</li><li>Now build another VM. Add the second network adapter for &#8220;Internal Network&#8221;. When done, shut it down and change its vdi disk file&#8217;s autoreset property, described above;</li><li>Because this VM and the domain controller VM use the same base image, so by default they will have the same SID, which will prevent them to be in the same domain. Fix that by starting this new VM, then run sysprep. <a
href="http://oasysadmin.com/2012/02/27/generate-a-new-sid-on-windows-server-2008-and-windows-7/">Follow the instructions here</a>, if you need it.</li><li>Reboot the new VM again, configure its &#8220;Ethernet 2&#8243; before it can join the domain we created earlier. Here is my configuration. Once again I didn&#8217;t touch IPv6:<br
/> IP address: 192.168.1.11<br
/> Subnet mask: 255.255.255.0<br
/> Default gateway: 192.168.1.10<br
/> Preferred DNS server: 192.168.1.10</li><li>Rename the VM to an appropriate name, for example, sql1. Then join the domain we created earlier. Obviously the other domain controller VM needs to be up and you&#8217;ve run sysprep on this second VM. If you haven&#8217;t assigned a different SID for this new VM, you will receive error message like &#8220;the domain join cannot be completed because the SID of the domain you attempted to join was identical to the SID of this machine &#8230;&#8221;</li><li>Repeat the process of building other VMs to join the domain, if you have the need.</li></ol><p>Any comments/suggestions welcome. Good luck!</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2013/04/01/setting-up-windows-development-environment-with-virtualbox/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Install sqlps as a PowerShell module</title><link>http://www.haidongji.com/2013/02/15/install-sqlps-as-a-powershell-module/</link> <comments>http://www.haidongji.com/2013/02/15/install-sqlps-as-a-powershell-module/#comments</comments> <pubDate>Sat, 16 Feb 2013 03:53:22 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[PowerShell]]></category> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1437</guid> <description><![CDATA[Most of SQL Server automation scripts using PowerShell use SMO directly. For example, one would do something like: [System.Reflection.Assembly]::LoadWithPartialName(&#8220;Microsoft.SqlServer.SMO&#8221;) $MyServer = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Server&#8217;) &#8216;HOME&#8217; $MyDataBase = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Database&#8217;) ($Server, &#8220;MyDataBase&#8221;) That&#8217;s a lot of typing and looks messy and cumbersome. Since SQL Server 2008, Microsoft provides a sqlps shell which exposes a lot of SQL [...]]]></description> <content:encoded><![CDATA[<p>Most of SQL Server automation scripts using PowerShell use SMO directly. For example, one would do something like:</p><p>[System.Reflection.Assembly]::LoadWithPartialName(&#8220;Microsoft.SqlServer.SMO&#8221;)<br
/> $MyServer = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Server&#8217;) &#8216;HOME&#8217;<br
/> $MyDataBase = new-object (&#8216;Microsoft.SqlServer.Management.Smo.Database&#8217;)<br
/> ($Server, &#8220;MyDataBase&#8221;)</p><p>That&#8217;s a lot of typing and looks messy and cumbersome.</p><p>Since SQL Server 2008, Microsoft provides a sqlps shell which exposes a lot of SQL Server objects and functions for easy browsing and inspection, but one couldn&#8217;t import that into an existing scripts for easy automation. With the release of SQL Server 2012 Feature Pack, it is now possible to expose sqlps functions in your scripts by running:</p><p>Import-Module sqlps</p><p>Here is how to get sqlps installed so it is available to import as a module. I&#8217;ve tested this successfully on both Windows Server 2008 R2 and Windows Server 2012. To get some idea on how to use it, perhaps <a
href="https://github.com/haidong/mssqlps/blob/master/README.md">some of my scripts here</a> can get you started <img
src='http://www.haidongji.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p>1. Go to <a
href="https://www.microsoft.com/en-us/download/details.aspx?id=29065">SQL Server 2012 Feature Pack site</a></p><p>2. Download and install the 3 items below<br
/> Microsoft System CLR Types for Microsoft SQL Server 2012<br
/> Microsoft SQL Server 2012 Shared Management Objects<br
/> Microsoft Windows PowerShell Extensions for Microsoft SQL Server 2012</p><p>BeginDigression<br
/> Given a choice, I&#8217;d rather write automation programs on Windows with either Python and Perl, because both languages come with a comprehensive testing framework that supports TDD. With test cases and various testing harness, it is much easier to build solid, high quality software solutions.</p><p>However, if for whatever reason, it is difficult to use Python or Perl, then the natural choice on Windows is PowerShell, because it comes with the OS. Too bad PowerShell does not have a solid testing framework. <a
href="http://www.haidongji.com/2012/10/22/powershell-tdd-with-psunit-some-usage-examples/">I have been using PSUnit</a>, which leaves a lot to be desired for people who are used to xUnit framework, but it is still worthwhile to use.<br
/> EndDigression</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2013/02/15/install-sqlps-as-a-powershell-module/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Gathering SQL Server database free space and space usage by schema</title><link>http://www.haidongji.com/2012/11/12/gathering-sql-server-database-free-space-and-space-usage-by-schema/</link> <comments>http://www.haidongji.com/2012/11/12/gathering-sql-server-database-free-space-and-space-usage-by-schema/#comments</comments> <pubDate>Mon, 12 Nov 2012 23:08:45 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[SQLServer]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1433</guid> <description><![CDATA[I did analysis for SQL Server database free space and space usage by schema twice in the last few months, without any outside monitoring tools. I wrote this down here for future reference. A significant part of the code came from this article here by The MAK, with slight modification. And I&#8217;ve verified that it [...]]]></description> <content:encoded><![CDATA[<p>I did analysis for SQL Server database free space and space usage by schema twice in the last few months, without any outside monitoring tools. I wrote this down here for future reference. A significant part of the code came from <a
href="http://www.databasejournal.com/features/mssql/article.php/3733996/Find-space-Usage-by-Table--Schema-in-SQL-Server-2005-and-2008.htm">this article here by The MAK</a>, with slight modification. And I&#8217;ve verified that it gives the correct result.</p> <span
class="collapseomatic " id="id5604"  title="T-SQL to get row count and space usage by schemas">T-SQL to get row count and space usage by schemas</span><div
id="target-id5604" class="collapseomatic_content "></p><pre class="brush: sql">
--To get a particular schema, add &quot;a3.name = &#039;schemaName&#039;&quot;
SELECT
 --(row_number() over(order by a3.name, a2.name))%2 as l1,
 a3.name AS [schemaname],
 count(a2.name ) as NumberOftables,
 sum(a1.rows) as row_count,
 sum((a1.reserved + ISNULL(a4.reserved,0))* 8) AS reservedKB,
 sum(a1.data * 8) AS dataKB,
 sum((CASE WHEN (a1.used + ISNULL(a4.used,0)) &gt; a1.data THEN
   (a1.used + ISNULL(a4.used,0)) - a1.data ELSE 0 END) * 8 )AS index_sizeKB,
 sum((CASE WHEN (a1.reserved + ISNULL(a4.reserved,0)) &gt; a1.used THEN
   (a1.reserved + ISNULL(a4.reserved,0)) - a1.used ELSE 0 END) * 8) AS unusedKB
FROM
 (SELECT
  ps.object_id,
  SUM (
   CASE
    WHEN (ps.index_id &lt; 2) THEN row_count
    ELSE 0
   END
   ) AS [rows],
  SUM (ps.reserved_page_count) AS reserved,
  SUM (
   CASE
    WHEN (ps.index_id &lt; 2) THEN
     (ps.in_row_data_page_count + ps.lob_used_page_count + ps.row_overflow_used_page_count)
    ELSE (ps.lob_used_page_count + ps.row_overflow_used_page_count)
   END
   ) AS data,
  SUM (ps.used_page_count) AS used
 FROM sys.dm_db_partition_stats ps
 GROUP BY ps.object_id) AS a1
LEFT OUTER JOIN
 (SELECT
  it.parent_id,
  SUM(ps.reserved_page_count) AS reserved,
  SUM(ps.used_page_count) AS used
  FROM sys.dm_db_partition_stats ps
  INNER JOIN sys.internal_tables it ON (it.object_id = ps.object_id)
  WHERE it.internal_type IN (202,204)
  GROUP BY it.parent_id) AS a4 ON (a4.parent_id = a1.object_id)
INNER JOIN sys.all_objects a2  ON ( a1.object_id = a2.object_id )
INNER JOIN sys.schemas a3 ON (a2.schema_id = a3.schema_id)
WHERE a2.type &lt;&gt; &#039;S&#039; and a2.type &lt;&gt; &#039;IT&#039;
group by a3.name
ORDER BY a3.name
</pre><p></div> <span
class="collapseomatic " id="id710"  title="T-SQL to get database free space">T-SQL to get database free space</span><div
id="target-id710" class="collapseomatic_content "></p><pre class="brush: sql">
-- database free space by file
select
      name
    , filename
    , convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB
    , convert(decimal(12,2),round(fileproperty(a.name,&#039;SpaceUsed&#039;)/128.000,2)) as SpaceUsedMB
    , convert(decimal(12,2),round((a.size-fileproperty(a.name,&#039;SpaceUsed&#039;))/128.000,2)) as FreeSpaceMB
from dbo.sysfiles a
--sum of all database free space, excluding log file free space
select
    sum(convert(decimal(12,2),round((a.size-fileproperty(a.name,&#039;SpaceUsed&#039;))/128.000,2)))/1024 as FreeSpaceGB
from dbo.sysfiles a where groupid &gt; 0
</pre><p></div> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/11/12/gathering-sql-server-database-free-space-and-space-usage-by-schema/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>PASS 2012 impressions</title><link>http://www.haidongji.com/2012/11/10/pass-2012-impressions/</link> <comments>http://www.haidongji.com/2012/11/10/pass-2012-impressions/#comments</comments> <pubDate>Sun, 11 Nov 2012 03:31:37 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Other]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1428</guid> <description><![CDATA[This is my first PASS conference after a few years of absence. A few thoughts and impressions: 1. It was good catching up with a few friends. There are still a few old friends I missed, and a few that I really would have liked to talk longer. 2. Microsoft&#8217;s Hekaton announcement is interesting. A [...]]]></description> <content:encoded><![CDATA[<p>This is my first PASS conference after a few years of absence. A few thoughts and impressions:</p><p>1. It was good catching up with a few friends. There are still a few old friends I missed, and a few that I really would have liked to talk longer.</p><p>2. Microsoft&#8217;s Hekaton announcement is interesting. A few years back I saw VoltDB demos and that was impressive. Microsoft&#8217;s announcement will accelerate in-memory transactional database developement and help bring this technology to a wider audience.</p><p>3. Solid state disks see more adaption but they are still pretty expensive. Hopefully its wider adaption will help to drive the cost down as time goes on. There are a few vendors that uses some creative ways to squeeze the most out of the investment by pairing mechenical disks with solid state disks, such as algo-based data movement: when it detects some data on spindle disks are &#8220;hot&#8221;, it moves them to solid state disks. That sounds interesting if done well.</p><p>4. It was really nice meeting a number of Chinese-speaking attendees. Great networking and fun discussions!</p><p>5. The mojito and bloody mary from Edge Grille is not very good in my opinion. Beer from Gordon Biersch is ordinary, nothing to write home about, ymmv.</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/11/10/pass-2012-impressions/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>关于软件测试：持续集成测试</title><link>http://www.haidongji.com/2012/11/05/%e5%85%b3%e4%ba%8e%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%ef%bc%9a%e6%8c%81%e7%bb%ad%e9%9b%86%e6%88%90%e6%b5%8b%e8%af%95/</link> <comments>http://www.haidongji.com/2012/11/05/%e5%85%b3%e4%ba%8e%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%ef%bc%9a%e6%8c%81%e7%bb%ad%e9%9b%86%e6%88%90%e6%b5%8b%e8%af%95/#comments</comments> <pubDate>Mon, 05 Nov 2012 23:37:34 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Chinese]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1421</guid> <description><![CDATA[讲完了单元测试，我来分享下个人感觉另一个开发团队的重要测试步骤：持续集成测试，continuous integration testing。 稍具规模的软件开发，不管是商业或开源，一般是由一个团队来完成。团队写软件，必然会有一系列的分工。比方说，张三可能会写一些处理客户订单方面的library, class,和/或function；李四会写库存管理方面的代码；而王五负责物流和采购；等等。当然也不能排除张三发现李四代码内的问题进而修正的现象。我们在这里假定队里的工程师们已经统一使用一种单元测试机制并且会写单元测试。但最终我们要把这些代码通过代码库整合集中起来，确保作为一个整体，这些代码可以通过所有单元测试，没有冲突和编译错误，并且编译好的binary/executable符合事先订好的规范。这种源代码的整合、总体通过所有单元测试、代码链接和编译以及编译后的一些测试，即为集成测试，integration testing。 在团队开发环境里，各团队成员写的部件会有依赖性。如果我们数天、一周、甚至更长的时间里才做一次代码集成，就很可能会遇到以下情况：来自不同成员的代码部件可能有冲突，界面和生产环境的变化没能及时得到沟通和传达到每个队员，一部分的代码故障修复造成了其它部分的故障，等等等等。这会引进bugs，造成部分或全部的代码回滚，代码的不可编译，甚至拖延产品的发布或升级。为了解决这一常见问题，我们需要尽可能早、多次、自动化地运行集成测试。持续集成测试，即continuous integration testing，就应运而生。 自动化的持续集成测试由软件来完成。现在业界有不少continuous integration testing的工具，开源和商业的都有，像Buildbot（Python写成），Jenkins（Java写成），TeamCity，CruiseControl等。在本文底下我会给两个实战集成测试链接：一个是MariaDB的，用的是Buildbot；一个是Percona的，用的是Jenkins。读完本博后你可以到那两个网站看看，可以分析出他们的集成测试都做什么，有什么报告等，希望对你有所启发。集成测试机制一般有以下特点： 1. 和代码库挂钩。代码提交/check in/commit会自动触发集成测试； 2. 除了代码提交的自动激活测试外，一般可以安排时间定期来做集成测试，比如每天晚上一次； 3. 在编译和构建binary的同时或之后，该机制还可以和其它机制挂钩，如生成文档，给软件打包，做其它测试甚至生产发布，等等； 4. 有网页界面来回报集成测试结果。这结果包括集成测试状态，该集成包括哪些代码变化、bug fixes，新性能等，代码覆盖测试/code coverage报告等等。 一般情况下，如果队伍和产品有一定规模，可以考虑设置专门的build server来完成以上步骤。持续集成测试会给我们带来以下优势： 1. 尽早发现不同队员代码之间的冲突，减少甚至铲除难缠的由于不能尽快整合而对全局有负影响的问题； 2. 促进团队内部沟通； 3. 在紧急情况下，如果需要部署最新软件来解决生产环境中的燃眉之急，你有了可以直接部署的binary。 我个人以为建立持续集成测试机制对一个团队很重要。从我的理解和体会，我觉得以下几点值得注意： 1. 如上文所述，团队用统一的单元测试架构很重要。有了这个架构，工程师要写单元测试； 2. 养成多次从代码库拉下更新自己代码的习惯，每个工作日至少一次。一天可以多次提交代码； 3. 在代码提交之前，完整运行一次所有单元测试，包括组内其他成员的单元测试。测试不通过，不提交自己的代码。更何况，如果你的代码没通过单元测试而被提交，那集成测试机制会发现这个问题并在网页上公布出来，你会觉得很没面子。 好，这次就写到这儿。下次继续，可能会写下接收测试acceptance testing，stress/performan testing之类吧。如上文所述，这里是MariaDB的持续集成测试网站链接（Buildbot）；这里是Percona的持续集成测试网站链接（Jenkins）。]]></description> <content:encoded><![CDATA[<p>讲完了<a
href="http://www.haidongji.com/2012/10/21/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95/">单元测试</a>，我来分享下个人感觉另一个开发团队的重要测试步骤：持续集成测试，continuous integration testing。</p><p>稍具规模的软件开发，不管是商业或开源，一般是由一个团队来完成。团队写软件，必然会有一系列的分工。比方说，张三可能会写一些处理客户订单方面的library, class,和/或function；李四会写库存管理方面的代码；而王五负责物流和采购；等等。当然也不能排除张三发现李四代码内的问题进而修正的现象。我们在这里假定队里的工程师们已经统一使用一种单元测试机制并且会写单元测试。但最终我们要把这些代码通过代码库整合集中起来，确保作为一个整体，这些代码可以通过所有单元测试，没有冲突和编译错误，并且编译好的binary/executable符合事先订好的规范。这种源代码的整合、总体通过所有单元测试、代码链接和编译以及编译后的一些测试，即为集成测试，integration testing。</p><p>在团队开发环境里，各团队成员写的部件会有依赖性。如果我们数天、一周、甚至更长的时间里才做一次代码集成，就很可能会遇到以下情况：来自不同成员的代码部件可能有冲突，界面和生产环境的变化没能及时得到沟通和传达到每个队员，一部分的代码故障修复造成了其它部分的故障，等等等等。这会引进bugs，造成部分或全部的代码回滚，代码的不可编译，甚至拖延产品的发布或升级。为了解决这一常见问题，我们需要尽可能早、多次、自动化地运行集成测试。持续集成测试，即continuous integration testing，就应运而生。</p><p>自动化的持续集成测试由软件来完成。现在业界有不少continuous integration testing的工具，开源和商业的都有，像Buildbot（Python写成），Jenkins（Java写成），TeamCity，CruiseControl等。在本文底下我会给两个实战集成测试链接：一个是MariaDB的，用的是Buildbot；一个是Percona的，用的是Jenkins。读完本博后你可以到那两个网站看看，可以分析出他们的集成测试都做什么，有什么报告等，希望对你有所启发。集成测试机制一般有以下特点：</p><p>1. 和代码库挂钩。代码提交/check in/commit会自动触发集成测试；<br
/> 2. 除了代码提交的自动激活测试外，一般可以安排时间定期来做集成测试，比如每天晚上一次；<br
/> 3. 在编译和构建binary的同时或之后，该机制还可以和其它机制挂钩，如生成文档，给软件打包，做其它测试甚至生产发布，等等；<br
/> 4. 有网页界面来回报集成测试结果。这结果包括集成测试状态，该集成包括哪些代码变化、bug fixes，新性能等，代码覆盖测试/code coverage报告等等。</p><p>一般情况下，如果队伍和产品有一定规模，可以考虑设置专门的build server来完成以上步骤。持续集成测试会给我们带来以下优势：<br
/> 1. 尽早发现不同队员代码之间的冲突，减少甚至铲除难缠的由于不能尽快整合而对全局有负影响的问题；<br
/> 2. 促进团队内部沟通；<br
/> 3. 在紧急情况下，如果需要部署最新软件来解决生产环境中的燃眉之急，你有了可以直接部署的binary。</p><p>我个人以为建立持续集成测试机制对一个团队很重要。从我的理解和体会，我觉得以下几点值得注意：<br
/> 1. 如上文所述，团队用统一的单元测试架构很重要。有了这个架构，工程师要写单元测试；<br
/> 2. 养成多次从代码库拉下更新自己代码的习惯，每个工作日至少一次。一天可以多次提交代码；<br
/> 3. 在代码提交之前，完整运行一次所有单元测试，包括组内其他成员的单元测试。测试不通过，不提交自己的代码。更何况，如果你的代码没通过单元测试而被提交，那集成测试机制会发现这个问题并在网页上公布出来，你会觉得很没面子。</p><p>好，这次就写到这儿。下次继续，可能会写下接收测试acceptance testing，stress/performan testing之类吧。如上文所述，这里是<a
href="http://buildbot.askmonty.org/buildbot/grid">MariaDB的持续集成测试网站</a>链接（Buildbot）；这里是<a
href="http://jenkins.percona.com/">Percona的持续集成测试网站</a>链接（Jenkins）。</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/11/05/%e5%85%b3%e4%ba%8e%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95%ef%bc%9a%e6%8c%81%e7%bb%ad%e9%9b%86%e6%88%90%e6%b5%8b%e8%af%95/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Cliffs Notes: administrating Active Directory with PowerShell</title><link>http://www.haidongji.com/2012/10/24/cliffs-notes-administrating-active-directory-with-powershell/</link> <comments>http://www.haidongji.com/2012/10/24/cliffs-notes-administrating-active-directory-with-powershell/#comments</comments> <pubDate>Thu, 25 Oct 2012 04:44:48 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[PowerShell]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1414</guid> <description><![CDATA[1. Install ActiveDirectory modules by running PowerShell as Administrator and executing the commands below: PS C:\Windows\system32&#62; Import-Module ServerManager PS C:\Windows\system32&#62; Add-WindowsFeature RSAT-AD-PowerShell Success Restart Needed Exit Code Feature Result ------- -------------- --------- -------------- True No Success {Active Directory module for Windows Power... 2. Link for newly added Active Directory cmdlets after installation, pretty useful; 3. [...]]]></description> <content:encoded><![CDATA[<p>1. Install ActiveDirectory modules by running PowerShell as Administrator and executing the commands below:</p><pre class="brush: text">
PS C:\Windows\system32&gt; Import-Module ServerManager
PS C:\Windows\system32&gt; Add-WindowsFeature RSAT-AD-PowerShell
Success Restart Needed Exit Code Feature Result
------- -------------- --------- --------------
True    No             Success   {Active Directory module for Windows Power...
</pre><p>2. <a
href="http://technet.microsoft.com/en-us/library/ee617195.aspx">Link for newly added Active Directory cmdlets after installation</a>, pretty useful;</p><p>3. There are two types of Active Directory groups: distribution and security. The former is for email distribution and the later is what most people had in mind: groups used for security and access management. So if you are not sure, use security group;</p><p>4. LDAP search string and DN (Distinguished Name) follow the convention of putting contained item in the front. Scope increases from left to right, in other words. For example, &#8220;CN=eastRegion,OU=Benefits,OU=HR,DC=research,DC=hardware,DC=acme,DC=com&#8221; can be a security/permission group called eastRegion that belongs to OU (OrganizationUnit) Benefits, which belongs to OU HR, under domain of research.hardware.acme.com. DC stands for Domain Component, by the way.</p><p>5. <a
href="http://technet.microsoft.com/en-us/library/hh531527(v=ws.10).aspx">This site has info on filtering of Active Directory</a>. Very useful. Below is an example of getting all security groups. You may need to run Import-Module ActiveDirectory first:<br
/> $a = Get-ADGroup -SearchBase &#8220;OU=Benefits,OU=HR,DC=research,DC=hardware,DC=acme,DC=com&#8221; -filter &#8216;groupcategory -eq &#8220;security&#8221;&#8216;</p><p>6. Oh, dsa.msc, if you have installed, is a useful GUI tool. Good for verification and quick glances.</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/10/24/cliffs-notes-administrating-active-directory-with-powershell/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>PowerShell TDD with PSUnit: some usage examples</title><link>http://www.haidongji.com/2012/10/22/powershell-tdd-with-psunit-some-usage-examples/</link> <comments>http://www.haidongji.com/2012/10/22/powershell-tdd-with-psunit-some-usage-examples/#comments</comments> <pubDate>Mon, 22 Oct 2012 21:02:12 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[PowerShell]]></category> <category><![CDATA[Technology]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1407</guid> <description><![CDATA[I discussed setting up PSUnit for unit testing PowerShell before. This is a quick followup for my own record and consumption in the future. I will update this post as I find more interesting things to record. 1. Put PowerShell functions in one file such as myBaseFunctions.ps1; 2. Create a test directory and under that [...]]]></description> <content:encoded><![CDATA[<p>I discussed <a
href="http://www.haidongji.com/2012/05/08/powershell-tdd-with-psunit/">setting up PSUnit for unit testing PowerShell</a> before. This is a quick followup for my own record and consumption in the future. I will update this post as I find more interesting things to record.</p><p>1. Put PowerShell functions in one file such as myBaseFunctions.ps1;<br
/> 2. Create a test directory and under that directory, create a test case file called myBaseFunctions.Test.ps1;<br
/> 3. Here is what the first two lines of the test case file:</p><pre class="brush: text">
. PSUnit.ps1
. &quot;c:\properPath\myBaseFunctions.ps1&quot;
</pre><p>4. A sample test case:</p><pre class="brush: text">
function Test.getFileName()
{
#Arrange
    	$expectedResult = &quot;X:\somePath\someName.sql&quot;
#Act
	$actualResult = functionBeingTested -Para1 &quot;someValue&quot; -Para2 &quot;someValue&quot;
#Assert
	Assert-That -ActualValue $actualResult -Constraint {$actualResult -eq $expectedResult}
}
</pre><p>5. Running tests on command line:<br
/> PS C:\myDirectory> PSUnit.Run.ps1 -PSUnitTestFile properPath\myBaseFunctions.Test.ps1</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/10/22/powershell-tdd-with-psunit-some-usage-examples/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>关于软件测试：单元测试</title><link>http://www.haidongji.com/2012/10/21/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95/</link> <comments>http://www.haidongji.com/2012/10/21/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95/#comments</comments> <pubDate>Mon, 22 Oct 2012 04:38:53 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Chinese]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1399</guid> <description><![CDATA[上博我写了测试的重要性。现在我来谈谈软件开发最基本的测试：单元测试，unit testing。 单元测试主要用来检测某class，function，method的正确性。我想绝大多数工程师在编程时都会做各种各样的单元测试。但直到约上世纪末和本世纪初，系统性、自动化的单元测试机制才被真正地建立起来注,见下。说到这里，我们必须提到Kent Beck和Erich Gamma。 Kent Beck在90年代末用Smalltalk编程，在那段时期他写出SUnit，是用Smalltalk写出的给Smalltalk程序做单元测试的架构。在此基础之上，他和Erich Gamma携手创建了JUnit。JUnit是用Java写成的用来做Java单元测试的架构。从JUnit开始，统称为xUnit的测试架构以燎原之势传入到其它语言：Python（unittest），PHP（PHPUnit），NUnit（.Net语言如C#，VB.net）等。 xUnit的架构和基本思路实际上很简单：Arrange-Act-Assert： 1.把输入值和期望值安排好，必要时可以装逼（mock）。mock是当你的单元测试需要大、笨重、或复杂的外部资源支持时（如数据库，其它复杂API等等），你可以用个假模子mock来代替。这一步是Arrange，设定期望值； 2.调动要被测试的function/method/class，得到实际值，这是Act； 3.比较期望值和实际值，报出测试结果，这是Assert. 或许你会说，这有啥大不了的，我编程时所做的随机测试就是这么做的吗。没错，但像xUnit这样的测试机制提供了自动化、可重复性和方便性等特点。 1.自动化 良好的xUnit测试机制有所谓的test harness，即各种脚手架、工具等配套的捆绑机制。在按xUnit规范写出测试案例后，可以很方便地用该捆绑机制轻松调用全部或部分测试案例，不用笨手笨脚地做手动调节； 2.即时性 和上文紧密相关的是xUnit带来的即时性。在写出一个测试案例后，你可以很方便得用copy/paste/adjust来写出更多其它测试案例。因为有自动化机制，运行这些案例会很方便快捷，成功与否，结果立现。这样你就可以在思路、信息还停留在脑海之时，趁热打铁，迅速、有效地解决这个问题。顺便说一句，英语中也有这一俚语：strike while the iron is hot。不知该成语是否是翻译、借用来（中英或英中），我感觉自发生成的可能性大些。 单元测试是你的安全网。随着你单元测试的增多，你的安全网就变得越来越密集，漏网之鱼就会越来越少。测试给你的软件质量提供了足够的保障。 如果说我们对单元测试的重要性和自动化有了共识，我有以下想法和你共勉，让我们一起来学习和提高。弄好测试，如俗谚所说，磨刀不费砍柴工，会大大提高你的功力！ 1.搜索出并安装适合你编程语言的xUnit测试框架。比方说你写PHP，那你可以用PHPUnit。如你用Python，如果是2.7以后的话，你可以用标准的unittest模块来做测试：注意要安装nose来做这个测试的harness，来方便地调用你的测试案例。像其它语言，大多数都会有xUnit，搜搜就可以找到； 2.花时间学习如何写单元测试案例和如何有效地调用单元测试。相关的tutorial，how to等应当不少； 3.在做新开发时，要给新function/method/class写单元测试。当有时间或需要修改已存在代码时，如那段代码没单元测试，逐步引进单元测试； 4.单元测试也是代码的一部分，当然要进入代码管理库； 5.用copy和paste，写出各种满足边缘参数、一般参数等各种情况下的测试案例。硬盘便宜，多存几个案例占不了多大空，注意好的文档和命名方式对案例的管理很有帮助； 6.如果在整合测试/integration test或质量检测/quality assurance甚至是生产环境中发现你的function/method/class出现问题时，请首先写出个单元测试案例来捕捉这条漏网之鱼，然后修改代码来让测试通过。这样你的安全网就会被织得更紧一些。 在完成这个修改之后，你要反思一下为什么一开始你没想到要写这个测试？为什么没有想到鱼会有这种形状和技巧来挣脱你已经编织的网？通过这种反思，你会慢慢增加你的编程洞察力、嗅觉、和敏锐。 好，先写到这里。下一篇继续。我可能会写integration testing和continuous integration testing。或者专门写一下单元测试的应用实例如Python。如有问题或评论，欢迎提出，我们一起切磋，共同前进。 注：Perl的TAP（Test Anything Protocol）值得专门提一下。早在1987年，即随Perl第一版发布，就有了自己的单元测试机制。这比SUnit（xUnit之前身）要早10年左右。之后大部分在CPAN上公布的模块都经过了Test：：More的符合TAP规格的单元测试。我个人以为这是Perl作为元老级脚本语言，至今仍威风八面、历久弥新的主要原因之一。所以如果你用Perl编程，我强烈建议你掌握Test::More和相关的Test::Harness模块。]]></description> <content:encoded><![CDATA[<p><a
href="http://www.haidongji.com/2012/10/08/关于软件测试：测试的重要性/">上博我写了测试的重要性</a>。现在我来谈谈软件开发最基本的测试：单元测试，unit testing。</p><p>单元测试主要用来检测某class，function，method的正确性。我想绝大多数工程师在编程时都会做各种各样的单元测试。但直到约上世纪末和本世纪初，系统性、自动化的单元测试机制才被真正地建立起来<sup>注,见下</sup>。说到这里，我们必须提到Kent Beck和Erich Gamma。</p><p>Kent Beck在90年代末用Smalltalk编程，在那段时期他写出SUnit，是用Smalltalk写出的给Smalltalk程序做单元测试的架构。在此基础之上，他和Erich Gamma携手创建了JUnit。JUnit是用Java写成的用来做Java单元测试的架构。从JUnit开始，统称为xUnit的测试架构以燎原之势传入到其它语言：Python（unittest），PHP（PHPUnit），NUnit（.Net语言如C#，VB.net）等。</p><p>xUnit的架构和基本思路实际上很简单：Arrange-Act-Assert：<br
/> 1.把输入值和期望值安排好，必要时可以装逼（mock）。mock是当你的单元测试需要大、笨重、或复杂的外部资源支持时（如数据库，其它复杂API等等），你可以用个假模子mock来代替。这一步是Arrange，设定期望值；<br
/> 2.调动要被测试的function/method/class，得到实际值，这是Act；<br
/> 3.比较期望值和实际值，报出测试结果，这是Assert.</p><p>或许你会说，这有啥大不了的，我编程时所做的随机测试就是这么做的吗。没错，但像xUnit这样的测试机制提供了自动化、可重复性和方便性等特点。</p><p>1.自动化<br
/> 良好的xUnit测试机制有所谓的test harness，即各种脚手架、工具等配套的捆绑机制。在按xUnit规范写出测试案例后，可以很方便地用该捆绑机制轻松调用全部或部分测试案例，不用笨手笨脚地做手动调节；</p><p>2.即时性<br
/> 和上文紧密相关的是xUnit带来的即时性。在写出一个测试案例后，你可以很方便得用copy/paste/adjust来写出更多其它测试案例。因为有自动化机制，运行这些案例会很方便快捷，成功与否，结果立现。这样你就可以在思路、信息还停留在脑海之时，趁热打铁，迅速、有效地解决这个问题。顺便说一句，英语中也有这一俚语：strike while the iron is hot。不知该成语是否是翻译、借用来（中英或英中），我感觉自发生成的可能性大些。</p><p>单元测试是你的安全网。随着你单元测试的增多，你的安全网就变得越来越密集，漏网之鱼就会越来越少。测试给你的软件质量提供了足够的保障。</p><p>如果说我们对单元测试的重要性和自动化有了共识，我有以下想法和你共勉，让我们一起来学习和提高。弄好测试，如俗谚所说，磨刀不费砍柴工，会大大提高你的功力！</p><p>1.搜索出并安装适合你编程语言的xUnit测试框架。比方说你写PHP，那你可以用PHPUnit。如你用Python，如果是2.7以后的话，你可以用标准的unittest模块来做测试：注意要安装nose来做这个测试的harness，来方便地调用你的测试案例。像其它语言，<a
href="https://en.wikipedia.org/wiki/List_of_unit_testing_frameworks">大多数都会有xUnit</a>，搜搜就可以找到；</p><p>2.花时间学习如何写单元测试案例和如何有效地调用单元测试。相关的tutorial，how to等应当不少；</p><p>3.在做新开发时，要给新function/method/class写单元测试。当有时间或需要修改已存在代码时，如那段代码没单元测试，逐步引进单元测试；</p><p>4.单元测试也是代码的一部分，当然要进入代码管理库；</p><p>5.用copy和paste，写出各种满足边缘参数、一般参数等各种情况下的测试案例。硬盘便宜，多存几个案例占不了多大空，注意好的文档和命名方式对案例的管理很有帮助；</p><p>6.如果在整合测试/integration test或质量检测/quality assurance甚至是生产环境中发现你的function/method/class出现问题时，请首先写出个单元测试案例来捕捉这条漏网之鱼，然后修改代码来让测试通过。这样你的安全网就会被织得更紧一些。<br
/> 在完成这个修改之后，你要反思一下为什么一开始你没想到要写这个测试？为什么没有想到鱼会有这种形状和技巧来挣脱你已经编织的网？通过这种反思，你会慢慢增加你的编程洞察力、嗅觉、和敏锐。</p><p>好，先写到这里。下一篇继续。我可能会写integration testing和continuous integration testing。或者专门写一下单元测试的应用实例如Python。如有问题或评论，欢迎提出，我们一起切磋，共同前进。</p><p>注：Perl的TAP（Test Anything Protocol）值得专门提一下。早在1987年，即随Perl第一版发布，就有了自己的单元测试机制。这比SUnit（xUnit之前身）要早10年左右。之后大部分在CPAN上公布的模块都经过了Test：：More的符合TAP规格的单元测试。我个人以为这是Perl作为元老级脚本语言，至今仍威风八面、历久弥新的主要原因之一。所以如果你用Perl编程，我强烈建议你掌握Test::More和相关的Test::Harness模块。</p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/10/21/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>关于软件测试：测试的重要性</title><link>http://www.haidongji.com/2012/10/08/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e6%b5%8b%e8%af%95%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7/</link> <comments>http://www.haidongji.com/2012/10/08/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e6%b5%8b%e8%af%95%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7/#comments</comments> <pubDate>Tue, 09 Oct 2012 03:48:07 +0000</pubDate> <dc:creator>Haidong Ji</dc:creator> <category><![CDATA[Chinese]]></category> <category><![CDATA[EastWest]]></category> <category><![CDATA[Technology]]></category> <guid
isPermaLink="false">http://www.haidongji.com/?p=1386</guid> <description><![CDATA[题记：去年12月在淘宝和奥莱利公司举办的Velocity会议上，结交了很多好朋友。当时有好多想分享的主意，回来后写了个“开源软件的参与及社区互动的一些体会和建议”系列。之后就酝酿着要写一个测试系列，因为我坚信好的测试是生产高质量软件的必要条件。后来因为有家人从各地来，再加上时间安排不当（主要是懒和自律性差）和其它方面的原因，现在才动手写这个系列。这是开头篇。 通过自己的实战经验和阅读别人经验体会，我感觉我们可以总结出在生产高质量和高可靠的软硬件产品过程中，大都会走如下流程： 尽快制出原型产品（prototype）； 尽快把原型产品拿到生产环境中做检测和获得反馈； 尽快把在生产中得到的反馈数据和教训融入到下一步的故障修复和性能提升； 把改进后的产品再次尽快拿到生产环境中做检测和反馈； 循环以上步骤。 在这样的流程里，有以下几个方面特别值得注意： 1. 尽快做出原型产品并拿到实战中（或近可能类似于生产环境中）检验。 和用户互动，了解实战应用场景，检索、阅读、和了解业界同仁的文章和经验积累，搜索、阅读、分析网上网下的关于各种语言、解决方案的文档等都是很重要的过程。但把想法和了解到的信息付之与实践之前，那些理论、分析、和研究到头来只是纸上谈兵。把东西付诸与实践后，你会加深和巩固自己对已有知识的理解，碰到和解决自己和别人没有想象或碰到的问题，把学到的理论和步骤加以调整后运用到自己的环境中去。这是Kent Beck在极限编程/eXtreme Programming/XP中的一大重要、也是在实战中极端有效的一个原则。这一点和陆游在1199年写的《冬夜读书示子聿》里说得是同一个道理： 古人学问无遗力，少壮工夫老始成。 纸上得来终觉浅，绝知此事要躬行。 2. 快速的检测和反馈需要可靠、方便、和尽可能自动化的测试机制。 原型产品出来后肯定会有这样或那样的问题：有的地方需要改进和提高，有的地方需要添加新功能等等。我们在做改进和提高的同时还要做到已有功能不退化（regression bugs）。要能够实现这一目标，唯一有效的办法是建立起有效、系统、和自动化的测试脚手架。有了这种测试机制，并配有有效的源代码管理系统，我们就敢于去做代码改进和重构，就有足够的信心来保证代码的质量和可靠性。 前一段时间读Kent Beck的TDD（Test-Driven Development）书，他举了这样一个例子，我觉得挺贴切的。我们都知道用轱辘打水比从井里直接往上提省力，是打水的有效方法。假设水提到一半后你累啦或有别的情况而hold不住，你的努力就会前功尽弃。但如果你在轱辘上安装个齿轮机制来咬住你的进程而不至于让提上来的绳子后退，那你就会轻松、自信地打很多水。有效、自动化的测试机制就是你用轱辘打水的那个齿轮，它给你信心和回旋余地，让你大胆、放心地去修复、改进、重构、和添加新功能，让你有信心和实效来生产和提高产品，在保留既有功效的前提下扩大战果，打一场干脆利索、无后顾之忧（或担忧最小化）、不拖泥带水的漂亮仗！ 在编程中，如果我们把每一个类/class、每一个函数/function都当作一个产品的话，根据上述“尽快做出原型产品并拿到实战中检验”的法则，我们就可以很容易地理解TDD的原理： a.我们对某类或某函数的界面有初步打算，即脑中已经设计出原型； b.在实施该类或函数之前，我们写出一个单元测试案例来在实战中应用该函数/类的原型； c.我们运行该单元测试，那么这第一个测试应当失败； d.好，接下来我们用代码来实施该函数或类的原型，直到该单元测试通过； e.我们接着写更多的测试来强化该函数/类的代码，直至新的单元测试通过； f.循环以上步骤，来搭建其它函数/类。 按照这种方式来写代码，这些单元测试就构建成咬定既有成果的齿轮和脚手架。它们给我们提供质量保证和信心，给整个产品的稳定、可靠、和高质量打下了坚实的基础。 先写到这儿，该系列的下一篇，我写一下单元测试。 后记：这些生产原型产品、实战测试和反馈、之后提高的良性循环经验过程，不仅仅可以应用到软件开发方面，个人以为在硬件、工业制造、能源和环保产品的设计和研发等很多方面也能派上用场。比方说关于节能型汽车的研发、改进和提高，关于高铁、大飞机各部件的研发和应用，关于能源产品如藻类/海藻产生能源和风能、地热能、太阳能方面的可行性研究和应用，关于很多产业向创新、高附加值型转型等等，这些对于中国和很多后发国家都有很现实的指导意义。有的东西你可以在网上、书里、和各种会议上学，有的东西别人会藏着、掖着不想让你学到，但你若不自己去应用和尝试，那永远都不会成为你自己的东西。不要自高自大，也不要妄自菲薄，一步步建立起完善的研发、测试、反馈、和提高机制，有勇气和实际行动去大胆尝试，踏踏实实地走稳每一步，那么最后领先的会是你。]]></description> <content:encoded><![CDATA[<p><em>题记：<a
href="http://www.haidongji.com/2011/12/18/velocity-2011中国行随记/">去年12月在淘宝和奥莱利公司举办的Velocity会议</a>上，结交了很多好朋友。当时有好多想分享的主意，回来后写了个<a
href="http://www.haidongji.com/2012/03/11/开源软件的参与及社区互动的体会和建议：开门篇/">“开源软件的参与及社区互动的一些体会和建议”系列</a>。之后就酝酿着要写一个测试系列，因为我坚信好的测试是生产高质量软件的必要条件。后来因为有家人从各地来，再加上时间安排不当（主要是懒和自律性差）和其它方面的原因，现在才动手写这个系列。这是开头篇。<br
/> </em><br
/> 通过自己的实战经验和阅读别人经验体会，我感觉我们可以总结出在生产高质量和高可靠的软硬件产品过程中，大都会走如下流程：</p><ol><li>尽快制出原型产品（prototype）；</li><li>尽快把原型产品拿到生产环境中做检测和获得反馈；</li><li>尽快把在生产中得到的反馈数据和教训融入到下一步的故障修复和性能提升；</li><li>把改进后的产品再次尽快拿到生产环境中做检测和反馈；</li><li>循环以上步骤。</li></ol><p>在这样的流程里，有以下几个方面特别值得注意：</p><p>1. 尽快做出原型产品并拿到实战中（或近可能类似于生产环境中）检验。<br
/> 和用户互动，了解实战应用场景，检索、阅读、和了解业界同仁的文章和经验积累，搜索、阅读、分析网上网下的关于各种语言、解决方案的文档等都是很重要的过程。但把想法和了解到的信息付之与实践之前，那些理论、分析、和研究到头来只是纸上谈兵。把东西付诸与实践后，你会加深和巩固自己对已有知识的理解，碰到和解决自己和别人没有想象或碰到的问题，把学到的理论和步骤加以调整后运用到自己的环境中去。这是Kent Beck在极限编程/eXtreme Programming/XP中的一大重要、也是在实战中极端有效的一个原则。这一点和陆游在1199年写的《冬夜读书示子聿》里说得是同一个道理：</p><p>古人学问无遗力，少壮工夫老始成。<br
/> <strong>纸上得来终觉浅，绝知此事要躬行。</strong></p><p>2. 快速的检测和反馈需要可靠、方便、和尽可能自动化的测试机制。<br
/> 原型产品出来后肯定会有这样或那样的问题：有的地方需要改进和提高，有的地方需要添加新功能等等。我们在做改进和提高的同时还要做到已有功能不退化（regression bugs）。要能够实现这一目标，唯一有效的办法是建立起有效、系统、和自动化的测试脚手架。有了这种测试机制，并配有<a
href="http://www.haidongji.com/2012/03/23/%e5%bc%80%e6%ba%90%e8%bd%af%e4%bb%b6%e7%9a%84%e5%8f%82%e4%b8%8e%e5%8f%8a%e7%a4%be%e5%8c%ba%e4%ba%92%e5%8a%a8%e7%9a%84%e4%b8%80%e4%ba%9b%e4%bd%93%e4%bc%9a%e5%92%8c%e5%bb%ba%e8%ae%ae%ef%bc%9a%e7%86%9f/">有效的源代码管理系统</a>，我们就敢于去做代码改进和重构，就有足够的信心来保证代码的质量和可靠性。</p><p>前一段时间读Kent Beck的TDD（Test-Driven Development）书，他举了这样一个例子，我觉得挺贴切的。我们都知道用轱辘打水比从井里直接往上提省力，是打水的有效方法。假设水提到一半后你累啦或有别的情况而hold不住，你的努力就会前功尽弃。但如果你在轱辘上安装个齿轮机制来咬住你的进程而不至于让提上来的绳子后退，那你就会轻松、自信地打很多水。有效、自动化的测试机制就是你用轱辘打水的那个齿轮，它给你信心和回旋余地，让你大胆、放心地去修复、改进、重构、和添加新功能，让你有信心和实效来生产和提高产品，在保留既有功效的前提下扩大战果，打一场干脆利索、无后顾之忧（或担忧最小化）、不拖泥带水的漂亮仗！</p><p>在编程中，如果我们把每一个类/class、每一个函数/function都当作一个产品的话，根据上述“尽快做出原型产品并拿到实战中检验”的法则，我们就可以很容易地理解TDD的原理：<br
/> a.我们对某类或某函数的界面有初步打算，即脑中已经设计出原型；<br
/> b.在实施该类或函数之前，我们写出一个单元测试案例来在实战中应用该函数/类的原型；<br
/> c.我们运行该单元测试，那么这第一个测试应当失败；<br
/> d.好，接下来我们用代码来实施该函数或类的原型，直到该单元测试通过；<br
/> e.我们接着写更多的测试来强化该函数/类的代码，直至新的单元测试通过；<br
/> f.循环以上步骤，来搭建其它函数/类。</p><p>按照这种方式来写代码，这些单元测试就构建成咬定既有成果的齿轮和脚手架。它们给我们提供质量保证和信心，给整个产品的稳定、可靠、和高质量打下了坚实的基础。</p><p>先写到这儿，该系列的下一篇，我写一下<a
href="http://www.haidongji.com/2012/10/21/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95/">单元测试</a>。</p><p><em>后记：这些生产原型产品、实战测试和反馈、之后提高的良性循环经验过程，不仅仅可以应用到软件开发方面，个人以为在硬件、工业制造、能源和环保产品的设计和研发等很多方面也能派上用场。比方说关于节能型汽车的研发、改进和提高，关于高铁、大飞机各部件的研发和应用，关于能源产品如藻类/海藻产生能源和风能、地热能、太阳能方面的可行性研究和应用，关于很多产业向创新、高附加值型转型等等，这些对于中国和很多后发国家都有很现实的指导意义。有的东西你可以在网上、书里、和各种会议上学，有的东西别人会藏着、掖着不想让你学到，但你若不自己去应用和尝试，那永远都不会成为你自己的东西。不要自高自大，也不要妄自菲薄，一步步建立起完善的研发、测试、反馈、和提高机制，有勇气和实际行动去大胆尝试，踏踏实实地走稳每一步，那么最后领先的会是你。</em></p> ]]></content:encoded> <wfw:commentRss>http://www.haidongji.com/2012/10/08/%e5%85%b3%e4%ba%8e%e8%bd%af%e4%bb%b6%e6%b5%8b%e8%af%95%ef%bc%9a%e6%b5%8b%e8%af%95%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>