权限管理、用户权限系统、开源用户权限系统、信息化建设标准基础数据管理平台
代码改变世界

C# winform 窗体接收命令行参数自动登录进行系统,模拟600个WCF客户端的并发压力测试

2011-10-02 12:31  通用C#系统架构  阅读(12139)  评论(9编辑  收藏  举报

  我们想要达到的目的是模拟600个客户端的消息提醒功能,当然我们没有600个电脑可以拿来测试,我们只有4-5台电脑可以用来测试,那我们就想办法在一个电脑上执行100来个客户端,用不通的帐户登录,模拟600个并发时的情况.

  现在遇到的问题:

  1:一个个登录,每个电脑上登录100来个用户是很繁琐的事情,人都会眼花缭乱。

  2:在测试过程中往往会发现一些问题,这时候又需要重新部署服务器端,又要部署客户端,那是很要命的事情。

  3:随时想测试程序性能的时候,不需要别人的协助,只要自己一个人是否可以顺利进行测试,又轻松又快的方式是否可以?总不能要求别人总加班,靠自己。

 

解决问题的思路:

 1:先把数据库里的用户名密码,都修改为有规律的密码,由于我们用的是测试数据库,所以密码是可以随便修改的,我们编写一段程序让系统中所有的用户的密码与用户名相同,由于密码在数据库里是加密的,所以需要用程序脚本来设置。

 2:在设置密码的同时,我们把自动运行的DOS脚本指令也获取了,那不是一箭双雕了不是,那我们就编写一段程序来实现一下这2个任务。

View Code
//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , Hairihan TECH, Ltd. 
//--------------------------------------------------------------------

using System.Data;
using System.IO;
using System.Text;

namespace DotNet.Example
{
    using DotNet.DbUtilities;
    using DotNet.Manager;
    using DotNet.Model;
    using DotNet.Utilities;

    public class UserPassword
    {
        public void SetPassword()
        {
            // 检查密码强度,默认要检查比较好,系统安全性高一些
            BaseSystemInfo.CheckPasswordStrength = true;
            // 打开数据库
            
// IDbHelper dbHelper = new OracleHelper("Data Source=KANGFU;user=usercenter;password=usercenter;");
            IDbHelper dbHelper = new SqlHelper("Data Source=localhost;Initial Catalog=UserCenterV36;User Id = sa ; Password = Password@sa;");
            dbHelper.Open();
            // 批处理指令
            StringBuilder Batch = new StringBuilder(); 
            // 获取用户列表,有效的,没有被删除的
            BaseUserManager userManager = new BaseUserManager(dbHelper);
            DataTable dt = userManager.GetDT(BaseUserTable.FieldDeletionStateCode, "0", BaseUserTable.FieldEnabled, "1");
            BaseUserEntity userEntity = null;
            foreach (DataRow dataRow in dt.Rows)
            {
                userEntity = new BaseUserEntity(dataRow);
                // 设置密码
                userManager.SetPassword(userEntity.Id.ToString(), userEntity.UserName.ToString());
                Batch.AppendLine(string.Format("start D:\\DotNet.Common\\DotNet.CommonV3.6\\DotNet.WinForm\\bin\\Debug\\DotNet.WinForm.exe UserName={0} Password={1}", userEntity.UserName.ToString(), userEntity.UserName.ToString()));
            }
            dbHelper.Close();

            // 将批处理写入文件
            WriterFile(Batch.ToString());
        }

        /// <summary>
        
/// 将批处理写入文件
        
/// </summary>
        
/// <param name="batch">指令</param>
        private void WriterFile(string batch)
        {
            // 把批处理结果保存到文件
            string writerFileName = "D:\\DotNet.Common\\DotNet.CommonV3.6\\DotNet.WinForm\\bin\\Debug\\RunTest.bat";
            StreamWriter streamWriter = new StreamWriter(writerFileName, false, Encoding.Default);
            streamWriter.Write(batch);
            streamWriter.Close();
        }
    }
}

 得到的DOS批量处理的结果如下RunTest.bat,为了节省篇幅就截取一部份:

View Code
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=Administrator Password=Administrator
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=SystemAdmin Password=SystemAdmin
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=UserAdmin Password=UserAdmin
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=jiangyanxiao Password=jiangyanxiao
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=hukuangming Password=hukuangming
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=caoyaping Password=caoyaping
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=gaoyufen Password=gaoyufen
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=xuqianping Password=xuqianping
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=jianglisha Password=jianglisha
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=zhuqingqing Password=zhuqingqing
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=luyizhou Password=luyizhou
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=qianleilei Password=qianleilei
start D:\DotNet.Common\DotNet.CommonV3.6\DotNet.WinForm\bin\Debug\DotNet.WinForm.exe UserName=lijia Password=lijia

 3:调试winform程序,让程序能接受命令行参数,参考代码如下:

View Code
        /// <summary>
        
/// 应用程序的主入口点。
        
/// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // System.Console.WriteLine(WindowsIdentity.GetCurrent().Name);

            
// 主应用程序集名
            BaseSystemInfo.MainAssembly = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
            BaseSystemInfo.StartupPath = Application.StartupPath;
            BaseSystemInfo.AppIco = Path.Combine(Application.StartupPath, "Resource/Form.ico");

            // 获取配置信息
            GetConfig();

            // 这里检查是否有外部用户名密码传输进来过
            if (args.Length > 0)
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i].ToString().StartsWith("UserName"))
                    {
                        BaseSystemInfo.CurrentUserName = args[i].ToString().Substring("UserName".Length + 1);
                    }
                    else if (args[i].ToString().StartsWith("Password"))
                    {
                        BaseSystemInfo.CurrentPassword = args[i].ToString().Substring("Password".Length + 1);
                        if (BaseSystemInfo.ClientEncryptPassword)
                        {
                            BaseSystemInfo.CurrentPassword = SecretUtil.Encrypt(BaseSystemInfo.CurrentPassword);
                        }
                    }
                    // Console.WriteLine(i.ToString() + ":" + args[i].ToString());
                }
            }

            if (BaseSystemInfo.MultiLanguage)
            {
                // 多语言国际化加载
                ResourceManagerWrapper.Instance.LoadResources(Path.Combine(Application.StartupPath, "Resource/Localization/"));
                // 从当前指定的语言包读取信息
                AppMessage.GetLanguageResource();
            }

            // 初始化服务
            DotNetService.Instance.InitService();

            // 按配置的登录页面进行登录,这里需要运行的是主程序才可以
            Form mainForm = BaseInterfaceLogic.GetForm(BaseSystemInfo.MainAssembly, BaseSystemInfo.MainForm);
            Application.Run(mainForm);
        }

 4:配置项目工程属性,模拟输入命令行参数如下图:

  5:把程序配置为能自动登录的状态如下效果图:

     也可以通过手工修改配置文件的方式把  Config.xml 里的

 <!-- 是否自动登录,默认不自动登录会好一些 -->
 <add key="AutoLogOn"value="True" /> 也可以达到相同的效果.

 

  6:万事大吉,只要双击 RunTest.bat,瞬间几百个客户端就自动登录,能模拟多用户并发压力测试了,将来做其它项目还可以反复使用,不用依靠别人自己一个人就可以测试了,省心省事了。

 

    开发软件很多功能都很简单,但是一个每个功能实现好都需要几个小时时间,例如这个自动登录进行压力测试的程序也编写调试了4个小时左右, 一个软件系统往往需要成白上千的功能点,前后编写花费的时间就会很长了,需要投入很密集的劳动力.

 

   例如我想购买一个软件,软件只有一个要求"姓李的周三不能登录,姓白的周日不能登录",就这么一个软件,你需要多久能开发好? 其实他没说这个软件背后的100个需求,当你开发好了后,客户会说,怎么没有权限管理功能? 怎么没有角色管理功能? 怎么没设置密码功能? 怎么没锁定帐户功能??????????

 

   就像是我想购买一个进销存,需要分级管理分级授权,你要开价多少? 其实他想要用友软件的所有功能,同时也想要金蝶的所有的优点之上要结合他们自己在用的系统优点,再加一个分级管理分级授权的功能,所以我们做任何软件系统,都需要把那些公用的,基础的1000个功能点都做好了,再去开发第1001个功能,才好满足客户的需要。

 



C# ASP.NET 通用权限设计、通用权限管理、通用权限组件、单点登录、集中式权限管理、统一授权体系、分级管理分级授权


微信扫一扫加好友