Archive for the ‘computer science’ Category

Timer for Performance Testing

Wednesday, May 13th, 2009

呢排重溫緊 Pragmatic Unit Testing in C# with Nunit, 開始試緊寫有關 Performance 既 Test Case, 書中有一段有關 Performance 既 Code:

[Test]
public void FilterRanges()
{
    Timer timer = new Timer();
    String naughty_url = "http://www.xxxxxxxxxxx.com";
    // First, check a bad URL against a small list
    URLFilter filter = new URLFilter(small_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 1.0);
    // Next, check a bad URL against a big list
    filter = new URLFilter(big_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 2.0);
    // Finally, check a bad URL against a huge list
    filter = new URLFilter(huge_list);
    timer.Start();
    filter.Check(naughty_url);
    timer.End();
    Assert.IsTrue(timer.ElapsedTime < 3.0);
}

段 code 係一個幾好既 example 去講點 Test Performance, 但當真係要試果陣, 就發現左樣野, 就係我搵唔到 Code 中既 Timer Class, 係我在 System 入面既幾個 namespace 中, 都搵唔到啱用既 Timer, 因為 System namespace 入面既 Timer 大部份都係用黎 Trigger Timeout Event, 而當中既 Stop method 都只係用黎停止 Event Trigger, 而沒有任何計時結果做到出黎, 所以經過一輪網上既搜尋之後, 得出以下 Timer, 主要目的係用黎計時, 仔細度高, 最啱用黎作 Performance Testing 之用!!~

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestUtil
{
    public class HighResolutionTimer
    {
        private long frequency;
        private long start;
        private long stop;

        public HighResolutionTimer()
        {
            QueryPerformanceFrequency(ref frequency);
        }

        public float ElapsedTime
        {
            get
            {
                float elapsed = (((float)(stop - start)) / ((float)frequency));
                return elapsed;
            }
        }

        public void Start()
        {
            QueryPerformanceCounter(ref start);
        }

        public void Stop()
        {
            QueryPerformanceCounter(ref stop);
        }

        [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceCounter([In, Out] ref long performanceCount);

        [System.Runtime.InteropServices.DllImport("KERNEL32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool QueryPerformanceFrequency([In, Out] ref long frequency);

    }
}

HighResolutionTimer 來源 : 在 C# 中實現高性能計時

Acceptance Test Driven Development

Thursday, April 30th, 2009

Last night, I went to join the meeting of Agile Hong Kong, the topic is Acceptance Test-Driven Development(ATDD), presented by Steven Mark, which look-like great, user friendly, and example-driven concept to improve the software quality and meet the customer expectation, the different between TDD and ATDD is that the TDD is more focus on unit testing, test case written by developer and ATDD is focus on the customer example, test case written by user in readable format, one of practice is customer/business analyst provide html “table” of example using wiki and let the FIT or Robot framework read the html “table” and provide test result.

ATDD is great, but currently I still need a time to pickup the current unit testing with NUnit, lack of experience and no partnership in agile development is my weakness, and I need more time to train up my experience. I hope that I have opportunity to apply agile in future although current environment seen doesn’t permit.

Another good thing in this meeting is meeting with Rex, who is the Ruby developer and IT company owner, we have similar idea of Hong Kong IT Event Calendar before and he did better than me. Also, in this meeting, free pizza was good, and small gift (should be the score card) was offered.

Rebuild Index on MsSQL

Tuesday, April 28th, 2009

今日要對現有既 Table 做 Performance Testing, 但因為要 join 太多既 Table, 搜尋一個要 join 4 個 Table 先拎到想要既 “一隻” Record 既時間竟然要 11 秒, 可想而知個 Table Size 有幾大吧 (其實七位數字既 Record Count 啫, 而 where case 只有兩個用 ‘=’ 既 criteria, 而且所有 Record 只能 insert, 不能 delete).

經過我 rebuild 過接近 10 個已有既 index 之後 (等了好幾分鐘), 搜尋時間減少至只需 5 秒…

以下係 mssql 2005 下 rebuild index 既一個 statement 既例子

ALTER INDEX IX_tblMaster ON tblMaster REBUILD;

IX_tblMaster 係 index 名
tblMaster 係 table 名

Sqlcmd

Monday, April 27th, 2009

Project Development Phase 已經開始, 但因為部份 Design 未全面, 所以仲有好多 Drop Table, Create Table 及 Initialize Value 既動作要做, 因為係咁, 所以就上網搵左點樣在 command prompt 行 SQL file.

Usage: Sqlcmd [-U login id] [-P password] [-S server] [-H hostname] [-E trusted connection] [-d use database name] [-l login timeout] [-t query timeout] [-h headers] [-s colseparator] [-w screen width] [-a packetsize] [-e echo input] [-I Enable Quoted Identifiers] [-c cmdend] [-L list servers[clean output]] [-q "cmdline query"] [-Q "cmdline query" and exit] [-m errorlevel] [-V severitylevel] [-W remove trailing spaces] [-u unicode output] [-r[0|1] msgs to stderr] [-i inputfile] [-o outputfile] [-z new password] [-f | i:[,o:]] [-Z new password and exit] [-k[1|2] remove[replace] control characters] [-y variable length type display width] [-Y fixed length type display width] [-p[1] print statistics[colon format]] [-R use client regional setting] [-b On error batch abort] [-v var = "value"...] [-A dedicated admin connection] [-X[1] disable commands, startup script, environment variables [and exit]] [-x disable variable substitution] [-? show syntax summary]

而自己就寫左個 batch 係 rebuild database:

sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Drop_Table.sql
sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Create_Table.sql
sqlcmd -U calendarw -P password -S 192.168.1.10 -d testing_database -i Init_Default_Value.sql

Update Statement with Other Table

Thursday, April 23rd, 2009

呢排為設置 Testing 場而預備緊 d SQL statement, 有部份 Data 得 detail table, 但使用時要整返個 master 比佢, 所以就要使用 update statement 黎 link 返唔同 table 既 key, 以下係一個例子:

update tblModel
set series_pkey = tblSeries.pkey
from tblModel, tblSeries
where tblSeries.full_name = 'A Series' and tblModel.full_name like 'A %';