淺談SQL Server數(shù)據(jù)庫(kù)并發(fā)測(cè)試方法

字號(hào):


    1. 利用測(cè)試工具模擬多個(gè)最終用戶進(jìn)行并發(fā)測(cè)試;
    這種測(cè)試方法的缺點(diǎn):最終用戶往往并不是直接連接到數(shù)據(jù)庫(kù)上,而是要經(jīng)過(guò)一個(gè)和多個(gè)中間服務(wù)程序,所以并不能保證訪問(wèn)數(shù)據(jù)庫(kù)時(shí)還是并發(fā)。其次,這種測(cè)試方法需要等到客戶端程序、服務(wù)端程序全部完成才能進(jìn)行;
    2. 利用測(cè)試工具編寫腳本,直接連接數(shù)據(jù)庫(kù)進(jìn)行并發(fā)測(cè)試;
    這種方法可以有效的保證并發(fā)操作,而且在數(shù)據(jù)庫(kù)訪問(wèn)程序完成即可測(cè)試,可以大大縮短測(cè)試時(shí)間,而且測(cè)試效果更好。
    下面通過(guò)一個(gè)演示程序,演示使用Robot使用第二種測(cè)試方法進(jìn)行數(shù)據(jù)庫(kù)的并發(fā)測(cè)試。
    第一步:創(chuàng)建演示程序
    打開SQL SERVER查詢分析器,在SQL SERVER測(cè)試數(shù)據(jù)庫(kù)中執(zhí)行下列腳本(腳本執(zhí)行操作:創(chuàng)建表testtable,并插入一條記錄;創(chuàng)建存儲(chǔ)過(guò)程test): 
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Test]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
    drop procedure [dbo].[Test]
    GO
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[testtable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[testtable]
    GO
    CREATE TABLE [dbo].[testtable] (
    [testid] [int] NULL ,
    [counts] [int] NULL
    ) ON [PRIMARY]
    GO
    insert into testtable (testid,counts) values (1,0)
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    SET ANSI_NULLS ON
    GO
    CREATE Procedure dbo.Test
    as
    declare @count int
    begin tran TEST
    select @count=countsfrom testtable where testid=1
    update testtable setcounts=@count+1
    if (@@error >0) begin
    rollback tran TEST
    end else begin
    commit tran TEST
    end
    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    SET ANSI_NULLS ON
    GO
    第二步:創(chuàng)建測(cè)試腳本
    在Robot中新建VU腳本,輸入以下內(nèi)容:
    #include
    {
    push Timeout_scale = 200; /* Set timeouts to 200% of maximum response time */
    push Think_def = "LR";
    Min_tmout = 120000; /* Set minimum Timeout_val to 2 minutes*/
    push Timeout_val = Min_tmout;
    ser=sqlconnect("server","sa","888","192.168.0.99","sqlserver");
    set Server_connection = ser;
    push Think_avg = 0;
    sync_point "logon";
    sqlexec ["sql_1000"] "testdb..test";
    sqldisconnect (ser);
    }
    說(shuō)明:
    ser=sqlconnect("server","sa","888","192.168.0.99","sqlserver")sa為數(shù)據(jù)庫(kù)用戶名,888為sa密碼,192.168.0.99數(shù)據(jù)庫(kù)IP地址
    以上三項(xiàng)按實(shí)際的測(cè)試數(shù)據(jù)庫(kù)設(shè)置更改,其他兩項(xiàng)不用修改
    sqlexec ["sql_1000"] "testdb..test"testdb為新建存儲(chǔ)過(guò)程test所在的數(shù)據(jù)庫(kù),按實(shí)際的數(shù)據(jù)庫(kù)修改
    第三步:執(zhí)行測(cè)試
    運(yùn)行上一步創(chuàng)建的腳本(運(yùn)行時(shí)自動(dòng)創(chuàng)建Suite),在Run Suite窗口中的"Number of users"上輸入20。運(yùn)行完腳本,打開數(shù)據(jù)庫(kù)查看counts的數(shù)值。把counts值改為零多次運(yùn)行腳本,觀察每次運(yùn)行后counts的結(jié)果。
    測(cè)試說(shuō)明:
    1. 測(cè)試示例程序的目的是,存儲(chǔ)過(guò)程test每執(zhí)行一次,表testtable中的counts字段增加一;
    2. 第三步的測(cè)試可以發(fā)現(xiàn)每次執(zhí)行后counts結(jié)果并不相同,而且不等于20,這說(shuō)明這個(gè)程序是在并發(fā)時(shí)是問(wèn)題的;
    3. 將存儲(chǔ)過(guò)程中的select @count=countsfrom testtable where testid=1修改為select @count=countsfrom testtable with (UPDLOCK) where testid=1。再次進(jìn)行并發(fā)測(cè)試,每次的結(jié)果應(yīng)該都是20。
    以上演示程序,僅僅演示了測(cè)試的方法。在實(shí)際的數(shù)據(jù)庫(kù)并發(fā)測(cè)試中,首先要確定存在哪些并發(fā)情況、哪些數(shù)據(jù)受到并發(fā)影響,然后編寫腳本,設(shè)置suite進(jìn)行并發(fā)測(cè)試。