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

字號:


    1. 利用測試工具模擬多個最終用戶進(jìn)行并發(fā)測試;
    這種測試方法的缺點(diǎn):最終用戶往往并不是直接連接到數(shù)據(jù)庫上,而是要經(jīng)過一個和多個中間服務(wù)程序,所以并不能保證訪問數(shù)據(jù)庫時還是并發(fā)。其次,這種測試方法需要等到客戶端程序、服務(wù)端程序全部完成才能進(jìn)行;
    2. 利用測試工具編寫腳本,直接連接數(shù)據(jù)庫進(jìn)行并發(fā)測試;
    這種方法可以有效的保證并發(fā)操作,而且在數(shù)據(jù)庫訪問程序完成即可測試,可以大大縮短測試時間,而且測試效果更好。
    下面通過一個演示程序,演示使用Robot使用第二種測試方法進(jìn)行數(shù)據(jù)庫的并發(fā)測試。
    第一步:創(chuàng)建演示程序
    打開SQL SERVER查詢分析器,在SQL SERVER測試數(shù)據(jù)庫中執(zhí)行下列腳本(腳本執(zhí)行操作:創(chuàng)建表testtable,并插入一條記錄;創(chuàng)建存儲過程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)建測試腳本
    在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);
    }
    說明:
    ser=sqlconnect("server","sa","888","192.168.0.99","sqlserver")sa為數(shù)據(jù)庫用戶名,888為sa密碼,192.168.0.99數(shù)據(jù)庫IP地址
    以上三項(xiàng)按實(shí)際的測試數(shù)據(jù)庫設(shè)置更改,其他兩項(xiàng)不用修改
    sqlexec ["sql_1000"] "testdb..test"testdb為新建存儲過程test所在的數(shù)據(jù)庫,按實(shí)際的數(shù)據(jù)庫修改
    第三步:執(zhí)行測試
    運(yùn)行上一步創(chuàng)建的腳本(運(yùn)行時自動創(chuàng)建Suite),在Run Suite窗口中的"Number of users"上輸入20。運(yùn)行完腳本,打開數(shù)據(jù)庫查看counts的數(shù)值。把counts值改為零多次運(yùn)行腳本,觀察每次運(yùn)行后counts的結(jié)果。
    測試說明:
    1. 測試示例程序的目的是,存儲過程test每執(zhí)行一次,表testtable中的counts字段增加一;
    2. 第三步的測試可以發(fā)現(xiàn)每次執(zhí)行后counts結(jié)果并不相同,而且不等于20,這說明這個程序是在并發(fā)時是問題的;
    3. 將存儲過程中的select @count=countsfrom testtable where testid=1修改為select @count=countsfrom testtable with (UPDLOCK) where testid=1。再次進(jìn)行并發(fā)測試,每次的結(jié)果應(yīng)該都是20。
    以上演示程序,僅僅演示了測試的方法。在實(shí)際的數(shù)據(jù)庫并發(fā)測試中,首先要確定存在哪些并發(fā)情況、哪些數(shù)據(jù)受到并發(fā)影響,然后編寫腳本,設(shè)置suite進(jìn)行并發(fā)測試。