Mrfei 发表于 2016-11-22 10:12:01

海量数据自动生成系统的架构与实现(附源码)

  海量数据自动生成系统介绍
  在做单元测试和功能测试的时候,需要比较多的数据来发现问题,对于软件工程师来讲,手动输入这些数据
  是蛮浪费时间的事。为了减轻咱们工程师的工作压力和工作量,我开发了这个系统。
  希望大家在使用过程中提供更多宝贵意见。
http://www.learnqtp.com/wp-content/uploads/2009/01/testdata7.gif
  该系统的主要功能
        1> 自动生成字符串、中文字符串、整数和浮点数。
        2> 目前支持MSSql,MySql, Oracle,Sqlite
        3> 对需要加密用户密码的系统,提供加密

        4> 支持第三方开发的插件

  该系统的使用方法

  该系统已内置了三种常用的数据库,包括MSSql,MySql, 和Oracle.

  对于这几种数据库的使用方法如下: (在App.Config需要配置ConnectionString)
  MSSql:

http://www.cnblogs.com/caichunsheng/archive/2010/05/29/e:%5C%E8%B5%84%E6%96%99%E5%BA%93%5C%E6%8A%80%E6%9C%AF%E5%BA%93%5Csql%20server%5Cdbscript_mssql.png  //定义插入数据表的sql语句
  string query = "insert into Users(Email,Password, Salt, Status, Online, CreateDate, VerifyDate, LastLoginDate) values(@Email, @Password, @Salt, @Status, @Online, @CreateDate, @VerifyDate, @LastLoginDate)";
            //创建 PreparedStatement
            PreparedStatement pstmt = new PreparedStatement(DBType.MSSql, query);            
  for (int index = 0; index < 1000000; index++)
  {               
  var email = Gloser.GetRandomEmail(8, "@gmail.com");
  var password = Gloser.getRandomString(8);
  pstmt.SetParam("@Email", email);
  pstmt.SetParam("@Password", CryptographyManager.EncodePassowrd(password));
  pstmt.SetParam("@Salt", Gloser.getRandomString(15));
  pstmt.SetParam("@Status", 1);
  pstmt.SetParam("@Online", 1);
  pstmt.SetParam("@CreateDate", DateTime.Now);
  pstmt.SetParam("@VerifyDate", DateTime.Now);
  pstmt.SetParam("@LastLoginDate", DateTime.Now);
  pstmt.AddBatch();
  if ((index > 0) && (index % 500 == 0))
  {   
  pstmt.Execute();
  pstmt.ClearParameters();
  }
  }            
  pstmt.Execute();
   MySql
  string query = "insert into settings(k,v) values(@k,@v)";
            PreparedStatement pstmt = new PreparedStatement(DBType.MySql, query);
            for (int index = 0; index < 100000; index++)
            {
                pstmt.SetParam("@k", Gloser.getRandomString(32));
                pstmt.SetParam("@v", Gloser.GetRandomChinese(100));
                pstmt.AddBatch();
                if ((index > 0) && (index % 500 == 0))
                {
                  pstmt.Execute();
                  pstmt.ClearParameters();
                }
            }
            pstmt.Execute();
  Oracle:
  string query = "insert into book(bookid,bookname,author,price) values(:bookid,:bookname,:author,:price)";
            PreparedStatement pstmt = new PreparedStatement(DBType.Oralce, query);
            for (int index = 0; index < 100000; index++)
            {
                pstmt.SetParam(":bookid", Gloser.GetRandomInt(100000));
                pstmt.SetParam(":bookname", Gloser.GetRandomChinese(25));
                pstmt.SetParam(":author", Gloser.GetRandomChinese(10));
                pstmt.SetParam(":price", Gloser.GetRandomInt(200));
                pstmt.AddBatch();
                if ((index > 0) && (index % 500 == 0))
                {
                  pstmt.Execute();
                  pstmt.ClearParameters();
                }
            }
            pstmt.Execute();
  Sqlite:
  Sqlite的使用方法与以上数据库类似,不同的地方是系统通过插件接口调用的。需要在配置文件中定义:

           <appSettings>
          <add key="SqlCacheAssembly" value="PerfRunner.Sqlite"/>
          <add key="SqlCacheName" value="PerfRunner.Sqlite.SqlitePerformanceTest"/>
      </appSettings>

            SqlCacheAssembly指实现Sqlite的程序集名称,SqlCacheName指实现ISqlCache接口的类名

  配置Connection:

  <add name="OtherDbConnectionString" connectionString="Data Source=E:\test.db" />

  生成海量数据代码如下:
  string query = "insert into book(bookid,bookname,author,price) values(@bookid,@bookname,@author,@price)";
            PreparedStatement pstmt = new PreparedStatement(query);
            for (int index = 0; index < 100000; index++)
            {
                pstmt.SetParam("@bookid", index);
                pstmt.SetParam("@bookname", Gloser.GetRandomChinese(25));
                pstmt.SetParam("@author", Gloser.GetRandomChinese(10));
                pstmt.SetParam("@price", Gloser.GetRandomInt(200));
                pstmt.AddBatch();
                if ((index > 0) && (index % 500 == 0))
                {
                  pstmt.Execute();
                  pstmt.ClearParameters();
                }
            }
            pstmt.Execute();
  该系统的架构
http://images.cnblogs.com/cnblogs_com/caichunsheng/248148/r_sqlcache.gif
  未来的发展路径

  以后的版本会提供如下功能:
  1>支持Mono
  2>支持更多的数据库,包括DB2,Postgsql等等。
  3>界面操作
  4>集成在开发环境中
  5>支持更多业务规则

  参考
  C#中海量数据的批量插入和更新   

  PreparedStatement
  ConnectionString
  设计模式 from TerryLee
  插件系统的设计
  Reflection in .NET
  test driven
  Ezsocio

  dotConnect for PostgreSQL

  主键生成器
  generating random char and random string, random double
  oracle 数据测试
  表或视图不存在

  附源码
页: [1]
查看完整版本: 海量数据自动生成系统的架构与实现(附源码)