ORM增删改查并发性能测试

打印 上一主题 下一主题

主题 911|帖子 911|积分 2733

测试的现实意义

这两天在对一些ORM进行性能测试(涉及SqlSugar、FreeSql、Fast.Framework、Dapper.LiteSql),测试用的是Winform程序,别人第一眼看到我的程序,说,你这测试没意义!
可能我的测试程序的某些地方写的比较变态吧,但我认为有现实意义,并且网上有相关网站崩溃问题的文章,那什么“爆高分析”,“崩溃问题”,WinDbg用的很6,那写程序阶段为什么没发现呢?
场景

假设WebApi(或者说网站后台服务)正在被高并发访问,一段时间后,服务的线程数量达到了200正常的是类似这样的,任务来了,就干活:及以上,那么.NET的线程池,如果线程处于空闲状态,默认是20秒后释放线程,假设这200多个线程空闲了,但是还没有达到空闲20秒的时间,还没有释放,如果此时,又有大量高并发的访问,200个线程去操作数据库,那就可能很危险了。
关于性能

虽然代码写的可能有点变态,但我的测试不是很严格,也不是很规范。
性能如果不是数量级的差,问题都不大,比如一个是0.8秒,一个是1.8秒,那1.8秒的慢是慢了一点,但问题不大;但如果一个是0.8秒,一个是10秒,那10秒的这个,可能就有点问题了,即使别人用的Emit你用的反射,也不应该差这么多。
当然了,我的测试,数据规模不大,10000条,button可能会点10次,那也才10万数据量,在数据量方面我没有做极端测试,我的重点不是这个,因为很少会去查100万数据到内存中,就暂不讨论这个。
这个测试除了大体上测试一下性能,主要就是增加了并发测试。
非并发性能测试的截图我就不放全了,这几个ORM有差别,但我觉得2秒3秒还是6秒甚至8秒,都问题不大的,都算差不多,都算堪用。
测试代码

测试数据库是MySQL版本5.7.28,MySql的连接池大小是1200,MySql.Data.dll目前用的是最新的8.0.30版本(题外话,它里面的异步是假异步,把同步方法包装成的异步,我说怎么反而慢了呢,真异步要使用MySqlConnector.dll,Fast.Framework使用的是这个性能很好)。

1. 数据库连接字符串
  1. server=localhost;database=sqlsugar_test;user id=root;password=123456;character set=utf8mb4;
复制代码
这里没有设置min pool size和max pool size,也没有设置connection timeout,没有设置的话connection timeout默认是15秒。
2. Winfrom配置

在写Winform做高并发测试的时候,有个参数可能需要设置
  1. <system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>
复制代码
测试高并发网络请求的时候,要设置这个,那么数据库增删改查要不要设置呢?反正就一个参数,加或不加,可以试试。
3. 实体类

以SqlSugar的为例吧,几种ORM测试用的表结构是相同的。
  1. using System;using System.Linq;using System.Text;using SqlSugar;namespace Models{    ///    ///用户表    ///    [SugarTable("sys_user")]    public partial class SysUser    {           public SysUser(){           }           ///            /// Desc:主键           /// Default:           /// Nullable:False           ///<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>       [SugarColumn(IsPrimaryKey=true,IsIdentity=true,ColumnName="id")]           public long Id {get;set;}           ///            /// Desc:用户名           /// Default:           /// Nullable:False           ///<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>       [SugarColumn(ColumnName="user_name")]           public string UserName {get;set;}           ///            /// Desc:用户姓名           /// Default:           /// Nullable:True           ///<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>       [SugarColumn(ColumnName="real_name")]           public string RealName {get;set;}           ///            /// Desc:用户密码           /// Default:           /// Nullable:False           ///<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>       [SugarColumn(ColumnName="password")]           public string Password {get;set;}           ///            /// Desc:备注           /// Default:           /// Nullable:True           ///<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>       [SugarColumn(ColumnName="remark")]           public string Remark {get;set;}           ///            /// Desc:创建者ID           /// Default:           /// Nullable:False           ///<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>       [SugarColumn(ColumnName="create_userid")]           public string CreateUserid {get;set;}           ///            /// Desc:创建时间           /// Default:           /// Nullable:False           ///<system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>       [SugarColumn(ColumnName="create_time")]           public DateTime CreateTime {get;set;}           ///            /// Desc:更新者ID           /// Default:           /// Nullable:True           ///<system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>       [SugarColumn(ColumnName="update_userid")]           public string UpdateUserid {get;set;}           ///            /// Desc:更新时间           /// Default:           /// Nullable:True           ///<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>       [SugarColumn(ColumnName="update_time")]           public DateTime? UpdateTime {get;set;}    }}
复制代码
4. 测试界面,感受一下


5. FreeSql测试代码
  1. public class FreeSqlUtil
  2. {
  3.     #region CreateFreeSqlClient
  4.     public static IFreeSql CreateFreeSqlClient()
  5.     {
  6.         IFreeSql db = new FreeSql.FreeSqlBuilder()
  7.             .UseConnectionString(FreeSql.DataType.MySql, @"server=localhost;database=freesql_test;user id=root;password=123456;character set=utf8mb4;")
  8.             .UseGenerateCommandParameterWithLambda(true)
  9.             .Build(); //请务必定义成 Singleton 单例模式
  10.         return db;
  11.     }
  12.     #endregion
  13. }
复制代码
  1. using Models;using NLog;using FreeSqlDemo.Utils;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using FreeSql.Aop;using Newtonsoft.Json;using System.Threading;namespace FreeSqlDemo{    public partial class Form1 : Form    {        #region 变量        private Logger _log = NLog.LogManager.GetCurrentClassLogger();        private IFreeSql _db;        private int _count = 10000;        private bool _printSql = false;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            _db = FreeSqlUtil.CreateFreeSqlClient();            _db.Aop.CurdAfter += CurdAfter;            _printSql = cbxPrintSql.Checked;            RunTask(() =>            {<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>_db.Queryable().Count(); //预热<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>Log("预热完成");            });        }        #endregion        #region Log        private void Log(string log)        {            if (!this.IsDisposed)            {<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>if (this.InvokeRequired)<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>{<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>    this.BeginInvoke(new Action(() =><system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>    {<system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>        textBox1.AppendText(msg);<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>    }));<system.net>
  38.     <connectionManagement>
  39.         <add address="*" maxconnection="1200" />
  40.     </connectionManagement>
  41. </system.net>}<system.net>
  42.     <connectionManagement>
  43.         <add address="*" maxconnection="1200" />
  44.     </connectionManagement>
  45. </system.net>else<system.net>
  46.     <connectionManagement>
  47.         <add address="*" maxconnection="1200" />
  48.     </connectionManagement>
  49. </system.net>{<system.net>
  50.     <connectionManagement>
  51.         <add address="*" maxconnection="1200" />
  52.     </connectionManagement>
  53. </system.net>    textBox1.AppendText(msg);<system.net>
  54.     <connectionManagement>
  55.         <add address="*" maxconnection="1200" />
  56.     </connectionManagement>
  57. </system.net>}            }        }        #endregion        #region 清空输出框        private void button10_Click(object sender, EventArgs e)        {            textBox1.Text = string.Empty;        }        #endregion        #region RunTask        private Task RunTask(Action action)        {            return Task.Run(() =>            {<system.net>
  58.     <connectionManagement>
  59.         <add address="*" maxconnection="1200" />
  60.     </connectionManagement>
  61. </system.net>try<system.net>
  62.     <connectionManagement>
  63.         <add address="*" maxconnection="1200" />
  64.     </connectionManagement>
  65. </system.net>{<system.net>
  66.     <connectionManagement>
  67.         <add address="*" maxconnection="1200" />
  68.     </connectionManagement>
  69. </system.net>    action();<system.net>
  70.     <connectionManagement>
  71.         <add address="*" maxconnection="1200" />
  72.     </connectionManagement>
  73. </system.net>}<system.net>
  74.     <connectionManagement>
  75.         <add address="*" maxconnection="1200" />
  76.     </connectionManagement>
  77. </system.net>catch (Exception ex)<system.net>
  78.     <connectionManagement>
  79.         <add address="*" maxconnection="1200" />
  80.     </connectionManagement>
  81. </system.net>{<system.net>
  82.     <connectionManagement>
  83.         <add address="*" maxconnection="1200" />
  84.     </connectionManagement>
  85. </system.net>    Log(ex.Message + "\r\n" + ex.StackTrace);<system.net>
  86.     <connectionManagement>
  87.         <add address="*" maxconnection="1200" />
  88.     </connectionManagement>
  89. </system.net>}            });        }        private Task RunTask(Action action, T t)        {            return Task.Factory.StartNew(obj =>            {<system.net>
  90.     <connectionManagement>
  91.         <add address="*" maxconnection="1200" />
  92.     </connectionManagement>
  93. </system.net>try<system.net>
  94.     <connectionManagement>
  95.         <add address="*" maxconnection="1200" />
  96.     </connectionManagement>
  97. </system.net>{<system.net>
  98.     <connectionManagement>
  99.         <add address="*" maxconnection="1200" />
  100.     </connectionManagement>
  101. </system.net>    action((T)obj);<system.net>
  102.     <connectionManagement>
  103.         <add address="*" maxconnection="1200" />
  104.     </connectionManagement>
  105. </system.net>}<system.net>
  106.     <connectionManagement>
  107.         <add address="*" maxconnection="1200" />
  108.     </connectionManagement>
  109. </system.net>catch (Exception ex)<system.net>
  110.     <connectionManagement>
  111.         <add address="*" maxconnection="1200" />
  112.     </connectionManagement>
  113. </system.net>{<system.net>
  114.     <connectionManagement>
  115.         <add address="*" maxconnection="1200" />
  116.     </connectionManagement>
  117. </system.net>    Log(ex.ToString());<system.net>
  118.     <connectionManagement>
  119.         <add address="*" maxconnection="1200" />
  120.     </connectionManagement>
  121. </system.net>    throw;<system.net>
  122.     <connectionManagement>
  123.         <add address="*" maxconnection="1200" />
  124.     </connectionManagement>
  125. </system.net>}            }, t);        }        #endregion        #region 打印SQL        private void CurdAfter(object sender, CurdAfterEventArgs e)        {            if (_printSql)            {<system.net>
  126.     <connectionManagement>
  127.         <add address="*" maxconnection="1200" />
  128.     </connectionManagement>
  129. </system.net>RunTask(() =><system.net>
  130.     <connectionManagement>
  131.         <add address="*" maxconnection="1200" />
  132.     </connectionManagement>
  133. </system.net>{<system.net>
  134.     <connectionManagement>
  135.         <add address="*" maxconnection="1200" />
  136.     </connectionManagement>
  137. </system.net>    string msg = "SQL:" + e.Sql + "\r\nParam:" + JsonConvert.SerializeObject(e.DbParms.ToDictionary(it => it.ParameterName, it => it.Value));<system.net>
  138.     <connectionManagement>
  139.         <add address="*" maxconnection="1200" />
  140.     </connectionManagement>
  141. </system.net>    Console.WriteLine(msg);<system.net>
  142.     <connectionManagement>
  143.         <add address="*" maxconnection="1200" />
  144.     </connectionManagement>
  145. </system.net>    _log.Debug(msg);<system.net>
  146.     <connectionManagement>
  147.         <add address="*" maxconnection="1200" />
  148.     </connectionManagement>
  149. </system.net>});            }        }        #endregion        #region cbxPrintSql_Click        private void cbxPrintSql_Click(object sender, EventArgs e)        {            _printSql = cbxPrintSql.Checked;        }        #endregion        #region 生成实体类        private void button1_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  150.     <connectionManagement>
  151.         <add address="*" maxconnection="1200" />
  152.     </connectionManagement>
  153. </system.net>Log("开始生成实体类");<system.net>
  154.     <connectionManagement>
  155.         <add address="*" maxconnection="1200" />
  156.     </connectionManagement>
  157. </system.net>Log("生成实体类完成");            });        }        #endregion        #region 测试查询        private void button2_Click(object sender, EventArgs e)        {            Log("开始查询");            SysUser conditionModel = new SysUser();            conditionModel.Remark = "管理员";            string remark = "管理员";            List list = _db.Queryable().Where(t => t.Id < 20 && t.Remark.Contains(conditionModel.Remark) && t.CreateTime > new DateTime(2010, 1, 1)).ToList();            foreach (SysUser user in list)            {<system.net>
  158.     <connectionManagement>
  159.         <add address="*" maxconnection="1200" />
  160.     </connectionManagement>
  161. </system.net>Log(string.Format("{0},{1},{2},{3}", user.UserName, user.RealName, user.Remark, user.CreateTime.ToString("yyyy-MM-dd")));            }            Log("查询结束 count=" + list.Count);        }        #endregion        #region 测试批量修改        private void button3_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  162.     <connectionManagement>
  163.         <add address="*" maxconnection="1200" />
  164.     </connectionManagement>
  165. </system.net>Random rnd = new Random();<system.net>
  166.     <connectionManagement>
  167.         <add address="*" maxconnection="1200" />
  168.     </connectionManagement>
  169. </system.net>List userList = _db.Queryable().Where(t => t.Id > 20).ToList();<system.net>
  170.     <connectionManagement>
  171.         <add address="*" maxconnection="1200" />
  172.     </connectionManagement>
  173. </system.net>Log("批量修改 开始 count=" + userList.Count.ToString("0 000"));<system.net>
  174.     <connectionManagement>
  175.         <add address="*" maxconnection="1200" />
  176.     </connectionManagement>
  177. </system.net>foreach (SysUser user in userList)<system.net>
  178.     <connectionManagement>
  179.         <add address="*" maxconnection="1200" />
  180.     </connectionManagement>
  181. </system.net>{<system.net>
  182.     <connectionManagement>
  183.         <add address="*" maxconnection="1200" />
  184.     </connectionManagement>
  185. </system.net>    user.Remark = "测试修改用户" + rnd.Next(0, 10000);<system.net>
  186.     <connectionManagement>
  187.         <add address="*" maxconnection="1200" />
  188.     </connectionManagement>
  189. </system.net>    user.UpdateUserid = "1";<system.net>
  190.     <connectionManagement>
  191.         <add address="*" maxconnection="1200" />
  192.     </connectionManagement>
  193. </system.net>    user.UpdateTime = DateTime.Now;<system.net>
  194.     <connectionManagement>
  195.         <add address="*" maxconnection="1200" />
  196.     </connectionManagement>
  197. </system.net>}<system.net>
  198.     <connectionManagement>
  199.         <add address="*" maxconnection="1200" />
  200.     </connectionManagement>
  201. </system.net>DateTime dt = DateTime.Now;<system.net>
  202.     <connectionManagement>
  203.         <add address="*" maxconnection="1200" />
  204.     </connectionManagement>
  205. </system.net>_db.Ado.Transaction(() =><system.net>
  206.     <connectionManagement>
  207.         <add address="*" maxconnection="1200" />
  208.     </connectionManagement>
  209. </system.net>{<system.net>
  210.     <connectionManagement>
  211.         <add address="*" maxconnection="1200" />
  212.     </connectionManagement>
  213. </system.net>    _db.Update().SetSource(userList).ExecuteAffrows();<system.net>
  214.     <connectionManagement>
  215.         <add address="*" maxconnection="1200" />
  216.     </connectionManagement>
  217. </system.net>});<system.net>
  218.     <connectionManagement>
  219.         <add address="*" maxconnection="1200" />
  220.     </connectionManagement>
  221. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  222.     <connectionManagement>
  223.         <add address="*" maxconnection="1200" />
  224.     </connectionManagement>
  225. </system.net>Log("批量修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试批量添加        private void button4_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  226.     <connectionManagement>
  227.         <add address="*" maxconnection="1200" />
  228.     </connectionManagement>
  229. </system.net>List userList = new List();<system.net>
  230.     <connectionManagement>
  231.         <add address="*" maxconnection="1200" />
  232.     </connectionManagement>
  233. </system.net>for (int i = 1; i<system.net>
  234.     <connectionManagement>
  235.         <add address="*" maxconnection="1200" />
  236.     </connectionManagement>
  237. </system.net> {<system.net>
  238.     <connectionManagement>
  239.         <add address="*" maxconnection="1200" />
  240.     </connectionManagement>
  241. </system.net>    _db.Insert(userList).ExecuteAffrows();<system.net>
  242.     <connectionManagement>
  243.         <add address="*" maxconnection="1200" />
  244.     </connectionManagement>
  245. </system.net>});<system.net>
  246.     <connectionManagement>
  247.         <add address="*" maxconnection="1200" />
  248.     </connectionManagement>
  249. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  250.     <connectionManagement>
  251.         <add address="*" maxconnection="1200" />
  252.     </connectionManagement>
  253. </system.net>Log("批量添加 结束,完成:" + time + "秒");            });        }        #endregion        #region 删除        private void button5_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  254.     <connectionManagement>
  255.         <add address="*" maxconnection="1200" />
  256.     </connectionManagement>
  257. </system.net>Log("删除 开始");<system.net>
  258.     <connectionManagement>
  259.         <add address="*" maxconnection="1200" />
  260.     </connectionManagement>
  261. </system.net>_db.Delete().Where(t => t.Id > 20).ExecuteAffrows();<system.net>
  262.     <connectionManagement>
  263.         <add address="*" maxconnection="1200" />
  264.     </connectionManagement>
  265. </system.net>Log("删除 完成");            });        }        #endregion        #region 测试循环修改        private void button7_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  266.     <connectionManagement>
  267.         <add address="*" maxconnection="1200" />
  268.     </connectionManagement>
  269. </system.net>Random rnd = new Random();<system.net>
  270.     <connectionManagement>
  271.         <add address="*" maxconnection="1200" />
  272.     </connectionManagement>
  273. </system.net>List userList = _db.Queryable().Where(t => t.Id > 20).ToList();<system.net>
  274.     <connectionManagement>
  275.         <add address="*" maxconnection="1200" />
  276.     </connectionManagement>
  277. </system.net>Log("循环修改 开始 count=" + userList.Count.ToString("0 000"));<system.net>
  278.     <connectionManagement>
  279.         <add address="*" maxconnection="1200" />
  280.     </connectionManagement>
  281. </system.net>DateTime dt = DateTime.Now;<system.net>
  282.     <connectionManagement>
  283.         <add address="*" maxconnection="1200" />
  284.     </connectionManagement>
  285. </system.net>_db.Ado.Transaction(() =><system.net>
  286.     <connectionManagement>
  287.         <add address="*" maxconnection="1200" />
  288.     </connectionManagement>
  289. </system.net>{<system.net>
  290.     <connectionManagement>
  291.         <add address="*" maxconnection="1200" />
  292.     </connectionManagement>
  293. </system.net>    var repo = _db.GetRepository();<system.net>
  294.     <connectionManagement>
  295.         <add address="*" maxconnection="1200" />
  296.     </connectionManagement>
  297. </system.net>    foreach (SysUser user in userList)<system.net>
  298.     <connectionManagement>
  299.         <add address="*" maxconnection="1200" />
  300.     </connectionManagement>
  301. </system.net>    {<system.net>
  302.     <connectionManagement>
  303.         <add address="*" maxconnection="1200" />
  304.     </connectionManagement>
  305. </system.net>        repo.Attach(user);<system.net>
  306.     <connectionManagement>
  307.         <add address="*" maxconnection="1200" />
  308.     </connectionManagement>
  309. </system.net>    }<system.net>
  310.     <connectionManagement>
  311.         <add address="*" maxconnection="1200" />
  312.     </connectionManagement>
  313. </system.net>    foreach (SysUser user in userList)<system.net>
  314.     <connectionManagement>
  315.         <add address="*" maxconnection="1200" />
  316.     </connectionManagement>
  317. </system.net>    {<system.net>
  318.     <connectionManagement>
  319.         <add address="*" maxconnection="1200" />
  320.     </connectionManagement>
  321. </system.net>        user.Remark = "测试修改用户" + rnd.Next(0, 10000);<system.net>
  322.     <connectionManagement>
  323.         <add address="*" maxconnection="1200" />
  324.     </connectionManagement>
  325. </system.net>        user.UpdateUserid = "1";<system.net>
  326.     <connectionManagement>
  327.         <add address="*" maxconnection="1200" />
  328.     </connectionManagement>
  329. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  330.     <connectionManagement>
  331.         <add address="*" maxconnection="1200" />
  332.     </connectionManagement>
  333. </system.net>    }<system.net>
  334.     <connectionManagement>
  335.         <add address="*" maxconnection="1200" />
  336.     </connectionManagement>
  337. </system.net>    foreach (SysUser user in userList)<system.net>
  338.     <connectionManagement>
  339.         <add address="*" maxconnection="1200" />
  340.     </connectionManagement>
  341. </system.net>    {<system.net>
  342.     <connectionManagement>
  343.         <add address="*" maxconnection="1200" />
  344.     </connectionManagement>
  345. </system.net>        repo.Update(user);<system.net>
  346.     <connectionManagement>
  347.         <add address="*" maxconnection="1200" />
  348.     </connectionManagement>
  349. </system.net>    }<system.net>
  350.     <connectionManagement>
  351.         <add address="*" maxconnection="1200" />
  352.     </connectionManagement>
  353. </system.net>});<system.net>
  354.     <connectionManagement>
  355.         <add address="*" maxconnection="1200" />
  356.     </connectionManagement>
  357. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  358.     <connectionManagement>
  359.         <add address="*" maxconnection="1200" />
  360.     </connectionManagement>
  361. </system.net>Log("循环修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试循环添加        private void button6_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  362.     <connectionManagement>
  363.         <add address="*" maxconnection="1200" />
  364.     </connectionManagement>
  365. </system.net>List userList = new List();<system.net>
  366.     <connectionManagement>
  367.         <add address="*" maxconnection="1200" />
  368.     </connectionManagement>
  369. </system.net>for (int i = 1; i<system.net>
  370.     <connectionManagement>
  371.         <add address="*" maxconnection="1200" />
  372.     </connectionManagement>
  373. </system.net> {<system.net>
  374.     <connectionManagement>
  375.         <add address="*" maxconnection="1200" />
  376.     </connectionManagement>
  377. </system.net>    foreach (SysUser user in userList)<system.net>
  378.     <connectionManagement>
  379.         <add address="*" maxconnection="1200" />
  380.     </connectionManagement>
  381. </system.net>    {<system.net>
  382.     <connectionManagement>
  383.         <add address="*" maxconnection="1200" />
  384.     </connectionManagement>
  385. </system.net>        _db.Insert(user).ExecuteIdentity();<system.net>
  386.     <connectionManagement>
  387.         <add address="*" maxconnection="1200" />
  388.     </connectionManagement>
  389. </system.net>    }<system.net>
  390.     <connectionManagement>
  391.         <add address="*" maxconnection="1200" />
  392.     </connectionManagement>
  393. </system.net>});<system.net>
  394.     <connectionManagement>
  395.         <add address="*" maxconnection="1200" />
  396.     </connectionManagement>
  397. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  398.     <connectionManagement>
  399.         <add address="*" maxconnection="1200" />
  400.     </connectionManagement>
  401. </system.net>Log("循环添加 完成,耗时:" + time + "秒");            });        }        #endregion        #region 查询        private void button9_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  402.     <connectionManagement>
  403.         <add address="*" maxconnection="1200" />
  404.     </connectionManagement>
  405. </system.net>_db.Queryable().Where(t => t.Id == 1).ToList(); //预热<system.net>
  406.     <connectionManagement>
  407.         <add address="*" maxconnection="1200" />
  408.     </connectionManagement>
  409. </system.net>Log("查询 开始");<system.net>
  410.     <connectionManagement>
  411.         <add address="*" maxconnection="1200" />
  412.     </connectionManagement>
  413. </system.net>DateTime dt = DateTime.Now;<system.net>
  414.     <connectionManagement>
  415.         <add address="*" maxconnection="1200" />
  416.     </connectionManagement>
  417. </system.net>for (int i = 0; i < 10; i++)<system.net>
  418.     <connectionManagement>
  419.         <add address="*" maxconnection="1200" />
  420.     </connectionManagement>
  421. </system.net>{<system.net>
  422.     <connectionManagement>
  423.         <add address="*" maxconnection="1200" />
  424.     </connectionManagement>
  425. </system.net>    List userList = _db.Queryable().Where(t => t.Id > 20 && t.RealName.Contains("测试")).OrderByDescending(t => t.CreateTime).OrderBy(t => t.Id).ToList();<system.net>
  426.     <connectionManagement>
  427.         <add address="*" maxconnection="1200" />
  428.     </connectionManagement>
  429. </system.net>    Log("查询结果 count=" + userList.Count.ToString());<system.net>
  430.     <connectionManagement>
  431.         <add address="*" maxconnection="1200" />
  432.     </connectionManagement>
  433. </system.net>}<system.net>
  434.     <connectionManagement>
  435.         <add address="*" maxconnection="1200" />
  436.     </connectionManagement>
  437. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  438.     <connectionManagement>
  439.         <add address="*" maxconnection="1200" />
  440.     </connectionManagement>
  441. </system.net>Log("查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 分页查询        private void button8_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  442.     <connectionManagement>
  443.         <add address="*" maxconnection="1200" />
  444.     </connectionManagement>
  445. </system.net>_db.Queryable().Where(t => t.Id == 1).ToList(); //预热<system.net>
  446.     <connectionManagement>
  447.         <add address="*" maxconnection="1200" />
  448.     </connectionManagement>
  449. </system.net>Log("分页查询 开始");<system.net>
  450.     <connectionManagement>
  451.         <add address="*" maxconnection="1200" />
  452.     </connectionManagement>
  453. </system.net>DateTime dt = DateTime.Now;<system.net>
  454.     <connectionManagement>
  455.         <add address="*" maxconnection="1200" />
  456.     </connectionManagement>
  457. </system.net>for (int i = 0; i < 10; i++)<system.net>
  458.     <connectionManagement>
  459.         <add address="*" maxconnection="1200" />
  460.     </connectionManagement>
  461. </system.net>{<system.net>
  462.     <connectionManagement>
  463.         <add address="*" maxconnection="1200" />
  464.     </connectionManagement>
  465. </system.net>    long total = _db.Queryable().Count();<system.net>
  466.     <connectionManagement>
  467.         <add address="*" maxconnection="1200" />
  468.     </connectionManagement>
  469. </system.net>    int pageSize = 100;<system.net>
  470.     <connectionManagement>
  471.         <add address="*" maxconnection="1200" />
  472.     </connectionManagement>
  473. </system.net>    int pageCount = (int)((total - 1) / pageSize + 1);<system.net>
  474.     <connectionManagement>
  475.         <add address="*" maxconnection="1200" />
  476.     </connectionManagement>
  477. </system.net>    List userList = new List();<system.net>
  478.     <connectionManagement>
  479.         <add address="*" maxconnection="1200" />
  480.     </connectionManagement>
  481. </system.net>    for (int page = 1; page  t.Id > 20 && t.RealName.Contains("测试")).OrderByDescending(t => t.CreateTime).OrderBy(t => t.Id).Page(page, pageSize).ToList());<system.net>
  482.     <connectionManagement>
  483.         <add address="*" maxconnection="1200" />
  484.     </connectionManagement>
  485. </system.net>    }<system.net>
  486.     <connectionManagement>
  487.         <add address="*" maxconnection="1200" />
  488.     </connectionManagement>
  489. </system.net>    Log("分页查询结果 count=" + userList.Count.ToString());<system.net>
  490.     <connectionManagement>
  491.         <add address="*" maxconnection="1200" />
  492.     </connectionManagement>
  493. </system.net>}<system.net>
  494.     <connectionManagement>
  495.         <add address="*" maxconnection="1200" />
  496.     </connectionManagement>
  497. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  498.     <connectionManagement>
  499.         <add address="*" maxconnection="1200" />
  500.     </connectionManagement>
  501. </system.net>Log("分页查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发查询        private void button11_Click(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            RunTask(() =>            {<system.net>
  502.     <connectionManagement>
  503.         <add address="*" maxconnection="1200" />
  504.     </connectionManagement>
  505. </system.net>Log("并发查询 开始");<system.net>
  506.     <connectionManagement>
  507.         <add address="*" maxconnection="1200" />
  508.     </connectionManagement>
  509. </system.net>DateTime dt = DateTime.Now;<system.net>
  510.     <connectionManagement>
  511.         <add address="*" maxconnection="1200" />
  512.     </connectionManagement>
  513. </system.net>List tasks = new List();<system.net>
  514.     <connectionManagement>
  515.         <add address="*" maxconnection="1200" />
  516.     </connectionManagement>
  517. </system.net>for (int i = 0; i < 200; i++)<system.net>
  518.     <connectionManagement>
  519.         <add address="*" maxconnection="1200" />
  520.     </connectionManagement>
  521. </system.net>{<system.net>
  522.     <connectionManagement>
  523.         <add address="*" maxconnection="1200" />
  524.     </connectionManagement>
  525. </system.net>    Task task = RunTask(() =><system.net>
  526.     <connectionManagement>
  527.         <add address="*" maxconnection="1200" />
  528.     </connectionManagement>
  529. </system.net>    {<system.net>
  530.     <connectionManagement>
  531.         <add address="*" maxconnection="1200" />
  532.     </connectionManagement>
  533. </system.net>        List userList = _db.Queryable()<system.net>
  534.     <connectionManagement>
  535.         <add address="*" maxconnection="1200" />
  536.     </connectionManagement>
  537. </system.net>            .Where(t => t.Id > 20 && t.RealName.Contains("测试"))<system.net>
  538.     <connectionManagement>
  539.         <add address="*" maxconnection="1200" />
  540.     </connectionManagement>
  541. </system.net>            .OrderByDescending(t => t.CreateTime).OrderBy(t => t.Id).ToList();<system.net>
  542.     <connectionManagement>
  543.         <add address="*" maxconnection="1200" />
  544.     </connectionManagement>
  545. </system.net>        Log("查询结果 count=" + userList.Count.ToString());<system.net>
  546.     <connectionManagement>
  547.         <add address="*" maxconnection="1200" />
  548.     </connectionManagement>
  549. </system.net>    });<system.net>
  550.     <connectionManagement>
  551.         <add address="*" maxconnection="1200" />
  552.     </connectionManagement>
  553. </system.net>    tasks.Add(task);<system.net>
  554.     <connectionManagement>
  555.         <add address="*" maxconnection="1200" />
  556.     </connectionManagement>
  557. </system.net>}<system.net>
  558.     <connectionManagement>
  559.         <add address="*" maxconnection="1200" />
  560.     </connectionManagement>
  561. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  562.     <connectionManagement>
  563.         <add address="*" maxconnection="1200" />
  564.     </connectionManagement>
  565. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  566.     <connectionManagement>
  567.         <add address="*" maxconnection="1200" />
  568.     </connectionManagement>
  569. </system.net>Log("并发查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发插入        private void button12_Click(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            RunTask(() =>            {<system.net>
  570.     <connectionManagement>
  571.         <add address="*" maxconnection="1200" />
  572.     </connectionManagement>
  573. </system.net>List userList = new List();<system.net>
  574.     <connectionManagement>
  575.         <add address="*" maxconnection="1200" />
  576.     </connectionManagement>
  577. </system.net>for (int i = 1; i<system.net>
  578.     <connectionManagement>
  579.         <add address="*" maxconnection="1200" />
  580.     </connectionManagement>
  581. </system.net>     {<system.net>
  582.     <connectionManagement>
  583.         <add address="*" maxconnection="1200" />
  584.     </connectionManagement>
  585. </system.net>        _db.Insert(user).ExecuteIdentity();<system.net>
  586.     <connectionManagement>
  587.         <add address="*" maxconnection="1200" />
  588.     </connectionManagement>
  589. </system.net>    }, item);<system.net>
  590.     <connectionManagement>
  591.         <add address="*" maxconnection="1200" />
  592.     </connectionManagement>
  593. </system.net>    tasks.Add(task);<system.net>
  594.     <connectionManagement>
  595.         <add address="*" maxconnection="1200" />
  596.     </connectionManagement>
  597. </system.net>}<system.net>
  598.     <connectionManagement>
  599.         <add address="*" maxconnection="1200" />
  600.     </connectionManagement>
  601. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  602.     <connectionManagement>
  603.         <add address="*" maxconnection="1200" />
  604.     </connectionManagement>
  605. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  606.     <connectionManagement>
  607.         <add address="*" maxconnection="1200" />
  608.     </connectionManagement>
  609. </system.net>Log("并发插入 完成,耗时:" + time + "秒");            });        }        #endregion    }}
复制代码
6. SqlSugar测试代码
  1. public class SqlSugarUtil
  2. {
  3.     public static readonly string ConnectionString = "server=localhost;database=sqlsugar_test;user id=root;password=123456;character set=utf8mb4;";
  4.     #region CreateSqlSugarClient
  5.     public static SqlSugarScope CreateSqlSugarClient()
  6.     {
  7.         SqlSugarScope db = new SqlSugarScope(new ConnectionConfig()
  8.         {
  9.             ConnectionString = ConnectionString,//连接符字串
  10.             DbType = SqlSugar.DbType.MySql, //数据库类型
  11.             IsAutoCloseConnection = true //不设成true要手动close
  12.         });
  13.         return db;
  14.     }
  15.     #endregion
  16.     #region CreateModels 生成实体类
  17.     public static void CreateModels(SqlSugarClient db, string tableName = null)
  18.     {
  19.         ......此处省略
  20.     }
  21.     #endregion
  22. }
复制代码
  1. using Models;using NLog;using SqlSugar;using SqlSugarDemo.Utils;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace SqlSugarDemo{    public partial class Form1 : Form    {        #region 变量        private Logger _log = NLog.LogManager.GetCurrentClassLogger();        private SqlSugarScope _db;        private int _count = 10000;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            _db = SqlSugarUtil.CreateSqlSugarClient();            _db.Aop.OnLogExecuting = OnLogExecuting;            RunTask(() =>            {<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>_db.Queryable().Count(); //预热<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>Log("预热完成");            });        }        #endregion        #region Log        private void Log(string log)        {            if (!this.IsDisposed)            {<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>if (this.InvokeRequired)<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>{<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>    this.BeginInvoke(new Action(() =><system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>    {<system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>        textBox1.AppendText(msg);<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>    }));<system.net>
  38.     <connectionManagement>
  39.         <add address="*" maxconnection="1200" />
  40.     </connectionManagement>
  41. </system.net>}<system.net>
  42.     <connectionManagement>
  43.         <add address="*" maxconnection="1200" />
  44.     </connectionManagement>
  45. </system.net>else<system.net>
  46.     <connectionManagement>
  47.         <add address="*" maxconnection="1200" />
  48.     </connectionManagement>
  49. </system.net>{<system.net>
  50.     <connectionManagement>
  51.         <add address="*" maxconnection="1200" />
  52.     </connectionManagement>
  53. </system.net>    textBox1.AppendText(msg);<system.net>
  54.     <connectionManagement>
  55.         <add address="*" maxconnection="1200" />
  56.     </connectionManagement>
  57. </system.net>}            }        }        #endregion        #region 清空输出框        private void button10_Click(object sender, EventArgs e)        {            textBox1.Text = string.Empty;        }        #endregion        #region RunTask        private Task RunTask(Action action)        {            return Task.Run(() =>            {<system.net>
  58.     <connectionManagement>
  59.         <add address="*" maxconnection="1200" />
  60.     </connectionManagement>
  61. </system.net>try<system.net>
  62.     <connectionManagement>
  63.         <add address="*" maxconnection="1200" />
  64.     </connectionManagement>
  65. </system.net>{<system.net>
  66.     <connectionManagement>
  67.         <add address="*" maxconnection="1200" />
  68.     </connectionManagement>
  69. </system.net>    action();<system.net>
  70.     <connectionManagement>
  71.         <add address="*" maxconnection="1200" />
  72.     </connectionManagement>
  73. </system.net>}<system.net>
  74.     <connectionManagement>
  75.         <add address="*" maxconnection="1200" />
  76.     </connectionManagement>
  77. </system.net>catch (Exception ex)<system.net>
  78.     <connectionManagement>
  79.         <add address="*" maxconnection="1200" />
  80.     </connectionManagement>
  81. </system.net>{<system.net>
  82.     <connectionManagement>
  83.         <add address="*" maxconnection="1200" />
  84.     </connectionManagement>
  85. </system.net>    Log(ex.Message + "\r\n" + ex.StackTrace);<system.net>
  86.     <connectionManagement>
  87.         <add address="*" maxconnection="1200" />
  88.     </connectionManagement>
  89. </system.net>}            });        }        private Task RunTask(Action action, T t)        {            return Task.Factory.StartNew(obj =>            {<system.net>
  90.     <connectionManagement>
  91.         <add address="*" maxconnection="1200" />
  92.     </connectionManagement>
  93. </system.net>try<system.net>
  94.     <connectionManagement>
  95.         <add address="*" maxconnection="1200" />
  96.     </connectionManagement>
  97. </system.net>{<system.net>
  98.     <connectionManagement>
  99.         <add address="*" maxconnection="1200" />
  100.     </connectionManagement>
  101. </system.net>    action((T)obj);<system.net>
  102.     <connectionManagement>
  103.         <add address="*" maxconnection="1200" />
  104.     </connectionManagement>
  105. </system.net>}<system.net>
  106.     <connectionManagement>
  107.         <add address="*" maxconnection="1200" />
  108.     </connectionManagement>
  109. </system.net>catch (Exception ex)<system.net>
  110.     <connectionManagement>
  111.         <add address="*" maxconnection="1200" />
  112.     </connectionManagement>
  113. </system.net>{<system.net>
  114.     <connectionManagement>
  115.         <add address="*" maxconnection="1200" />
  116.     </connectionManagement>
  117. </system.net>    Log(ex.ToString());<system.net>
  118.     <connectionManagement>
  119.         <add address="*" maxconnection="1200" />
  120.     </connectionManagement>
  121. </system.net>}            }, t);        }        #endregion        #region 打印SQL        private void OnLogExecuting(string sql, SugarParameter[] paramArr)        {            RunTask(() =>            {<system.net>
  122.     <connectionManagement>
  123.         <add address="*" maxconnection="1200" />
  124.     </connectionManagement>
  125. </system.net>string msg = "SQL:" + sql + "\r\nParam:" + _db.Utilities.SerializeObject(paramArr.ToDictionary(it => it.ParameterName, it => it.Value));<system.net>
  126.     <connectionManagement>
  127.         <add address="*" maxconnection="1200" />
  128.     </connectionManagement>
  129. </system.net>Console.WriteLine(msg);<system.net>
  130.     <connectionManagement>
  131.         <add address="*" maxconnection="1200" />
  132.     </connectionManagement>
  133. </system.net>_log.Debug(msg);            });        }        #endregion        #region 生成实体类        private void button1_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  134.     <connectionManagement>
  135.         <add address="*" maxconnection="1200" />
  136.     </connectionManagement>
  137. </system.net>Log("开始生成实体类");<system.net>
  138.     <connectionManagement>
  139.         <add address="*" maxconnection="1200" />
  140.     </connectionManagement>
  141. </system.net>SqlSugarUtil.CreateModels(new SqlSugarClient(new ConnectionConfig()<system.net>
  142.     <connectionManagement>
  143.         <add address="*" maxconnection="1200" />
  144.     </connectionManagement>
  145. </system.net>{<system.net>
  146.     <connectionManagement>
  147.         <add address="*" maxconnection="1200" />
  148.     </connectionManagement>
  149. </system.net>    ConnectionString = SqlSugarUtil.ConnectionString,//连接符字串<system.net>
  150.     <connectionManagement>
  151.         <add address="*" maxconnection="1200" />
  152.     </connectionManagement>
  153. </system.net>    DbType = SqlSugar.DbType.MySql, //数据库类型<system.net>
  154.     <connectionManagement>
  155.         <add address="*" maxconnection="1200" />
  156.     </connectionManagement>
  157. </system.net>    IsAutoCloseConnection = true //不设成true要手动close<system.net>
  158.     <connectionManagement>
  159.         <add address="*" maxconnection="1200" />
  160.     </connectionManagement>
  161. </system.net>}));<system.net>
  162.     <connectionManagement>
  163.         <add address="*" maxconnection="1200" />
  164.     </connectionManagement>
  165. </system.net>Log("生成实体类完成");            });        }        #endregion        #region 测试查询        private void button2_Click(object sender, EventArgs e)        {            Log("开始查询");            SysUser conditionModel = new SysUser();            conditionModel.Remark = "管理员";            string remark = "管理员";            List list = _db.Queryable().Where(t => t.Id < 20 && t.Remark.Contains(conditionModel.Remark) && t.CreateTime > new DateTime(2010, 1, 1)).ToList();            foreach (SysUser user in list)            {<system.net>
  166.     <connectionManagement>
  167.         <add address="*" maxconnection="1200" />
  168.     </connectionManagement>
  169. </system.net>Log(string.Format("{0},{1},{2},{3}", user.UserName, user.RealName, user.Remark, user.CreateTime.ToString("yyyy-MM-dd")));            }            Log("查询结束 count=" + list.Count);        }        #endregion        #region 测试批量修改        private void button3_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  170.     <connectionManagement>
  171.         <add address="*" maxconnection="1200" />
  172.     </connectionManagement>
  173. </system.net>Random rnd = new Random();<system.net>
  174.     <connectionManagement>
  175.         <add address="*" maxconnection="1200" />
  176.     </connectionManagement>
  177. </system.net>List userList = _db.Queryable().Where(t => t.Id > 20).ToList();<system.net>
  178.     <connectionManagement>
  179.         <add address="*" maxconnection="1200" />
  180.     </connectionManagement>
  181. </system.net>Log("批量修改 开始 count=" + userList.Count.ToString("0 000"));<system.net>
  182.     <connectionManagement>
  183.         <add address="*" maxconnection="1200" />
  184.     </connectionManagement>
  185. </system.net>foreach (SysUser user in userList)<system.net>
  186.     <connectionManagement>
  187.         <add address="*" maxconnection="1200" />
  188.     </connectionManagement>
  189. </system.net>{<system.net>
  190.     <connectionManagement>
  191.         <add address="*" maxconnection="1200" />
  192.     </connectionManagement>
  193. </system.net>    user.Remark = "测试修改用户" + rnd.Next(0, 10000);<system.net>
  194.     <connectionManagement>
  195.         <add address="*" maxconnection="1200" />
  196.     </connectionManagement>
  197. </system.net>    user.UpdateUserid = "1";<system.net>
  198.     <connectionManagement>
  199.         <add address="*" maxconnection="1200" />
  200.     </connectionManagement>
  201. </system.net>    user.UpdateTime = DateTime.Now;<system.net>
  202.     <connectionManagement>
  203.         <add address="*" maxconnection="1200" />
  204.     </connectionManagement>
  205. </system.net>}<system.net>
  206.     <connectionManagement>
  207.         <add address="*" maxconnection="1200" />
  208.     </connectionManagement>
  209. </system.net>DateTime dt = DateTime.Now;<system.net>
  210.     <connectionManagement>
  211.         <add address="*" maxconnection="1200" />
  212.     </connectionManagement>
  213. </system.net>try<system.net>
  214.     <connectionManagement>
  215.         <add address="*" maxconnection="1200" />
  216.     </connectionManagement>
  217. </system.net>{<system.net>
  218.     <connectionManagement>
  219.         <add address="*" maxconnection="1200" />
  220.     </connectionManagement>
  221. </system.net>    _db.Ado.BeginTran();<system.net>
  222.     <connectionManagement>
  223.         <add address="*" maxconnection="1200" />
  224.     </connectionManagement>
  225. </system.net>    _db.Updateable(userList).ExecuteCommand();<system.net>
  226.     <connectionManagement>
  227.         <add address="*" maxconnection="1200" />
  228.     </connectionManagement>
  229. </system.net>    _db.Ado.CommitTran();<system.net>
  230.     <connectionManagement>
  231.         <add address="*" maxconnection="1200" />
  232.     </connectionManagement>
  233. </system.net>}<system.net>
  234.     <connectionManagement>
  235.         <add address="*" maxconnection="1200" />
  236.     </connectionManagement>
  237. </system.net>catch (Exception ex)<system.net>
  238.     <connectionManagement>
  239.         <add address="*" maxconnection="1200" />
  240.     </connectionManagement>
  241. </system.net>{<system.net>
  242.     <connectionManagement>
  243.         <add address="*" maxconnection="1200" />
  244.     </connectionManagement>
  245. </system.net>    _db.Ado.RollbackTran();<system.net>
  246.     <connectionManagement>
  247.         <add address="*" maxconnection="1200" />
  248.     </connectionManagement>
  249. </system.net>    throw ex;<system.net>
  250.     <connectionManagement>
  251.         <add address="*" maxconnection="1200" />
  252.     </connectionManagement>
  253. </system.net>}<system.net>
  254.     <connectionManagement>
  255.         <add address="*" maxconnection="1200" />
  256.     </connectionManagement>
  257. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  258.     <connectionManagement>
  259.         <add address="*" maxconnection="1200" />
  260.     </connectionManagement>
  261. </system.net>Log("批量修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试批量添加        private void button4_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  262.     <connectionManagement>
  263.         <add address="*" maxconnection="1200" />
  264.     </connectionManagement>
  265. </system.net>List userList = new List();<system.net>
  266.     <connectionManagement>
  267.         <add address="*" maxconnection="1200" />
  268.     </connectionManagement>
  269. </system.net>for (int i = 1; i             {<system.net>
  270.     <connectionManagement>
  271.         <add address="*" maxconnection="1200" />
  272.     </connectionManagement>
  273. </system.net>Log("删除 开始");<system.net>
  274.     <connectionManagement>
  275.         <add address="*" maxconnection="1200" />
  276.     </connectionManagement>
  277. </system.net>_db.Deleteable(t => t.Id > 20).ExecuteCommand();<system.net>
  278.     <connectionManagement>
  279.         <add address="*" maxconnection="1200" />
  280.     </connectionManagement>
  281. </system.net>Log("删除 完成");            });        }        #endregion        #region 测试循环修改        private void button7_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  282.     <connectionManagement>
  283.         <add address="*" maxconnection="1200" />
  284.     </connectionManagement>
  285. </system.net>Random rnd = new Random();<system.net>
  286.     <connectionManagement>
  287.         <add address="*" maxconnection="1200" />
  288.     </connectionManagement>
  289. </system.net>List userList = _db.Queryable().Where(t => t.Id > 20).ToList();<system.net>
  290.     <connectionManagement>
  291.         <add address="*" maxconnection="1200" />
  292.     </connectionManagement>
  293. </system.net>Log("循环修改 开始 count=" + userList.Count.ToString("0 000"));<system.net>
  294.     <connectionManagement>
  295.         <add address="*" maxconnection="1200" />
  296.     </connectionManagement>
  297. </system.net>foreach (SysUser user in userList)<system.net>
  298.     <connectionManagement>
  299.         <add address="*" maxconnection="1200" />
  300.     </connectionManagement>
  301. </system.net>{<system.net>
  302.     <connectionManagement>
  303.         <add address="*" maxconnection="1200" />
  304.     </connectionManagement>
  305. </system.net>    user.Remark = "测试修改用户" + rnd.Next(0, 10000);<system.net>
  306.     <connectionManagement>
  307.         <add address="*" maxconnection="1200" />
  308.     </connectionManagement>
  309. </system.net>    user.UpdateUserid = "1";<system.net>
  310.     <connectionManagement>
  311.         <add address="*" maxconnection="1200" />
  312.     </connectionManagement>
  313. </system.net>    user.UpdateTime = DateTime.Now;<system.net>
  314.     <connectionManagement>
  315.         <add address="*" maxconnection="1200" />
  316.     </connectionManagement>
  317. </system.net>}<system.net>
  318.     <connectionManagement>
  319.         <add address="*" maxconnection="1200" />
  320.     </connectionManagement>
  321. </system.net>DateTime dt = DateTime.Now;<system.net>
  322.     <connectionManagement>
  323.         <add address="*" maxconnection="1200" />
  324.     </connectionManagement>
  325. </system.net>try<system.net>
  326.     <connectionManagement>
  327.         <add address="*" maxconnection="1200" />
  328.     </connectionManagement>
  329. </system.net>{<system.net>
  330.     <connectionManagement>
  331.         <add address="*" maxconnection="1200" />
  332.     </connectionManagement>
  333. </system.net>    _db.Ado.BeginTran();<system.net>
  334.     <connectionManagement>
  335.         <add address="*" maxconnection="1200" />
  336.     </connectionManagement>
  337. </system.net>    foreach (SysUser user in userList)<system.net>
  338.     <connectionManagement>
  339.         <add address="*" maxconnection="1200" />
  340.     </connectionManagement>
  341. </system.net>    {<system.net>
  342.     <connectionManagement>
  343.         <add address="*" maxconnection="1200" />
  344.     </connectionManagement>
  345. </system.net>        _db.Updateable(user).ExecuteCommand();<system.net>
  346.     <connectionManagement>
  347.         <add address="*" maxconnection="1200" />
  348.     </connectionManagement>
  349. </system.net>    }<system.net>
  350.     <connectionManagement>
  351.         <add address="*" maxconnection="1200" />
  352.     </connectionManagement>
  353. </system.net>    _db.Ado.CommitTran();<system.net>
  354.     <connectionManagement>
  355.         <add address="*" maxconnection="1200" />
  356.     </connectionManagement>
  357. </system.net>}<system.net>
  358.     <connectionManagement>
  359.         <add address="*" maxconnection="1200" />
  360.     </connectionManagement>
  361. </system.net>catch (Exception ex)<system.net>
  362.     <connectionManagement>
  363.         <add address="*" maxconnection="1200" />
  364.     </connectionManagement>
  365. </system.net>{<system.net>
  366.     <connectionManagement>
  367.         <add address="*" maxconnection="1200" />
  368.     </connectionManagement>
  369. </system.net>    _db.Ado.RollbackTran();<system.net>
  370.     <connectionManagement>
  371.         <add address="*" maxconnection="1200" />
  372.     </connectionManagement>
  373. </system.net>    throw ex;<system.net>
  374.     <connectionManagement>
  375.         <add address="*" maxconnection="1200" />
  376.     </connectionManagement>
  377. </system.net>}<system.net>
  378.     <connectionManagement>
  379.         <add address="*" maxconnection="1200" />
  380.     </connectionManagement>
  381. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  382.     <connectionManagement>
  383.         <add address="*" maxconnection="1200" />
  384.     </connectionManagement>
  385. </system.net>Log("循环修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试循环添加        private void button6_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  386.     <connectionManagement>
  387.         <add address="*" maxconnection="1200" />
  388.     </connectionManagement>
  389. </system.net>List userList = new List();<system.net>
  390.     <connectionManagement>
  391.         <add address="*" maxconnection="1200" />
  392.     </connectionManagement>
  393. </system.net>for (int i = 1; i             {<system.net>
  394.     <connectionManagement>
  395.         <add address="*" maxconnection="1200" />
  396.     </connectionManagement>
  397. </system.net>_db.Queryable().Where(t => t.Id == 1).ToList(); //预热<system.net>
  398.     <connectionManagement>
  399.         <add address="*" maxconnection="1200" />
  400.     </connectionManagement>
  401. </system.net>Log("查询 开始");<system.net>
  402.     <connectionManagement>
  403.         <add address="*" maxconnection="1200" />
  404.     </connectionManagement>
  405. </system.net>DateTime dt = DateTime.Now;<system.net>
  406.     <connectionManagement>
  407.         <add address="*" maxconnection="1200" />
  408.     </connectionManagement>
  409. </system.net>for (int i = 0; i < 10; i++)<system.net>
  410.     <connectionManagement>
  411.         <add address="*" maxconnection="1200" />
  412.     </connectionManagement>
  413. </system.net>{<system.net>
  414.     <connectionManagement>
  415.         <add address="*" maxconnection="1200" />
  416.     </connectionManagement>
  417. </system.net>    List userList = _db.Queryable().Where(t => t.Id > 20 && t.RealName.Contains("测试")).OrderBy(t => t.CreateTime, OrderByType.Desc).OrderBy(t => t.Id).ToList();<system.net>
  418.     <connectionManagement>
  419.         <add address="*" maxconnection="1200" />
  420.     </connectionManagement>
  421. </system.net>    Log("查询结果 count=" + userList.Count.ToString());<system.net>
  422.     <connectionManagement>
  423.         <add address="*" maxconnection="1200" />
  424.     </connectionManagement>
  425. </system.net>}<system.net>
  426.     <connectionManagement>
  427.         <add address="*" maxconnection="1200" />
  428.     </connectionManagement>
  429. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  430.     <connectionManagement>
  431.         <add address="*" maxconnection="1200" />
  432.     </connectionManagement>
  433. </system.net>Log("查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 分页查询        private void button8_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  434.     <connectionManagement>
  435.         <add address="*" maxconnection="1200" />
  436.     </connectionManagement>
  437. </system.net>_db.Queryable().Where(t => t.Id == 1).ToList(); //预热<system.net>
  438.     <connectionManagement>
  439.         <add address="*" maxconnection="1200" />
  440.     </connectionManagement>
  441. </system.net>Log("分页查询 开始");<system.net>
  442.     <connectionManagement>
  443.         <add address="*" maxconnection="1200" />
  444.     </connectionManagement>
  445. </system.net>DateTime dt = DateTime.Now;<system.net>
  446.     <connectionManagement>
  447.         <add address="*" maxconnection="1200" />
  448.     </connectionManagement>
  449. </system.net>for (int i = 0; i < 10; i++)<system.net>
  450.     <connectionManagement>
  451.         <add address="*" maxconnection="1200" />
  452.     </connectionManagement>
  453. </system.net>{<system.net>
  454.     <connectionManagement>
  455.         <add address="*" maxconnection="1200" />
  456.     </connectionManagement>
  457. </system.net>    int total = _db.Queryable().Count();<system.net>
  458.     <connectionManagement>
  459.         <add address="*" maxconnection="1200" />
  460.     </connectionManagement>
  461. </system.net>    int pageSize = 100;<system.net>
  462.     <connectionManagement>
  463.         <add address="*" maxconnection="1200" />
  464.     </connectionManagement>
  465. </system.net>    int pageCount = (total - 1) / pageSize + 1;<system.net>
  466.     <connectionManagement>
  467.         <add address="*" maxconnection="1200" />
  468.     </connectionManagement>
  469. </system.net>    List userList = new List();<system.net>
  470.     <connectionManagement>
  471.         <add address="*" maxconnection="1200" />
  472.     </connectionManagement>
  473. </system.net>    for (int page = 1; page  t.Id > 20 && t.RealName.Contains("测试")).OrderBy(t => t.CreateTime, OrderByType.Desc).OrderBy(t => t.Id).ToPageList(page, pageSize));<system.net>
  474.     <connectionManagement>
  475.         <add address="*" maxconnection="1200" />
  476.     </connectionManagement>
  477. </system.net>    }<system.net>
  478.     <connectionManagement>
  479.         <add address="*" maxconnection="1200" />
  480.     </connectionManagement>
  481. </system.net>    Log("分页查询结果 count=" + userList.Count.ToString());<system.net>
  482.     <connectionManagement>
  483.         <add address="*" maxconnection="1200" />
  484.     </connectionManagement>
  485. </system.net>}<system.net>
  486.     <connectionManagement>
  487.         <add address="*" maxconnection="1200" />
  488.     </connectionManagement>
  489. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  490.     <connectionManagement>
  491.         <add address="*" maxconnection="1200" />
  492.     </connectionManagement>
  493. </system.net>Log("分页查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发查询        private void button11_Click(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            RunTask(() =>            {<system.net>
  494.     <connectionManagement>
  495.         <add address="*" maxconnection="1200" />
  496.     </connectionManagement>
  497. </system.net>Log("并发查询 开始");<system.net>
  498.     <connectionManagement>
  499.         <add address="*" maxconnection="1200" />
  500.     </connectionManagement>
  501. </system.net>DateTime dt = DateTime.Now;<system.net>
  502.     <connectionManagement>
  503.         <add address="*" maxconnection="1200" />
  504.     </connectionManagement>
  505. </system.net>List tasks = new List();<system.net>
  506.     <connectionManagement>
  507.         <add address="*" maxconnection="1200" />
  508.     </connectionManagement>
  509. </system.net>for (int i = 0; i < 200; i++)<system.net>
  510.     <connectionManagement>
  511.         <add address="*" maxconnection="1200" />
  512.     </connectionManagement>
  513. </system.net>{<system.net>
  514.     <connectionManagement>
  515.         <add address="*" maxconnection="1200" />
  516.     </connectionManagement>
  517. </system.net>    Task task = RunTask(() =><system.net>
  518.     <connectionManagement>
  519.         <add address="*" maxconnection="1200" />
  520.     </connectionManagement>
  521. </system.net>    {<system.net>
  522.     <connectionManagement>
  523.         <add address="*" maxconnection="1200" />
  524.     </connectionManagement>
  525. </system.net>        List userList = _db.Queryable().Where(t => t.Id > 20 && t.RealName.Contains("测试")).OrderBy(t => t.CreateTime, OrderByType.Desc).OrderBy(t => t.Id).ToList();<system.net>
  526.     <connectionManagement>
  527.         <add address="*" maxconnection="1200" />
  528.     </connectionManagement>
  529. </system.net>        Log("查询结果 count=" + userList.Count.ToString());<system.net>
  530.     <connectionManagement>
  531.         <add address="*" maxconnection="1200" />
  532.     </connectionManagement>
  533. </system.net>    });<system.net>
  534.     <connectionManagement>
  535.         <add address="*" maxconnection="1200" />
  536.     </connectionManagement>
  537. </system.net>    tasks.Add(task);<system.net>
  538.     <connectionManagement>
  539.         <add address="*" maxconnection="1200" />
  540.     </connectionManagement>
  541. </system.net>}<system.net>
  542.     <connectionManagement>
  543.         <add address="*" maxconnection="1200" />
  544.     </connectionManagement>
  545. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  546.     <connectionManagement>
  547.         <add address="*" maxconnection="1200" />
  548.     </connectionManagement>
  549. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  550.     <connectionManagement>
  551.         <add address="*" maxconnection="1200" />
  552.     </connectionManagement>
  553. </system.net>Log("并发查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发插入        private void button12_Click(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            RunTask(() =>            {<system.net>
  554.     <connectionManagement>
  555.         <add address="*" maxconnection="1200" />
  556.     </connectionManagement>
  557. </system.net>List userList = new List();<system.net>
  558.     <connectionManagement>
  559.         <add address="*" maxconnection="1200" />
  560.     </connectionManagement>
  561. </system.net>for (int i = 1; i<system.net>
  562.     <connectionManagement>
  563.         <add address="*" maxconnection="1200" />
  564.     </connectionManagement>
  565. </system.net>     {<system.net>
  566.     <connectionManagement>
  567.         <add address="*" maxconnection="1200" />
  568.     </connectionManagement>
  569. </system.net>        _db.Insertable(user).ExecuteCommand();<system.net>
  570.     <connectionManagement>
  571.         <add address="*" maxconnection="1200" />
  572.     </connectionManagement>
  573. </system.net>    }, item);<system.net>
  574.     <connectionManagement>
  575.         <add address="*" maxconnection="1200" />
  576.     </connectionManagement>
  577. </system.net>    tasks.Add(task);<system.net>
  578.     <connectionManagement>
  579.         <add address="*" maxconnection="1200" />
  580.     </connectionManagement>
  581. </system.net>}<system.net>
  582.     <connectionManagement>
  583.         <add address="*" maxconnection="1200" />
  584.     </connectionManagement>
  585. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  586.     <connectionManagement>
  587.         <add address="*" maxconnection="1200" />
  588.     </connectionManagement>
  589. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  590.     <connectionManagement>
  591.         <add address="*" maxconnection="1200" />
  592.     </connectionManagement>
  593. </system.net>Log("并发插入 完成,耗时:" + time + "秒");            });        }        #endregion    }}
复制代码
8. Fast.Framework测试代码

(.NET6,Dapper.LiteSql .NET Framework 4.5.2和.NET6各写了一份测试,实测插入和更新速度差不多,查询.NET6会稍微快一些)
  1. using Fast.Framework;using Fast.Framework.Interfaces;using Fast.Framework.Models;using Fast.Framework.Extensions;using Fast.Framework.Aop;using Models;namespace FastFrameworkDemo{    public partial class Form1 : Form    {        #region 变量        private Random _rnd = new Random();        private int _count = 10000;        private bool _showSqlLog = false;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>await GetDbContext().Query().CountAsync();<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>Log("预热完成");            });        }        #endregion        #region Log        private void Log(string log)        {            if (!this.IsDisposed)            {<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>if (this.InvokeRequired)<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>{<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>    this.BeginInvoke(new Action(() =><system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>    {<system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>        textBox1.AppendText(msg);<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>    }));<system.net>
  38.     <connectionManagement>
  39.         <add address="*" maxconnection="1200" />
  40.     </connectionManagement>
  41. </system.net>}<system.net>
  42.     <connectionManagement>
  43.         <add address="*" maxconnection="1200" />
  44.     </connectionManagement>
  45. </system.net>else<system.net>
  46.     <connectionManagement>
  47.         <add address="*" maxconnection="1200" />
  48.     </connectionManagement>
  49. </system.net>{<system.net>
  50.     <connectionManagement>
  51.         <add address="*" maxconnection="1200" />
  52.     </connectionManagement>
  53. </system.net>    textBox1.AppendText(msg);<system.net>
  54.     <connectionManagement>
  55.         <add address="*" maxconnection="1200" />
  56.     </connectionManagement>
  57. </system.net>}            }        }        #endregion        #region 清空输出框        private void button10_Click(object sender, EventArgs e)        {            textBox1.Text = string.Empty;        }        #endregion        #region RunTask        private Task RunTask(Action action)        {            return Task.Run(() =>            {<system.net>
  58.     <connectionManagement>
  59.         <add address="*" maxconnection="1200" />
  60.     </connectionManagement>
  61. </system.net>try<system.net>
  62.     <connectionManagement>
  63.         <add address="*" maxconnection="1200" />
  64.     </connectionManagement>
  65. </system.net>{<system.net>
  66.     <connectionManagement>
  67.         <add address="*" maxconnection="1200" />
  68.     </connectionManagement>
  69. </system.net>    action();<system.net>
  70.     <connectionManagement>
  71.         <add address="*" maxconnection="1200" />
  72.     </connectionManagement>
  73. </system.net>}<system.net>
  74.     <connectionManagement>
  75.         <add address="*" maxconnection="1200" />
  76.     </connectionManagement>
  77. </system.net>catch (Exception ex)<system.net>
  78.     <connectionManagement>
  79.         <add address="*" maxconnection="1200" />
  80.     </connectionManagement>
  81. </system.net>{<system.net>
  82.     <connectionManagement>
  83.         <add address="*" maxconnection="1200" />
  84.     </connectionManagement>
  85. </system.net>    Log(ex.ToString());<system.net>
  86.     <connectionManagement>
  87.         <add address="*" maxconnection="1200" />
  88.     </connectionManagement>
  89. </system.net>}            });        }        private Task RunTask(Action action, T t)        {            return Task.Factory.StartNew(obj =>            {<system.net>
  90.     <connectionManagement>
  91.         <add address="*" maxconnection="1200" />
  92.     </connectionManagement>
  93. </system.net>try<system.net>
  94.     <connectionManagement>
  95.         <add address="*" maxconnection="1200" />
  96.     </connectionManagement>
  97. </system.net>{<system.net>
  98.     <connectionManagement>
  99.         <add address="*" maxconnection="1200" />
  100.     </connectionManagement>
  101. </system.net>    action((T)obj);<system.net>
  102.     <connectionManagement>
  103.         <add address="*" maxconnection="1200" />
  104.     </connectionManagement>
  105. </system.net>}<system.net>
  106.     <connectionManagement>
  107.         <add address="*" maxconnection="1200" />
  108.     </connectionManagement>
  109. </system.net>catch (Exception ex)<system.net>
  110.     <connectionManagement>
  111.         <add address="*" maxconnection="1200" />
  112.     </connectionManagement>
  113. </system.net>{<system.net>
  114.     <connectionManagement>
  115.         <add address="*" maxconnection="1200" />
  116.     </connectionManagement>
  117. </system.net>    Log(ex.ToString());<system.net>
  118.     <connectionManagement>
  119.         <add address="*" maxconnection="1200" />
  120.     </connectionManagement>
  121. </system.net>}            }, t);        }        #endregion        #region GetDbContext        private IDbContext GetDbContext()        {            IDbContext _db = new DbContext(new List() {<system.net>
  122.     <connectionManagement>
  123.         <add address="*" maxconnection="1200" />
  124.     </connectionManagement>
  125. </system.net>    new DbOptions()<system.net>
  126.     <connectionManagement>
  127.         <add address="*" maxconnection="1200" />
  128.     </connectionManagement>
  129. </system.net>    {<system.net>
  130.     <connectionManagement>
  131.         <add address="*" maxconnection="1200" />
  132.     </connectionManagement>
  133. </system.net>      DbId = "1",<system.net>
  134.     <connectionManagement>
  135.         <add address="*" maxconnection="1200" />
  136.     </connectionManagement>
  137. </system.net>      DbType = DbType.MySQL,<system.net>
  138.     <connectionManagement>
  139.         <add address="*" maxconnection="1200" />
  140.     </connectionManagement>
  141. </system.net>      ProviderName = "MySqlConnector",<system.net>
  142.     <connectionManagement>
  143.         <add address="*" maxconnection="1200" />
  144.     </connectionManagement>
  145. </system.net>      FactoryName = "MySqlConnector.MySqlConnectorFactory,MySqlConnector",<system.net>
  146.     <connectionManagement>
  147.         <add address="*" maxconnection="1200" />
  148.     </connectionManagement>
  149. </system.net>      ConnectionStrings = "server=localhost;database=fast_framework_test;user id=root;password=123456;character set=utf8mb4;"<system.net>
  150.     <connectionManagement>
  151.         <add address="*" maxconnection="1200" />
  152.     </connectionManagement>
  153. </system.net>    }<system.net>
  154.     <connectionManagement>
  155.         <add address="*" maxconnection="1200" />
  156.     </connectionManagement>
  157. </system.net>});            if (_showSqlLog)            {<system.net>
  158.     <connectionManagement>
  159.         <add address="*" maxconnection="1200" />
  160.     </connectionManagement>
  161. </system.net>_db.Aop.DbLog = (sql, dp) =><system.net>
  162.     <connectionManagement>
  163.         <add address="*" maxconnection="1200" />
  164.     </connectionManagement>
  165. </system.net>{<system.net>
  166.     <connectionManagement>
  167.         <add address="*" maxconnection="1200" />
  168.     </connectionManagement>
  169. </system.net>    Console.WriteLine($"执行Sql:{sql}");<system.net>
  170.     <connectionManagement>
  171.         <add address="*" maxconnection="1200" />
  172.     </connectionManagement>
  173. </system.net>    if (dp != null)<system.net>
  174.     <connectionManagement>
  175.         <add address="*" maxconnection="1200" />
  176.     </connectionManagement>
  177. </system.net>    {<system.net>
  178.     <connectionManagement>
  179.         <add address="*" maxconnection="1200" />
  180.     </connectionManagement>
  181. </system.net>        foreach (var item in dp)<system.net>
  182.     <connectionManagement>
  183.         <add address="*" maxconnection="1200" />
  184.     </connectionManagement>
  185. </system.net>        {<system.net>
  186.     <connectionManagement>
  187.         <add address="*" maxconnection="1200" />
  188.     </connectionManagement>
  189. </system.net>            Console.WriteLine($"参数名称:{item.ParameterName} 参数值:{item.Value}");<system.net>
  190.     <connectionManagement>
  191.         <add address="*" maxconnection="1200" />
  192.     </connectionManagement>
  193. </system.net>        }<system.net>
  194.     <connectionManagement>
  195.         <add address="*" maxconnection="1200" />
  196.     </connectionManagement>
  197. </system.net>    }<system.net>
  198.     <connectionManagement>
  199.         <add address="*" maxconnection="1200" />
  200.     </connectionManagement>
  201. </system.net>};            }            return _db;        }        #endregion        #region 删除        private void button1_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  202.     <connectionManagement>
  203.         <add address="*" maxconnection="1200" />
  204.     </connectionManagement>
  205. </system.net>Log("删除 开始");<system.net>
  206.     <connectionManagement>
  207.         <add address="*" maxconnection="1200" />
  208.     </connectionManagement>
  209. </system.net>await GetDbContext().Delete().Where(t => t.Id > 20).ExceuteAsync();<system.net>
  210.     <connectionManagement>
  211.         <add address="*" maxconnection="1200" />
  212.     </connectionManagement>
  213. </system.net>Log("删除 完成");            });        }        #endregion        #region 批量修改        private void button2_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  214.     <connectionManagement>
  215.         <add address="*" maxconnection="1200" />
  216.     </connectionManagement>
  217. </system.net>List userList = await GetDbContext().Query().Where(t => t.Id > 20).ToListAsync();<system.net>
  218.     <connectionManagement>
  219.         <add address="*" maxconnection="1200" />
  220.     </connectionManagement>
  221. </system.net>Log("批量修改 开始 count=" + userList.Count);<system.net>
  222.     <connectionManagement>
  223.         <add address="*" maxconnection="1200" />
  224.     </connectionManagement>
  225. </system.net>DateTime dt = DateTime.Now;<system.net>
  226.     <connectionManagement>
  227.         <add address="*" maxconnection="1200" />
  228.     </connectionManagement>
  229. </system.net>               foreach (SysUser user in userList)<system.net>
  230.     <connectionManagement>
  231.         <add address="*" maxconnection="1200" />
  232.     </connectionManagement>
  233. </system.net>    {<system.net>
  234.     <connectionManagement>
  235.         <add address="*" maxconnection="1200" />
  236.     </connectionManagement>
  237. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  238.     <connectionManagement>
  239.         <add address="*" maxconnection="1200" />
  240.     </connectionManagement>
  241. </system.net>        user.UpdateUserid = "1";<system.net>
  242.     <connectionManagement>
  243.         <add address="*" maxconnection="1200" />
  244.     </connectionManagement>
  245. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  246.     <connectionManagement>
  247.         <add address="*" maxconnection="1200" />
  248.     </connectionManagement>
  249. </system.net>    }<system.net>
  250.     <connectionManagement>
  251.         <add address="*" maxconnection="1200" />
  252.     </connectionManagement>
  253. </system.net>    await GetDbContext().Update(userList).ExceuteAsync();<system.net>
  254.     <connectionManagement>
  255.         <add address="*" maxconnection="1200" />
  256.     </connectionManagement>
  257. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  258.     <connectionManagement>
  259.         <add address="*" maxconnection="1200" />
  260.     </connectionManagement>
  261. </system.net>Log("批量修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 批量添加        private void button3_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  262.     <connectionManagement>
  263.         <add address="*" maxconnection="1200" />
  264.     </connectionManagement>
  265. </system.net>List userList = new List();<system.net>
  266.     <connectionManagement>
  267.         <add address="*" maxconnection="1200" />
  268.     </connectionManagement>
  269. </system.net>for (int i = 1; i             {<system.net>
  270.     <connectionManagement>
  271.         <add address="*" maxconnection="1200" />
  272.     </connectionManagement>
  273. </system.net>List userList = await GetDbContext().Query().Where(t => t.Id > 20).ToListAsync();<system.net>
  274.     <connectionManagement>
  275.         <add address="*" maxconnection="1200" />
  276.     </connectionManagement>
  277. </system.net>Log("循环修改 开始 count=" + userList.Count);<system.net>
  278.     <connectionManagement>
  279.         <add address="*" maxconnection="1200" />
  280.     </connectionManagement>
  281. </system.net>DateTime dt = DateTime.Now;<system.net>
  282.     <connectionManagement>
  283.         <add address="*" maxconnection="1200" />
  284.     </connectionManagement>
  285. </system.net>try<system.net>
  286.     <connectionManagement>
  287.         <add address="*" maxconnection="1200" />
  288.     </connectionManagement>
  289. </system.net>{<system.net>
  290.     <connectionManagement>
  291.         <add address="*" maxconnection="1200" />
  292.     </connectionManagement>
  293. </system.net>    foreach (SysUser user in userList)<system.net>
  294.     <connectionManagement>
  295.         <add address="*" maxconnection="1200" />
  296.     </connectionManagement>
  297. </system.net>    {<system.net>
  298.     <connectionManagement>
  299.         <add address="*" maxconnection="1200" />
  300.     </connectionManagement>
  301. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  302.     <connectionManagement>
  303.         <add address="*" maxconnection="1200" />
  304.     </connectionManagement>
  305. </system.net>        user.UpdateUserid = "1";<system.net>
  306.     <connectionManagement>
  307.         <add address="*" maxconnection="1200" />
  308.     </connectionManagement>
  309. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  310.     <connectionManagement>
  311.         <add address="*" maxconnection="1200" />
  312.     </connectionManagement>
  313. </system.net>    }<system.net>
  314.     <connectionManagement>
  315.         <add address="*" maxconnection="1200" />
  316.     </connectionManagement>
  317. </system.net>    var db = GetDbContext();<system.net>
  318.     <connectionManagement>
  319.         <add address="*" maxconnection="1200" />
  320.     </connectionManagement>
  321. </system.net>    await db.Ado.BeginTranAsync();<system.net>
  322.     <connectionManagement>
  323.         <add address="*" maxconnection="1200" />
  324.     </connectionManagement>
  325. </system.net>    foreach (SysUser user in userList)<system.net>
  326.     <connectionManagement>
  327.         <add address="*" maxconnection="1200" />
  328.     </connectionManagement>
  329. </system.net>    {<system.net>
  330.     <connectionManagement>
  331.         <add address="*" maxconnection="1200" />
  332.     </connectionManagement>
  333. </system.net>        await db.Update(user).ExceuteAsync();<system.net>
  334.     <connectionManagement>
  335.         <add address="*" maxconnection="1200" />
  336.     </connectionManagement>
  337. </system.net>    }<system.net>
  338.     <connectionManagement>
  339.         <add address="*" maxconnection="1200" />
  340.     </connectionManagement>
  341. </system.net>    await db.Ado.CommitTranAsync();<system.net>
  342.     <connectionManagement>
  343.         <add address="*" maxconnection="1200" />
  344.     </connectionManagement>
  345. </system.net>}<system.net>
  346.     <connectionManagement>
  347.         <add address="*" maxconnection="1200" />
  348.     </connectionManagement>
  349. </system.net>catch<system.net>
  350.     <connectionManagement>
  351.         <add address="*" maxconnection="1200" />
  352.     </connectionManagement>
  353. </system.net>{<system.net>
  354.     <connectionManagement>
  355.         <add address="*" maxconnection="1200" />
  356.     </connectionManagement>
  357. </system.net>    //todo:没有rollback?<system.net>
  358.     <connectionManagement>
  359.         <add address="*" maxconnection="1200" />
  360.     </connectionManagement>
  361. </system.net>    throw;<system.net>
  362.     <connectionManagement>
  363.         <add address="*" maxconnection="1200" />
  364.     </connectionManagement>
  365. </system.net>}<system.net>
  366.     <connectionManagement>
  367.         <add address="*" maxconnection="1200" />
  368.     </connectionManagement>
  369. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  370.     <connectionManagement>
  371.         <add address="*" maxconnection="1200" />
  372.     </connectionManagement>
  373. </system.net>Log("循环修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 循环添加        private void button5_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  374.     <connectionManagement>
  375.         <add address="*" maxconnection="1200" />
  376.     </connectionManagement>
  377. </system.net>List userList = new List();<system.net>
  378.     <connectionManagement>
  379.         <add address="*" maxconnection="1200" />
  380.     </connectionManagement>
  381. </system.net>for (int i = 1; i             {<system.net>
  382.     <connectionManagement>
  383.         <add address="*" maxconnection="1200" />
  384.     </connectionManagement>
  385. </system.net>Log("查询 开始");<system.net>
  386.     <connectionManagement>
  387.         <add address="*" maxconnection="1200" />
  388.     </connectionManagement>
  389. </system.net>DateTime dt = DateTime.Now;<system.net>
  390.     <connectionManagement>
  391.         <add address="*" maxconnection="1200" />
  392.     </connectionManagement>
  393. </system.net>var db = GetDbContext();<system.net>
  394.     <connectionManagement>
  395.         <add address="*" maxconnection="1200" />
  396.     </connectionManagement>
  397. </system.net>for (int i = 0; i < 10; i++)<system.net>
  398.     <connectionManagement>
  399.         <add address="*" maxconnection="1200" />
  400.     </connectionManagement>
  401. </system.net>{<system.net>
  402.     <connectionManagement>
  403.         <add address="*" maxconnection="1200" />
  404.     </connectionManagement>
  405. </system.net>    List userList = await db.Query()<system.net>
  406.     <connectionManagement>
  407.         <add address="*" maxconnection="1200" />
  408.     </connectionManagement>
  409. </system.net>        .Where(t => t.Id > 20 && t.RealName.Contains("%测试%"))<system.net>
  410.     <connectionManagement>
  411.         <add address="*" maxconnection="1200" />
  412.     </connectionManagement>
  413. </system.net>        .OrderBy("create_time", "desc")<system.net>
  414.     <connectionManagement>
  415.         <add address="*" maxconnection="1200" />
  416.     </connectionManagement>
  417. </system.net>        .OrderBy("id", "asc").ToListAsync();<system.net>
  418.     <connectionManagement>
  419.         <add address="*" maxconnection="1200" />
  420.     </connectionManagement>
  421. </system.net>    Log("查询结果 count=" + userList.Count.ToString());<system.net>
  422.     <connectionManagement>
  423.         <add address="*" maxconnection="1200" />
  424.     </connectionManagement>
  425. </system.net>}<system.net>
  426.     <connectionManagement>
  427.         <add address="*" maxconnection="1200" />
  428.     </connectionManagement>
  429. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  430.     <connectionManagement>
  431.         <add address="*" maxconnection="1200" />
  432.     </connectionManagement>
  433. </system.net>Log("查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 分页查询        private void button7_Click(object sender, EventArgs e)        {            Log("尚未实现");        }        #endregion        #region 并发查询        private void button8_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  434.     <connectionManagement>
  435.         <add address="*" maxconnection="1200" />
  436.     </connectionManagement>
  437. </system.net>ThreadPool.SetMaxThreads(1000, 1000);<system.net>
  438.     <connectionManagement>
  439.         <add address="*" maxconnection="1200" />
  440.     </connectionManagement>
  441. </system.net>ThreadPool.SetMinThreads(200, 200);<system.net>
  442.     <connectionManagement>
  443.         <add address="*" maxconnection="1200" />
  444.     </connectionManagement>
  445. </system.net>Log("并发查询 开始");<system.net>
  446.     <connectionManagement>
  447.         <add address="*" maxconnection="1200" />
  448.     </connectionManagement>
  449. </system.net>DateTime dt = DateTime.Now;<system.net>
  450.     <connectionManagement>
  451.         <add address="*" maxconnection="1200" />
  452.     </connectionManagement>
  453. </system.net>List tasks = new List();<system.net>
  454.     <connectionManagement>
  455.         <add address="*" maxconnection="1200" />
  456.     </connectionManagement>
  457. </system.net>for (int i = 0; i < 200; i++)<system.net>
  458.     <connectionManagement>
  459.         <add address="*" maxconnection="1200" />
  460.     </connectionManagement>
  461. </system.net>{<system.net>
  462.     <connectionManagement>
  463.         <add address="*" maxconnection="1200" />
  464.     </connectionManagement>
  465. </system.net>    Task task = RunTask(async () =><system.net>
  466.     <connectionManagement>
  467.         <add address="*" maxconnection="1200" />
  468.     </connectionManagement>
  469. </system.net>    {<system.net>
  470.     <connectionManagement>
  471.         <add address="*" maxconnection="1200" />
  472.     </connectionManagement>
  473. </system.net>        List userList = await GetDbContext().Query()<system.net>
  474.     <connectionManagement>
  475.         <add address="*" maxconnection="1200" />
  476.     </connectionManagement>
  477. </system.net>            .Where(t => t.Id > 20 && t.RealName.Contains("%测试%"))<system.net>
  478.     <connectionManagement>
  479.         <add address="*" maxconnection="1200" />
  480.     </connectionManagement>
  481. </system.net>            .OrderBy("create_time", "desc")<system.net>
  482.     <connectionManagement>
  483.         <add address="*" maxconnection="1200" />
  484.     </connectionManagement>
  485. </system.net>            .OrderBy("id", "asc").ToListAsync();<system.net>
  486.     <connectionManagement>
  487.         <add address="*" maxconnection="1200" />
  488.     </connectionManagement>
  489. </system.net>        Log("查询结果 count=" + userList.Count.ToString());<system.net>
  490.     <connectionManagement>
  491.         <add address="*" maxconnection="1200" />
  492.     </connectionManagement>
  493. </system.net>    });<system.net>
  494.     <connectionManagement>
  495.         <add address="*" maxconnection="1200" />
  496.     </connectionManagement>
  497. </system.net>    tasks.Add(task);<system.net>
  498.     <connectionManagement>
  499.         <add address="*" maxconnection="1200" />
  500.     </connectionManagement>
  501. </system.net>}<system.net>
  502.     <connectionManagement>
  503.         <add address="*" maxconnection="1200" />
  504.     </connectionManagement>
  505. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  506.     <connectionManagement>
  507.         <add address="*" maxconnection="1200" />
  508.     </connectionManagement>
  509. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  510.     <connectionManagement>
  511.         <add address="*" maxconnection="1200" />
  512.     </connectionManagement>
  513. </system.net>Log("并发查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发插入        private void button9_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  514.     <connectionManagement>
  515.         <add address="*" maxconnection="1200" />
  516.     </connectionManagement>
  517. </system.net>ThreadPool.SetMaxThreads(1000, 1000);<system.net>
  518.     <connectionManagement>
  519.         <add address="*" maxconnection="1200" />
  520.     </connectionManagement>
  521. </system.net>ThreadPool.SetMinThreads(200, 200);<system.net>
  522.     <connectionManagement>
  523.         <add address="*" maxconnection="1200" />
  524.     </connectionManagement>
  525. </system.net>List userList = new List();<system.net>
  526.     <connectionManagement>
  527.         <add address="*" maxconnection="1200" />
  528.     </connectionManagement>
  529. </system.net>for (int i = 1; i<system.net>
  530.     <connectionManagement>
  531.         <add address="*" maxconnection="1200" />
  532.     </connectionManagement>
  533. </system.net>     {<system.net>
  534.     <connectionManagement>
  535.         <add address="*" maxconnection="1200" />
  536.     </connectionManagement>
  537. </system.net>        await GetDbContext().Insert(user).ExceuteAsync();<system.net>
  538.     <connectionManagement>
  539.         <add address="*" maxconnection="1200" />
  540.     </connectionManagement>
  541. </system.net>    }, item);<system.net>
  542.     <connectionManagement>
  543.         <add address="*" maxconnection="1200" />
  544.     </connectionManagement>
  545. </system.net>    tasks.Add(task);<system.net>
  546.     <connectionManagement>
  547.         <add address="*" maxconnection="1200" />
  548.     </connectionManagement>
  549. </system.net>}<system.net>
  550.     <connectionManagement>
  551.         <add address="*" maxconnection="1200" />
  552.     </connectionManagement>
  553. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  554.     <connectionManagement>
  555.         <add address="*" maxconnection="1200" />
  556.     </connectionManagement>
  557. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  558.     <connectionManagement>
  559.         <add address="*" maxconnection="1200" />
  560.     </connectionManagement>
  561. </system.net>Log("并发插入 完成,耗时:" + time + "秒");            });        }        #endregion    }}
复制代码
9. Dapper.LiteSql

(还有一个不依赖Dapper的版本,那个插入和更新性能差不多,查询因为用的是反射,大约慢50%)
  1. public class LiteSqlFactory
  2. {
  3.     #region 变量
  4.     private static ILiteSqlClient _liteSqlClient = new LiteSqlClient(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString(), DBType.MySQL, new MySQLProvider());
  5.     #endregion
  6.     #region 获取 ISession
  7.     /// <summary>
  8.     /// 获取 ISession
  9.     /// </summary>
  10.     /// <param name="splitTableMapping">分表映射</param>
  11.     public static ISession GetSession(SplitTableMapping splitTableMapping = null)
  12.     {
  13.         return _liteSqlClient.GetSession(splitTableMapping);
  14.     }
  15.     #endregion
  16.     #region 获取 ISession (异步)
  17.     /// <summary>
  18.     /// 获取 ISession (异步)
  19.     /// </summary>
  20.     /// <param name="splitTableMapping">分表映射</param>
  21.     public static async Task<ISession> GetSessionAsync(SplitTableMapping splitTableMapping = null)
  22.     {
  23.         return await _liteSqlClient.GetSessionAsync(splitTableMapping);
  24.     }
  25.     #endregion
  26. }
复制代码
  1. using DAL;using Dapper.LiteSql;using Models;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;using Utils;namespace PerformanceTest{    public partial class Form1 : Form    {        #region 变量        private BsOrderDal m_BsOrderDal = ServiceHelper.Get();        private SysUserDal m_SysUserDal = ServiceHelper.Get();        private Random _rnd = new Random();        private int _count = 10000;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>LiteSqlFactory.GetSession(); //预热<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>Log("预热完成");            });        }        #endregion        #region Log        private void Log(string log)        {            if (!this.IsDisposed)            {<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>if (this.InvokeRequired)<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>{<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>    this.BeginInvoke(new Action(() =><system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>    {<system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>        textBox1.AppendText(msg);<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>    }));<system.net>
  38.     <connectionManagement>
  39.         <add address="*" maxconnection="1200" />
  40.     </connectionManagement>
  41. </system.net>}<system.net>
  42.     <connectionManagement>
  43.         <add address="*" maxconnection="1200" />
  44.     </connectionManagement>
  45. </system.net>else<system.net>
  46.     <connectionManagement>
  47.         <add address="*" maxconnection="1200" />
  48.     </connectionManagement>
  49. </system.net>{<system.net>
  50.     <connectionManagement>
  51.         <add address="*" maxconnection="1200" />
  52.     </connectionManagement>
  53. </system.net>    textBox1.AppendText(msg);<system.net>
  54.     <connectionManagement>
  55.         <add address="*" maxconnection="1200" />
  56.     </connectionManagement>
  57. </system.net>}            }        }        #endregion        #region 清空输出框        private void button10_Click(object sender, EventArgs e)        {            textBox1.Text = string.Empty;        }        #endregion        #region RunTask        private Task RunTask(Action action)        {            return Task.Run(() =>            {<system.net>
  58.     <connectionManagement>
  59.         <add address="*" maxconnection="1200" />
  60.     </connectionManagement>
  61. </system.net>try<system.net>
  62.     <connectionManagement>
  63.         <add address="*" maxconnection="1200" />
  64.     </connectionManagement>
  65. </system.net>{<system.net>
  66.     <connectionManagement>
  67.         <add address="*" maxconnection="1200" />
  68.     </connectionManagement>
  69. </system.net>    action();<system.net>
  70.     <connectionManagement>
  71.         <add address="*" maxconnection="1200" />
  72.     </connectionManagement>
  73. </system.net>}<system.net>
  74.     <connectionManagement>
  75.         <add address="*" maxconnection="1200" />
  76.     </connectionManagement>
  77. </system.net>catch (Exception ex)<system.net>
  78.     <connectionManagement>
  79.         <add address="*" maxconnection="1200" />
  80.     </connectionManagement>
  81. </system.net>{<system.net>
  82.     <connectionManagement>
  83.         <add address="*" maxconnection="1200" />
  84.     </connectionManagement>
  85. </system.net>    Log(ex.ToString());<system.net>
  86.     <connectionManagement>
  87.         <add address="*" maxconnection="1200" />
  88.     </connectionManagement>
  89. </system.net>    throw;<system.net>
  90.     <connectionManagement>
  91.         <add address="*" maxconnection="1200" />
  92.     </connectionManagement>
  93. </system.net>}            });        }        private Task RunTask(Action action, T t)        {            return Task.Factory.StartNew(obj =>            {<system.net>
  94.     <connectionManagement>
  95.         <add address="*" maxconnection="1200" />
  96.     </connectionManagement>
  97. </system.net>try<system.net>
  98.     <connectionManagement>
  99.         <add address="*" maxconnection="1200" />
  100.     </connectionManagement>
  101. </system.net>{<system.net>
  102.     <connectionManagement>
  103.         <add address="*" maxconnection="1200" />
  104.     </connectionManagement>
  105. </system.net>    action((T)obj);<system.net>
  106.     <connectionManagement>
  107.         <add address="*" maxconnection="1200" />
  108.     </connectionManagement>
  109. </system.net>}<system.net>
  110.     <connectionManagement>
  111.         <add address="*" maxconnection="1200" />
  112.     </connectionManagement>
  113. </system.net>catch (Exception ex)<system.net>
  114.     <connectionManagement>
  115.         <add address="*" maxconnection="1200" />
  116.     </connectionManagement>
  117. </system.net>{<system.net>
  118.     <connectionManagement>
  119.         <add address="*" maxconnection="1200" />
  120.     </connectionManagement>
  121. </system.net>    Log(ex.ToString());<system.net>
  122.     <connectionManagement>
  123.         <add address="*" maxconnection="1200" />
  124.     </connectionManagement>
  125. </system.net>    throw;<system.net>
  126.     <connectionManagement>
  127.         <add address="*" maxconnection="1200" />
  128.     </connectionManagement>
  129. </system.net>}            }, t);        }        #endregion        #region 删除        private void button5_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  130.     <connectionManagement>
  131.         <add address="*" maxconnection="1200" />
  132.     </connectionManagement>
  133. </system.net>Log("删除 开始");<system.net>
  134.     <connectionManagement>
  135.         <add address="*" maxconnection="1200" />
  136.     </connectionManagement>
  137. </system.net>var session = LiteSqlFactory.GetSession();<system.net>
  138.     <connectionManagement>
  139.         <add address="*" maxconnection="1200" />
  140.     </connectionManagement>
  141. </system.net>session.DeleteByCondition(string.Format("id>=12"));<system.net>
  142.     <connectionManagement>
  143.         <add address="*" maxconnection="1200" />
  144.     </connectionManagement>
  145. </system.net>Log("删除 完成");            });        }        #endregion        #region 测试批量修改        private void button3_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  146.     <connectionManagement>
  147.         <add address="*" maxconnection="1200" />
  148.     </connectionManagement>
  149. </system.net>List userList = m_SysUserDal.GetList("select t.* from sys_user t where t.id > 20");<system.net>
  150.     <connectionManagement>
  151.         <add address="*" maxconnection="1200" />
  152.     </connectionManagement>
  153. </system.net>Log("批量修改 开始 count=" + userList.Count);<system.net>
  154.     <connectionManagement>
  155.         <add address="*" maxconnection="1200" />
  156.     </connectionManagement>
  157. </system.net>DateTime dt = DateTime.Now;<system.net>
  158.     <connectionManagement>
  159.         <add address="*" maxconnection="1200" />
  160.     </connectionManagement>
  161. </system.net>var session = LiteSqlFactory.GetSession();<system.net>
  162.     <connectionManagement>
  163.         <add address="*" maxconnection="1200" />
  164.     </connectionManagement>
  165. </system.net>try<system.net>
  166.     <connectionManagement>
  167.         <add address="*" maxconnection="1200" />
  168.     </connectionManagement>
  169. </system.net>{<system.net>
  170.     <connectionManagement>
  171.         <add address="*" maxconnection="1200" />
  172.     </connectionManagement>
  173. </system.net>    session.OnExecuting = (sql, param) =><system.net>
  174.     <connectionManagement>
  175.         <add address="*" maxconnection="1200" />
  176.     </connectionManagement>
  177. </system.net>    {<system.net>
  178.     <connectionManagement>
  179.         <add address="*" maxconnection="1200" />
  180.     </connectionManagement>
  181. </system.net>        Console.WriteLine(sql); //打印SQL<system.net>
  182.     <connectionManagement>
  183.         <add address="*" maxconnection="1200" />
  184.     </connectionManagement>
  185. </system.net>    };<system.net>
  186.     <connectionManagement>
  187.         <add address="*" maxconnection="1200" />
  188.     </connectionManagement>
  189. </system.net>    session.AttachOld(userList);<system.net>
  190.     <connectionManagement>
  191.         <add address="*" maxconnection="1200" />
  192.     </connectionManagement>
  193. </system.net>    foreach (SysUser user in userList)<system.net>
  194.     <connectionManagement>
  195.         <add address="*" maxconnection="1200" />
  196.     </connectionManagement>
  197. </system.net>    {<system.net>
  198.     <connectionManagement>
  199.         <add address="*" maxconnection="1200" />
  200.     </connectionManagement>
  201. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  202.     <connectionManagement>
  203.         <add address="*" maxconnection="1200" />
  204.     </connectionManagement>
  205. </system.net>        user.UpdateUserid = "1";<system.net>
  206.     <connectionManagement>
  207.         <add address="*" maxconnection="1200" />
  208.     </connectionManagement>
  209. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  210.     <connectionManagement>
  211.         <add address="*" maxconnection="1200" />
  212.     </connectionManagement>
  213. </system.net>    }<system.net>
  214.     <connectionManagement>
  215.         <add address="*" maxconnection="1200" />
  216.     </connectionManagement>
  217. </system.net>    userList.ForEach(item => item.UpdateTime = DateTime.Now);<system.net>
  218.     <connectionManagement>
  219.         <add address="*" maxconnection="1200" />
  220.     </connectionManagement>
  221. </system.net>    session.BeginTransaction();<system.net>
  222.     <connectionManagement>
  223.         <add address="*" maxconnection="1200" />
  224.     </connectionManagement>
  225. </system.net>    session.Update(userList);<system.net>
  226.     <connectionManagement>
  227.         <add address="*" maxconnection="1200" />
  228.     </connectionManagement>
  229. </system.net>    session.CommitTransaction();<system.net>
  230.     <connectionManagement>
  231.         <add address="*" maxconnection="1200" />
  232.     </connectionManagement>
  233. </system.net>}<system.net>
  234.     <connectionManagement>
  235.         <add address="*" maxconnection="1200" />
  236.     </connectionManagement>
  237. </system.net>catch (Exception ex)<system.net>
  238.     <connectionManagement>
  239.         <add address="*" maxconnection="1200" />
  240.     </connectionManagement>
  241. </system.net>{<system.net>
  242.     <connectionManagement>
  243.         <add address="*" maxconnection="1200" />
  244.     </connectionManagement>
  245. </system.net>    session.RollbackTransaction();<system.net>
  246.     <connectionManagement>
  247.         <add address="*" maxconnection="1200" />
  248.     </connectionManagement>
  249. </system.net>    throw;<system.net>
  250.     <connectionManagement>
  251.         <add address="*" maxconnection="1200" />
  252.     </connectionManagement>
  253. </system.net>}<system.net>
  254.     <connectionManagement>
  255.         <add address="*" maxconnection="1200" />
  256.     </connectionManagement>
  257. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  258.     <connectionManagement>
  259.         <add address="*" maxconnection="1200" />
  260.     </connectionManagement>
  261. </system.net>Log("批量修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试批量添加        private void button4_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  262.     <connectionManagement>
  263.         <add address="*" maxconnection="1200" />
  264.     </connectionManagement>
  265. </system.net>List userList = new List();<system.net>
  266.     <connectionManagement>
  267.         <add address="*" maxconnection="1200" />
  268.     </connectionManagement>
  269. </system.net>for (int i = 1; i  Console.WriteLine(s); //打印SQL<system.net>
  270.     <connectionManagement>
  271.         <add address="*" maxconnection="1200" />
  272.     </connectionManagement>
  273. </system.net>    session.BeginTransaction();<system.net>
  274.     <connectionManagement>
  275.         <add address="*" maxconnection="1200" />
  276.     </connectionManagement>
  277. </system.net>    session.Insert(userList);<system.net>
  278.     <connectionManagement>
  279.         <add address="*" maxconnection="1200" />
  280.     </connectionManagement>
  281. </system.net>    session.CommitTransaction();<system.net>
  282.     <connectionManagement>
  283.         <add address="*" maxconnection="1200" />
  284.     </connectionManagement>
  285. </system.net>}<system.net>
  286.     <connectionManagement>
  287.         <add address="*" maxconnection="1200" />
  288.     </connectionManagement>
  289. </system.net>catch<system.net>
  290.     <connectionManagement>
  291.         <add address="*" maxconnection="1200" />
  292.     </connectionManagement>
  293. </system.net>{<system.net>
  294.     <connectionManagement>
  295.         <add address="*" maxconnection="1200" />
  296.     </connectionManagement>
  297. </system.net>    session.RollbackTransaction();<system.net>
  298.     <connectionManagement>
  299.         <add address="*" maxconnection="1200" />
  300.     </connectionManagement>
  301. </system.net>    throw;<system.net>
  302.     <connectionManagement>
  303.         <add address="*" maxconnection="1200" />
  304.     </connectionManagement>
  305. </system.net>}<system.net>
  306.     <connectionManagement>
  307.         <add address="*" maxconnection="1200" />
  308.     </connectionManagement>
  309. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  310.     <connectionManagement>
  311.         <add address="*" maxconnection="1200" />
  312.     </connectionManagement>
  313. </system.net>Log("批量添加 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试循环修改        private void button7_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  314.     <connectionManagement>
  315.         <add address="*" maxconnection="1200" />
  316.     </connectionManagement>
  317. </system.net>List userList = m_SysUserDal.GetList("select t.* from sys_user t where t.id > 20");<system.net>
  318.     <connectionManagement>
  319.         <add address="*" maxconnection="1200" />
  320.     </connectionManagement>
  321. </system.net>Log("循环修改 开始 count=" + userList.Count);<system.net>
  322.     <connectionManagement>
  323.         <add address="*" maxconnection="1200" />
  324.     </connectionManagement>
  325. </system.net>DateTime dt = DateTime.Now;<system.net>
  326.     <connectionManagement>
  327.         <add address="*" maxconnection="1200" />
  328.     </connectionManagement>
  329. </system.net>var session = LiteSqlFactory.GetSession();<system.net>
  330.     <connectionManagement>
  331.         <add address="*" maxconnection="1200" />
  332.     </connectionManagement>
  333. </system.net>try<system.net>
  334.     <connectionManagement>
  335.         <add address="*" maxconnection="1200" />
  336.     </connectionManagement>
  337. </system.net>{<system.net>
  338.     <connectionManagement>
  339.         <add address="*" maxconnection="1200" />
  340.     </connectionManagement>
  341. </system.net>    session.OnExecuting = (sql, param) =><system.net>
  342.     <connectionManagement>
  343.         <add address="*" maxconnection="1200" />
  344.     </connectionManagement>
  345. </system.net>    {<system.net>
  346.     <connectionManagement>
  347.         <add address="*" maxconnection="1200" />
  348.     </connectionManagement>
  349. </system.net>        Console.WriteLine(sql); //打印SQL<system.net>
  350.     <connectionManagement>
  351.         <add address="*" maxconnection="1200" />
  352.     </connectionManagement>
  353. </system.net>    };<system.net>
  354.     <connectionManagement>
  355.         <add address="*" maxconnection="1200" />
  356.     </connectionManagement>
  357. </system.net>    session.AttachOld(userList);<system.net>
  358.     <connectionManagement>
  359.         <add address="*" maxconnection="1200" />
  360.     </connectionManagement>
  361. </system.net>    foreach (SysUser user in userList)<system.net>
  362.     <connectionManagement>
  363.         <add address="*" maxconnection="1200" />
  364.     </connectionManagement>
  365. </system.net>    {<system.net>
  366.     <connectionManagement>
  367.         <add address="*" maxconnection="1200" />
  368.     </connectionManagement>
  369. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  370.     <connectionManagement>
  371.         <add address="*" maxconnection="1200" />
  372.     </connectionManagement>
  373. </system.net>        user.UpdateUserid = "1";<system.net>
  374.     <connectionManagement>
  375.         <add address="*" maxconnection="1200" />
  376.     </connectionManagement>
  377. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  378.     <connectionManagement>
  379.         <add address="*" maxconnection="1200" />
  380.     </connectionManagement>
  381. </system.net>    }<system.net>
  382.     <connectionManagement>
  383.         <add address="*" maxconnection="1200" />
  384.     </connectionManagement>
  385. </system.net>    session.BeginTransaction();<system.net>
  386.     <connectionManagement>
  387.         <add address="*" maxconnection="1200" />
  388.     </connectionManagement>
  389. </system.net>    foreach (SysUser user in userList)<system.net>
  390.     <connectionManagement>
  391.         <add address="*" maxconnection="1200" />
  392.     </connectionManagement>
  393. </system.net>    {<system.net>
  394.     <connectionManagement>
  395.         <add address="*" maxconnection="1200" />
  396.     </connectionManagement>
  397. </system.net>        session.Update(user);<system.net>
  398.     <connectionManagement>
  399.         <add address="*" maxconnection="1200" />
  400.     </connectionManagement>
  401. </system.net>    }<system.net>
  402.     <connectionManagement>
  403.         <add address="*" maxconnection="1200" />
  404.     </connectionManagement>
  405. </system.net>    session.CommitTransaction();<system.net>
  406.     <connectionManagement>
  407.         <add address="*" maxconnection="1200" />
  408.     </connectionManagement>
  409. </system.net>}<system.net>
  410.     <connectionManagement>
  411.         <add address="*" maxconnection="1200" />
  412.     </connectionManagement>
  413. </system.net>catch<system.net>
  414.     <connectionManagement>
  415.         <add address="*" maxconnection="1200" />
  416.     </connectionManagement>
  417. </system.net>{<system.net>
  418.     <connectionManagement>
  419.         <add address="*" maxconnection="1200" />
  420.     </connectionManagement>
  421. </system.net>    session.RollbackTransaction();<system.net>
  422.     <connectionManagement>
  423.         <add address="*" maxconnection="1200" />
  424.     </connectionManagement>
  425. </system.net>    throw;<system.net>
  426.     <connectionManagement>
  427.         <add address="*" maxconnection="1200" />
  428.     </connectionManagement>
  429. </system.net>}<system.net>
  430.     <connectionManagement>
  431.         <add address="*" maxconnection="1200" />
  432.     </connectionManagement>
  433. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  434.     <connectionManagement>
  435.         <add address="*" maxconnection="1200" />
  436.     </connectionManagement>
  437. </system.net>Log("循环修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 测试循环添加        private void button6_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  438.     <connectionManagement>
  439.         <add address="*" maxconnection="1200" />
  440.     </connectionManagement>
  441. </system.net>List userList = new List();<system.net>
  442.     <connectionManagement>
  443.         <add address="*" maxconnection="1200" />
  444.     </connectionManagement>
  445. </system.net>for (int i = 1; i<system.net>
  446.     <connectionManagement>
  447.         <add address="*" maxconnection="1200" />
  448.     </connectionManagement>
  449. </system.net>     {<system.net>
  450.     <connectionManagement>
  451.         <add address="*" maxconnection="1200" />
  452.     </connectionManagement>
  453. </system.net>        Console.WriteLine(sql); //打印SQL<system.net>
  454.     <connectionManagement>
  455.         <add address="*" maxconnection="1200" />
  456.     </connectionManagement>
  457. </system.net>    };<system.net>
  458.     <connectionManagement>
  459.         <add address="*" maxconnection="1200" />
  460.     </connectionManagement>
  461. </system.net>    session.BeginTransaction();<system.net>
  462.     <connectionManagement>
  463.         <add address="*" maxconnection="1200" />
  464.     </connectionManagement>
  465. </system.net>    foreach (SysUser user in userList)<system.net>
  466.     <connectionManagement>
  467.         <add address="*" maxconnection="1200" />
  468.     </connectionManagement>
  469. </system.net>    {<system.net>
  470.     <connectionManagement>
  471.         <add address="*" maxconnection="1200" />
  472.     </connectionManagement>
  473. </system.net>        session.Insert(user);<system.net>
  474.     <connectionManagement>
  475.         <add address="*" maxconnection="1200" />
  476.     </connectionManagement>
  477. </system.net>    }<system.net>
  478.     <connectionManagement>
  479.         <add address="*" maxconnection="1200" />
  480.     </connectionManagement>
  481. </system.net>    session.CommitTransaction();<system.net>
  482.     <connectionManagement>
  483.         <add address="*" maxconnection="1200" />
  484.     </connectionManagement>
  485. </system.net>}<system.net>
  486.     <connectionManagement>
  487.         <add address="*" maxconnection="1200" />
  488.     </connectionManagement>
  489. </system.net>catch<system.net>
  490.     <connectionManagement>
  491.         <add address="*" maxconnection="1200" />
  492.     </connectionManagement>
  493. </system.net>{<system.net>
  494.     <connectionManagement>
  495.         <add address="*" maxconnection="1200" />
  496.     </connectionManagement>
  497. </system.net>    session.RollbackTransaction();<system.net>
  498.     <connectionManagement>
  499.         <add address="*" maxconnection="1200" />
  500.     </connectionManagement>
  501. </system.net>    throw;<system.net>
  502.     <connectionManagement>
  503.         <add address="*" maxconnection="1200" />
  504.     </connectionManagement>
  505. </system.net>}<system.net>
  506.     <connectionManagement>
  507.         <add address="*" maxconnection="1200" />
  508.     </connectionManagement>
  509. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  510.     <connectionManagement>
  511.         <add address="*" maxconnection="1200" />
  512.     </connectionManagement>
  513. </system.net>Log("循环添加 完成,耗时:" + time + "秒");            });        }        #endregion        #region 查询        private void button1_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  514.     <connectionManagement>
  515.         <add address="*" maxconnection="1200" />
  516.     </connectionManagement>
  517. </system.net>Log("查询 开始");<system.net>
  518.     <connectionManagement>
  519.         <add address="*" maxconnection="1200" />
  520.     </connectionManagement>
  521. </system.net>DateTime dt = DateTime.Now;<system.net>
  522.     <connectionManagement>
  523.         <add address="*" maxconnection="1200" />
  524.     </connectionManagement>
  525. </system.net>for (int i = 0; i < 10; i++)<system.net>
  526.     <connectionManagement>
  527.         <add address="*" maxconnection="1200" />
  528.     </connectionManagement>
  529. </system.net>{<system.net>
  530.     <connectionManagement>
  531.         <add address="*" maxconnection="1200" />
  532.     </connectionManagement>
  533. </system.net>    var session = LiteSqlFactory.GetSession();<system.net>
  534.     <connectionManagement>
  535.         <add address="*" maxconnection="1200" />
  536.     </connectionManagement>
  537. </system.net>    ISqlString sql = session.CreateSql(@"<system.net>
  538.     <connectionManagement>
  539.         <add address="*" maxconnection="1200" />
  540.     </connectionManagement>
  541. </system.net>        select t.*<system.net>
  542.     <connectionManagement>
  543.         <add address="*" maxconnection="1200" />
  544.     </connectionManagement>
  545. </system.net>         from sys_user t<system.net>
  546.     <connectionManagement>
  547.         <add address="*" maxconnection="1200" />
  548.     </connectionManagement>
  549. </system.net>         where t.id > @id<system.net>
  550.     <connectionManagement>
  551.         <add address="*" maxconnection="1200" />
  552.     </connectionManagement>
  553. </system.net>         and t.real_name like @remark", 20, "%测试%");<system.net>
  554.     <connectionManagement>
  555.         <add address="*" maxconnection="1200" />
  556.     </connectionManagement>
  557. </system.net>    sql.Append(" order by t.create_time desc, t.id asc");<system.net>
  558.     <connectionManagement>
  559.         <add address="*" maxconnection="1200" />
  560.     </connectionManagement>
  561. </system.net>    List userList = sql.QueryList();<system.net>
  562.     <connectionManagement>
  563.         <add address="*" maxconnection="1200" />
  564.     </connectionManagement>
  565. </system.net>    Log("查询结果 count=" + userList.Count.ToString());<system.net>
  566.     <connectionManagement>
  567.         <add address="*" maxconnection="1200" />
  568.     </connectionManagement>
  569. </system.net>}<system.net>
  570.     <connectionManagement>
  571.         <add address="*" maxconnection="1200" />
  572.     </connectionManagement>
  573. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  574.     <connectionManagement>
  575.         <add address="*" maxconnection="1200" />
  576.     </connectionManagement>
  577. </system.net>Log("查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 分页查询        private void button2_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  578.     <connectionManagement>
  579.         <add address="*" maxconnection="1200" />
  580.     </connectionManagement>
  581. </system.net>Log("分页查询 开始");<system.net>
  582.     <connectionManagement>
  583.         <add address="*" maxconnection="1200" />
  584.     </connectionManagement>
  585. </system.net>DateTime dt = DateTime.Now;<system.net>
  586.     <connectionManagement>
  587.         <add address="*" maxconnection="1200" />
  588.     </connectionManagement>
  589. </system.net>for (int i = 0; i < 10; i++)<system.net>
  590.     <connectionManagement>
  591.         <add address="*" maxconnection="1200" />
  592.     </connectionManagement>
  593. </system.net>{<system.net>
  594.     <connectionManagement>
  595.         <add address="*" maxconnection="1200" />
  596.     </connectionManagement>
  597. </system.net>    int total = m_SysUserDal.GetTotalCount();<system.net>
  598.     <connectionManagement>
  599.         <add address="*" maxconnection="1200" />
  600.     </connectionManagement>
  601. </system.net>    int pageSize = 100;<system.net>
  602.     <connectionManagement>
  603.         <add address="*" maxconnection="1200" />
  604.     </connectionManagement>
  605. </system.net>    int pageCount = (total - 1) / pageSize + 1;<system.net>
  606.     <connectionManagement>
  607.         <add address="*" maxconnection="1200" />
  608.     </connectionManagement>
  609. </system.net>    var session = LiteSqlFactory.GetSession();<system.net>
  610.     <connectionManagement>
  611.         <add address="*" maxconnection="1200" />
  612.     </connectionManagement>
  613. </system.net>    List userList = new List();<system.net>
  614.     <connectionManagement>
  615.         <add address="*" maxconnection="1200" />
  616.     </connectionManagement>
  617. </system.net>    for (int page = 1; page             {<system.net>
  618.     <connectionManagement>
  619.         <add address="*" maxconnection="1200" />
  620.     </connectionManagement>
  621. </system.net>Log("并发查询 开始");<system.net>
  622.     <connectionManagement>
  623.         <add address="*" maxconnection="1200" />
  624.     </connectionManagement>
  625. </system.net>DateTime dt = DateTime.Now;<system.net>
  626.     <connectionManagement>
  627.         <add address="*" maxconnection="1200" />
  628.     </connectionManagement>
  629. </system.net>List tasks = new List();<system.net>
  630.     <connectionManagement>
  631.         <add address="*" maxconnection="1200" />
  632.     </connectionManagement>
  633. </system.net>for (int i = 0; i < 200; i++)<system.net>
  634.     <connectionManagement>
  635.         <add address="*" maxconnection="1200" />
  636.     </connectionManagement>
  637. </system.net>{<system.net>
  638.     <connectionManagement>
  639.         <add address="*" maxconnection="1200" />
  640.     </connectionManagement>
  641. </system.net>    Task task = RunTask(() =><system.net>
  642.     <connectionManagement>
  643.         <add address="*" maxconnection="1200" />
  644.     </connectionManagement>
  645. </system.net>    {<system.net>
  646.     <connectionManagement>
  647.         <add address="*" maxconnection="1200" />
  648.     </connectionManagement>
  649. </system.net>        var session = LiteSqlFactory.GetSession();<system.net>
  650.     <connectionManagement>
  651.         <add address="*" maxconnection="1200" />
  652.     </connectionManagement>
  653. </system.net>        ISqlString sql = session.CreateSql(@"<system.net>
  654.     <connectionManagement>
  655.         <add address="*" maxconnection="1200" />
  656.     </connectionManagement>
  657. </system.net>            select t.*<system.net>
  658.     <connectionManagement>
  659.         <add address="*" maxconnection="1200" />
  660.     </connectionManagement>
  661. </system.net>             from sys_user t<system.net>
  662.     <connectionManagement>
  663.         <add address="*" maxconnection="1200" />
  664.     </connectionManagement>
  665. </system.net>             where t.id > @id<system.net>
  666.     <connectionManagement>
  667.         <add address="*" maxconnection="1200" />
  668.     </connectionManagement>
  669. </system.net>             and t.real_name like @remark", 20, "%测试%");<system.net>
  670.     <connectionManagement>
  671.         <add address="*" maxconnection="1200" />
  672.     </connectionManagement>
  673. </system.net>        sql.Append(" order by t.create_time desc, t.id asc");<system.net>
  674.     <connectionManagement>
  675.         <add address="*" maxconnection="1200" />
  676.     </connectionManagement>
  677. </system.net>        List userList = sql.QueryList();<system.net>
  678.     <connectionManagement>
  679.         <add address="*" maxconnection="1200" />
  680.     </connectionManagement>
  681. </system.net>        Log("查询结果 count=" + userList.Count.ToString());<system.net>
  682.     <connectionManagement>
  683.         <add address="*" maxconnection="1200" />
  684.     </connectionManagement>
  685. </system.net>    });<system.net>
  686.     <connectionManagement>
  687.         <add address="*" maxconnection="1200" />
  688.     </connectionManagement>
  689. </system.net>    tasks.Add(task);<system.net>
  690.     <connectionManagement>
  691.         <add address="*" maxconnection="1200" />
  692.     </connectionManagement>
  693. </system.net>}<system.net>
  694.     <connectionManagement>
  695.         <add address="*" maxconnection="1200" />
  696.     </connectionManagement>
  697. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  698.     <connectionManagement>
  699.         <add address="*" maxconnection="1200" />
  700.     </connectionManagement>
  701. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  702.     <connectionManagement>
  703.         <add address="*" maxconnection="1200" />
  704.     </connectionManagement>
  705. </system.net>Log("并发查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发插入        private void button9_Click(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            RunTask(() =>            {<system.net>
  706.     <connectionManagement>
  707.         <add address="*" maxconnection="1200" />
  708.     </connectionManagement>
  709. </system.net>List userList = new List();<system.net>
  710.     <connectionManagement>
  711.         <add address="*" maxconnection="1200" />
  712.     </connectionManagement>
  713. </system.net>for (int i = 1; i<system.net>
  714.     <connectionManagement>
  715.         <add address="*" maxconnection="1200" />
  716.     </connectionManagement>
  717. </system.net>     {<system.net>
  718.     <connectionManagement>
  719.         <add address="*" maxconnection="1200" />
  720.     </connectionManagement>
  721. </system.net>        LiteSqlFactory.GetSession().Insert(user);<system.net>
  722.     <connectionManagement>
  723.         <add address="*" maxconnection="1200" />
  724.     </connectionManagement>
  725. </system.net>    }, item);<system.net>
  726.     <connectionManagement>
  727.         <add address="*" maxconnection="1200" />
  728.     </connectionManagement>
  729. </system.net>    tasks.Add(task);<system.net>
  730.     <connectionManagement>
  731.         <add address="*" maxconnection="1200" />
  732.     </connectionManagement>
  733. </system.net>}<system.net>
  734.     <connectionManagement>
  735.         <add address="*" maxconnection="1200" />
  736.     </connectionManagement>
  737. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  738.     <connectionManagement>
  739.         <add address="*" maxconnection="1200" />
  740.     </connectionManagement>
  741. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  742.     <connectionManagement>
  743.         <add address="*" maxconnection="1200" />
  744.     </connectionManagement>
  745. </system.net>Log("并发插入 完成,耗时:" + time + "秒");            });        }        #endregion    }}
复制代码
10. Dapper.LiteSql NET6版本

(为了和Fast.Framewor对比,用了异步,不过我用的MySql.Data.dll是假异步,Fast.Framework用的MySqlConnector是真异步):
  1. using Dapper.LiteSql;using Models;using Provider;namespace DapperLiteSqlDemo{    public partial class Form1 : Form    {        #region 变量        private Random _rnd = new Random();        private int _count = 10000;        private bool _showSqlLog = false;        private static ILiteSqlClient _db;        #endregion        #region Form1        public Form1()        {            InitializeComponent();        }        #endregion        #region Form1_Load        private void Form1_Load(object sender, EventArgs e)        {            ThreadPool.SetMaxThreads(1000, 1000);            ThreadPool.SetMinThreads(200, 200);            _db = new LiteSqlClient("server=localhost;database=litesql_test;user id=root;password=123456;character set=utf8mb4;",<system.net>
  2.     <connectionManagement>
  3.         <add address="*" maxconnection="1200" />
  4.     </connectionManagement>
  5. </system.net>DBType.MySQL, new MySQLProvider());            RunTask(() =>            {<system.net>
  6.     <connectionManagement>
  7.         <add address="*" maxconnection="1200" />
  8.     </connectionManagement>
  9. </system.net>_db.GetSession(); //预热<system.net>
  10.     <connectionManagement>
  11.         <add address="*" maxconnection="1200" />
  12.     </connectionManagement>
  13. </system.net>Log("预热完成");            });        }        #endregion        #region Log        private void Log(string log)        {            if (!this.IsDisposed)            {<system.net>
  14.     <connectionManagement>
  15.         <add address="*" maxconnection="1200" />
  16.     </connectionManagement>
  17. </system.net>string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";<system.net>
  18.     <connectionManagement>
  19.         <add address="*" maxconnection="1200" />
  20.     </connectionManagement>
  21. </system.net>if (this.InvokeRequired)<system.net>
  22.     <connectionManagement>
  23.         <add address="*" maxconnection="1200" />
  24.     </connectionManagement>
  25. </system.net>{<system.net>
  26.     <connectionManagement>
  27.         <add address="*" maxconnection="1200" />
  28.     </connectionManagement>
  29. </system.net>    this.BeginInvoke(new Action(() =><system.net>
  30.     <connectionManagement>
  31.         <add address="*" maxconnection="1200" />
  32.     </connectionManagement>
  33. </system.net>    {<system.net>
  34.     <connectionManagement>
  35.         <add address="*" maxconnection="1200" />
  36.     </connectionManagement>
  37. </system.net>        textBox1.AppendText(msg);<system.net>
  38.     <connectionManagement>
  39.         <add address="*" maxconnection="1200" />
  40.     </connectionManagement>
  41. </system.net>    }));<system.net>
  42.     <connectionManagement>
  43.         <add address="*" maxconnection="1200" />
  44.     </connectionManagement>
  45. </system.net>}<system.net>
  46.     <connectionManagement>
  47.         <add address="*" maxconnection="1200" />
  48.     </connectionManagement>
  49. </system.net>else<system.net>
  50.     <connectionManagement>
  51.         <add address="*" maxconnection="1200" />
  52.     </connectionManagement>
  53. </system.net>{<system.net>
  54.     <connectionManagement>
  55.         <add address="*" maxconnection="1200" />
  56.     </connectionManagement>
  57. </system.net>    textBox1.AppendText(msg);<system.net>
  58.     <connectionManagement>
  59.         <add address="*" maxconnection="1200" />
  60.     </connectionManagement>
  61. </system.net>}            }        }        #endregion        #region 清空输出框        private void button10_Click(object sender, EventArgs e)        {            textBox1.Text = string.Empty;        }        #endregion        #region RunTask        private Task RunTask(Action action)        {            return Task.Run(() =>            {<system.net>
  62.     <connectionManagement>
  63.         <add address="*" maxconnection="1200" />
  64.     </connectionManagement>
  65. </system.net>try<system.net>
  66.     <connectionManagement>
  67.         <add address="*" maxconnection="1200" />
  68.     </connectionManagement>
  69. </system.net>{<system.net>
  70.     <connectionManagement>
  71.         <add address="*" maxconnection="1200" />
  72.     </connectionManagement>
  73. </system.net>    action();<system.net>
  74.     <connectionManagement>
  75.         <add address="*" maxconnection="1200" />
  76.     </connectionManagement>
  77. </system.net>}<system.net>
  78.     <connectionManagement>
  79.         <add address="*" maxconnection="1200" />
  80.     </connectionManagement>
  81. </system.net>catch (Exception ex)<system.net>
  82.     <connectionManagement>
  83.         <add address="*" maxconnection="1200" />
  84.     </connectionManagement>
  85. </system.net>{<system.net>
  86.     <connectionManagement>
  87.         <add address="*" maxconnection="1200" />
  88.     </connectionManagement>
  89. </system.net>    Log(ex.ToString());<system.net>
  90.     <connectionManagement>
  91.         <add address="*" maxconnection="1200" />
  92.     </connectionManagement>
  93. </system.net>    throw;<system.net>
  94.     <connectionManagement>
  95.         <add address="*" maxconnection="1200" />
  96.     </connectionManagement>
  97. </system.net>}            });        }        private Task RunTask(Action action, T t)        {            return Task.Factory.StartNew(obj =>            {<system.net>
  98.     <connectionManagement>
  99.         <add address="*" maxconnection="1200" />
  100.     </connectionManagement>
  101. </system.net>try<system.net>
  102.     <connectionManagement>
  103.         <add address="*" maxconnection="1200" />
  104.     </connectionManagement>
  105. </system.net>{<system.net>
  106.     <connectionManagement>
  107.         <add address="*" maxconnection="1200" />
  108.     </connectionManagement>
  109. </system.net>    action((T)obj);<system.net>
  110.     <connectionManagement>
  111.         <add address="*" maxconnection="1200" />
  112.     </connectionManagement>
  113. </system.net>}<system.net>
  114.     <connectionManagement>
  115.         <add address="*" maxconnection="1200" />
  116.     </connectionManagement>
  117. </system.net>catch (Exception ex)<system.net>
  118.     <connectionManagement>
  119.         <add address="*" maxconnection="1200" />
  120.     </connectionManagement>
  121. </system.net>{<system.net>
  122.     <connectionManagement>
  123.         <add address="*" maxconnection="1200" />
  124.     </connectionManagement>
  125. </system.net>    Log(ex.ToString());<system.net>
  126.     <connectionManagement>
  127.         <add address="*" maxconnection="1200" />
  128.     </connectionManagement>
  129. </system.net>    throw;<system.net>
  130.     <connectionManagement>
  131.         <add address="*" maxconnection="1200" />
  132.     </connectionManagement>
  133. </system.net>}            }, t);        }        #endregion        #region 删除        private void button1_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  134.     <connectionManagement>
  135.         <add address="*" maxconnection="1200" />
  136.     </connectionManagement>
  137. </system.net>Log("删除 开始");<system.net>
  138.     <connectionManagement>
  139.         <add address="*" maxconnection="1200" />
  140.     </connectionManagement>
  141. </system.net>await _db.GetSession().CreateSql("id>@Id", 20).DeleteByConditionAsync();<system.net>
  142.     <connectionManagement>
  143.         <add address="*" maxconnection="1200" />
  144.     </connectionManagement>
  145. </system.net>Log("删除 完成");            });        }        #endregion        #region 批量修改        private void button2_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  146.     <connectionManagement>
  147.         <add address="*" maxconnection="1200" />
  148.     </connectionManagement>
  149. </system.net>List userList = await _db.GetSession().Queryable().Where(t => t.Id > 20).ToListAsync();<system.net>
  150.     <connectionManagement>
  151.         <add address="*" maxconnection="1200" />
  152.     </connectionManagement>
  153. </system.net>Log("批量修改 开始 count=" + userList.Count);<system.net>
  154.     <connectionManagement>
  155.         <add address="*" maxconnection="1200" />
  156.     </connectionManagement>
  157. </system.net>DateTime dt = DateTime.Now;<system.net>
  158.     <connectionManagement>
  159.         <add address="*" maxconnection="1200" />
  160.     </connectionManagement>
  161. </system.net>ISession? session = null;<system.net>
  162.     <connectionManagement>
  163.         <add address="*" maxconnection="1200" />
  164.     </connectionManagement>
  165. </system.net>try<system.net>
  166.     <connectionManagement>
  167.         <add address="*" maxconnection="1200" />
  168.     </connectionManagement>
  169. </system.net>{<system.net>
  170.     <connectionManagement>
  171.         <add address="*" maxconnection="1200" />
  172.     </connectionManagement>
  173. </system.net>    foreach (SysUser user in userList)<system.net>
  174.     <connectionManagement>
  175.         <add address="*" maxconnection="1200" />
  176.     </connectionManagement>
  177. </system.net>    {<system.net>
  178.     <connectionManagement>
  179.         <add address="*" maxconnection="1200" />
  180.     </connectionManagement>
  181. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  182.     <connectionManagement>
  183.         <add address="*" maxconnection="1200" />
  184.     </connectionManagement>
  185. </system.net>        user.UpdateUserid = "1";<system.net>
  186.     <connectionManagement>
  187.         <add address="*" maxconnection="1200" />
  188.     </connectionManagement>
  189. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  190.     <connectionManagement>
  191.         <add address="*" maxconnection="1200" />
  192.     </connectionManagement>
  193. </system.net>    }<system.net>
  194.     <connectionManagement>
  195.         <add address="*" maxconnection="1200" />
  196.     </connectionManagement>
  197. </system.net>    session = await _db.GetSessionAsync();<system.net>
  198.     <connectionManagement>
  199.         <add address="*" maxconnection="1200" />
  200.     </connectionManagement>
  201. </system.net>    session.BeginTransaction();<system.net>
  202.     <connectionManagement>
  203.         <add address="*" maxconnection="1200" />
  204.     </connectionManagement>
  205. </system.net>    await session.UpdateAsync(userList);<system.net>
  206.     <connectionManagement>
  207.         <add address="*" maxconnection="1200" />
  208.     </connectionManagement>
  209. </system.net>    session.CommitTransaction();<system.net>
  210.     <connectionManagement>
  211.         <add address="*" maxconnection="1200" />
  212.     </connectionManagement>
  213. </system.net>}<system.net>
  214.     <connectionManagement>
  215.         <add address="*" maxconnection="1200" />
  216.     </connectionManagement>
  217. </system.net>catch<system.net>
  218.     <connectionManagement>
  219.         <add address="*" maxconnection="1200" />
  220.     </connectionManagement>
  221. </system.net>{<system.net>
  222.     <connectionManagement>
  223.         <add address="*" maxconnection="1200" />
  224.     </connectionManagement>
  225. </system.net>    session?.RollbackTransaction();<system.net>
  226.     <connectionManagement>
  227.         <add address="*" maxconnection="1200" />
  228.     </connectionManagement>
  229. </system.net>    throw;<system.net>
  230.     <connectionManagement>
  231.         <add address="*" maxconnection="1200" />
  232.     </connectionManagement>
  233. </system.net>}<system.net>
  234.     <connectionManagement>
  235.         <add address="*" maxconnection="1200" />
  236.     </connectionManagement>
  237. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  238.     <connectionManagement>
  239.         <add address="*" maxconnection="1200" />
  240.     </connectionManagement>
  241. </system.net>Log("批量修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 批量添加        private void button3_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  242.     <connectionManagement>
  243.         <add address="*" maxconnection="1200" />
  244.     </connectionManagement>
  245. </system.net>List userList = new List();<system.net>
  246.     <connectionManagement>
  247.         <add address="*" maxconnection="1200" />
  248.     </connectionManagement>
  249. </system.net>for (int i = 1; i             {<system.net>
  250.     <connectionManagement>
  251.         <add address="*" maxconnection="1200" />
  252.     </connectionManagement>
  253. </system.net>List userList = await _db.GetSession().Queryable().Where(t => t.Id > 20).ToListAsync();<system.net>
  254.     <connectionManagement>
  255.         <add address="*" maxconnection="1200" />
  256.     </connectionManagement>
  257. </system.net>Log("循环修改 开始 count=" + userList.Count);<system.net>
  258.     <connectionManagement>
  259.         <add address="*" maxconnection="1200" />
  260.     </connectionManagement>
  261. </system.net>DateTime dt = DateTime.Now;<system.net>
  262.     <connectionManagement>
  263.         <add address="*" maxconnection="1200" />
  264.     </connectionManagement>
  265. </system.net>ISession? session = null;<system.net>
  266.     <connectionManagement>
  267.         <add address="*" maxconnection="1200" />
  268.     </connectionManagement>
  269. </system.net>try<system.net>
  270.     <connectionManagement>
  271.         <add address="*" maxconnection="1200" />
  272.     </connectionManagement>
  273. </system.net>{<system.net>
  274.     <connectionManagement>
  275.         <add address="*" maxconnection="1200" />
  276.     </connectionManagement>
  277. </system.net>    foreach (SysUser user in userList)<system.net>
  278.     <connectionManagement>
  279.         <add address="*" maxconnection="1200" />
  280.     </connectionManagement>
  281. </system.net>    {<system.net>
  282.     <connectionManagement>
  283.         <add address="*" maxconnection="1200" />
  284.     </connectionManagement>
  285. </system.net>        user.Remark = "测试修改用户" + _rnd.Next(1, 10000);<system.net>
  286.     <connectionManagement>
  287.         <add address="*" maxconnection="1200" />
  288.     </connectionManagement>
  289. </system.net>        user.UpdateUserid = "1";<system.net>
  290.     <connectionManagement>
  291.         <add address="*" maxconnection="1200" />
  292.     </connectionManagement>
  293. </system.net>        user.UpdateTime = DateTime.Now;<system.net>
  294.     <connectionManagement>
  295.         <add address="*" maxconnection="1200" />
  296.     </connectionManagement>
  297. </system.net>    }<system.net>
  298.     <connectionManagement>
  299.         <add address="*" maxconnection="1200" />
  300.     </connectionManagement>
  301. </system.net>    session = await _db.GetSessionAsync();<system.net>
  302.     <connectionManagement>
  303.         <add address="*" maxconnection="1200" />
  304.     </connectionManagement>
  305. </system.net>    session.BeginTransaction();<system.net>
  306.     <connectionManagement>
  307.         <add address="*" maxconnection="1200" />
  308.     </connectionManagement>
  309. </system.net>    foreach (SysUser user in userList)<system.net>
  310.     <connectionManagement>
  311.         <add address="*" maxconnection="1200" />
  312.     </connectionManagement>
  313. </system.net>    {<system.net>
  314.     <connectionManagement>
  315.         <add address="*" maxconnection="1200" />
  316.     </connectionManagement>
  317. </system.net>        await session.UpdateAsync(user);<system.net>
  318.     <connectionManagement>
  319.         <add address="*" maxconnection="1200" />
  320.     </connectionManagement>
  321. </system.net>    }<system.net>
  322.     <connectionManagement>
  323.         <add address="*" maxconnection="1200" />
  324.     </connectionManagement>
  325. </system.net>    session.CommitTransaction();<system.net>
  326.     <connectionManagement>
  327.         <add address="*" maxconnection="1200" />
  328.     </connectionManagement>
  329. </system.net>}<system.net>
  330.     <connectionManagement>
  331.         <add address="*" maxconnection="1200" />
  332.     </connectionManagement>
  333. </system.net>catch<system.net>
  334.     <connectionManagement>
  335.         <add address="*" maxconnection="1200" />
  336.     </connectionManagement>
  337. </system.net>{<system.net>
  338.     <connectionManagement>
  339.         <add address="*" maxconnection="1200" />
  340.     </connectionManagement>
  341. </system.net>    session?.RollbackTransaction();<system.net>
  342.     <connectionManagement>
  343.         <add address="*" maxconnection="1200" />
  344.     </connectionManagement>
  345. </system.net>    throw;<system.net>
  346.     <connectionManagement>
  347.         <add address="*" maxconnection="1200" />
  348.     </connectionManagement>
  349. </system.net>}<system.net>
  350.     <connectionManagement>
  351.         <add address="*" maxconnection="1200" />
  352.     </connectionManagement>
  353. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  354.     <connectionManagement>
  355.         <add address="*" maxconnection="1200" />
  356.     </connectionManagement>
  357. </system.net>Log("循环修改 完成,耗时:" + time + "秒");            });        }        #endregion        #region 循环添加        private void button5_Click(object sender, EventArgs e)        {            RunTask(async () =>            {<system.net>
  358.     <connectionManagement>
  359.         <add address="*" maxconnection="1200" />
  360.     </connectionManagement>
  361. </system.net>List userList = new List();<system.net>
  362.     <connectionManagement>
  363.         <add address="*" maxconnection="1200" />
  364.     </connectionManagement>
  365. </system.net>for (int i = 1; i             {<system.net>
  366.     <connectionManagement>
  367.         <add address="*" maxconnection="1200" />
  368.     </connectionManagement>
  369. </system.net>Log("查询 开始");<system.net>
  370.     <connectionManagement>
  371.         <add address="*" maxconnection="1200" />
  372.     </connectionManagement>
  373. </system.net>DateTime dt = DateTime.Now;<system.net>
  374.     <connectionManagement>
  375.         <add address="*" maxconnection="1200" />
  376.     </connectionManagement>
  377. </system.net>for (int i = 0; i < 10; i++)<system.net>
  378.     <connectionManagement>
  379.         <add address="*" maxconnection="1200" />
  380.     </connectionManagement>
  381. </system.net>{<system.net>
  382.     <connectionManagement>
  383.         <add address="*" maxconnection="1200" />
  384.     </connectionManagement>
  385. </system.net>    List userList = await _db.GetSession().Queryable()<system.net>
  386.     <connectionManagement>
  387.         <add address="*" maxconnection="1200" />
  388.     </connectionManagement>
  389. </system.net>             .Where(t => t.Id > 20 && t.RealName.Contains("%测试%"))<system.net>
  390.     <connectionManagement>
  391.         <add address="*" maxconnection="1200" />
  392.     </connectionManagement>
  393. </system.net>             .OrderByDescending(t => t.CreateTime)<system.net>
  394.     <connectionManagement>
  395.         <add address="*" maxconnection="1200" />
  396.     </connectionManagement>
  397. </system.net>             .OrderBy(t => t.Id).ToListAsync();<system.net>
  398.     <connectionManagement>
  399.         <add address="*" maxconnection="1200" />
  400.     </connectionManagement>
  401. </system.net>    Log("查询结果 count=" + userList.Count.ToString());<system.net>
  402.     <connectionManagement>
  403.         <add address="*" maxconnection="1200" />
  404.     </connectionManagement>
  405. </system.net>}<system.net>
  406.     <connectionManagement>
  407.         <add address="*" maxconnection="1200" />
  408.     </connectionManagement>
  409. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  410.     <connectionManagement>
  411.         <add address="*" maxconnection="1200" />
  412.     </connectionManagement>
  413. </system.net>Log("查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 分页查询        private void button7_Click(object sender, EventArgs e)        {            Log("尚未实现");        }        #endregion        #region 并发查询        private void button8_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  414.     <connectionManagement>
  415.         <add address="*" maxconnection="1200" />
  416.     </connectionManagement>
  417. </system.net>ThreadPool.SetMaxThreads(1000, 1000);<system.net>
  418.     <connectionManagement>
  419.         <add address="*" maxconnection="1200" />
  420.     </connectionManagement>
  421. </system.net>ThreadPool.SetMinThreads(200, 200);<system.net>
  422.     <connectionManagement>
  423.         <add address="*" maxconnection="1200" />
  424.     </connectionManagement>
  425. </system.net>Log("并发查询 开始");<system.net>
  426.     <connectionManagement>
  427.         <add address="*" maxconnection="1200" />
  428.     </connectionManagement>
  429. </system.net>DateTime dt = DateTime.Now;<system.net>
  430.     <connectionManagement>
  431.         <add address="*" maxconnection="1200" />
  432.     </connectionManagement>
  433. </system.net>List tasks = new List();<system.net>
  434.     <connectionManagement>
  435.         <add address="*" maxconnection="1200" />
  436.     </connectionManagement>
  437. </system.net>for (int i = 0; i < 200; i++)<system.net>
  438.     <connectionManagement>
  439.         <add address="*" maxconnection="1200" />
  440.     </connectionManagement>
  441. </system.net>{<system.net>
  442.     <connectionManagement>
  443.         <add address="*" maxconnection="1200" />
  444.     </connectionManagement>
  445. </system.net>    Task task = RunTask(async () =><system.net>
  446.     <connectionManagement>
  447.         <add address="*" maxconnection="1200" />
  448.     </connectionManagement>
  449. </system.net>    {<system.net>
  450.     <connectionManagement>
  451.         <add address="*" maxconnection="1200" />
  452.     </connectionManagement>
  453. </system.net>        List userList = await _db.GetSession().Queryable()<system.net>
  454.     <connectionManagement>
  455.         <add address="*" maxconnection="1200" />
  456.     </connectionManagement>
  457. </system.net>            .Where(t => t.Id > 20 && t.RealName.Contains("%测试%"))<system.net>
  458.     <connectionManagement>
  459.         <add address="*" maxconnection="1200" />
  460.     </connectionManagement>
  461. </system.net>            .OrderByDescending(t => t.CreateTime)<system.net>
  462.     <connectionManagement>
  463.         <add address="*" maxconnection="1200" />
  464.     </connectionManagement>
  465. </system.net>            .OrderBy(t => t.Id).ToListAsync();<system.net>
  466.     <connectionManagement>
  467.         <add address="*" maxconnection="1200" />
  468.     </connectionManagement>
  469. </system.net>        Log("查询结果 count=" + userList.Count.ToString());<system.net>
  470.     <connectionManagement>
  471.         <add address="*" maxconnection="1200" />
  472.     </connectionManagement>
  473. </system.net>    });<system.net>
  474.     <connectionManagement>
  475.         <add address="*" maxconnection="1200" />
  476.     </connectionManagement>
  477. </system.net>    tasks.Add(task);<system.net>
  478.     <connectionManagement>
  479.         <add address="*" maxconnection="1200" />
  480.     </connectionManagement>
  481. </system.net>}<system.net>
  482.     <connectionManagement>
  483.         <add address="*" maxconnection="1200" />
  484.     </connectionManagement>
  485. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  486.     <connectionManagement>
  487.         <add address="*" maxconnection="1200" />
  488.     </connectionManagement>
  489. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  490.     <connectionManagement>
  491.         <add address="*" maxconnection="1200" />
  492.     </connectionManagement>
  493. </system.net>Log("并发查询 完成,耗时:" + time + "秒");            });        }        #endregion        #region 并发插入        private void button9_Click(object sender, EventArgs e)        {            RunTask(() =>            {<system.net>
  494.     <connectionManagement>
  495.         <add address="*" maxconnection="1200" />
  496.     </connectionManagement>
  497. </system.net>ThreadPool.SetMaxThreads(1000, 1000);<system.net>
  498.     <connectionManagement>
  499.         <add address="*" maxconnection="1200" />
  500.     </connectionManagement>
  501. </system.net>ThreadPool.SetMinThreads(200, 200);<system.net>
  502.     <connectionManagement>
  503.         <add address="*" maxconnection="1200" />
  504.     </connectionManagement>
  505. </system.net>List userList = new List();<system.net>
  506.     <connectionManagement>
  507.         <add address="*" maxconnection="1200" />
  508.     </connectionManagement>
  509. </system.net>for (int i = 1; i<system.net>
  510.     <connectionManagement>
  511.         <add address="*" maxconnection="1200" />
  512.     </connectionManagement>
  513. </system.net>     {<system.net>
  514.     <connectionManagement>
  515.         <add address="*" maxconnection="1200" />
  516.     </connectionManagement>
  517. </system.net>        await _db.GetSession().InsertAsync(user);<system.net>
  518.     <connectionManagement>
  519.         <add address="*" maxconnection="1200" />
  520.     </connectionManagement>
  521. </system.net>    }, item);<system.net>
  522.     <connectionManagement>
  523.         <add address="*" maxconnection="1200" />
  524.     </connectionManagement>
  525. </system.net>    tasks.Add(task);<system.net>
  526.     <connectionManagement>
  527.         <add address="*" maxconnection="1200" />
  528.     </connectionManagement>
  529. </system.net>}<system.net>
  530.     <connectionManagement>
  531.         <add address="*" maxconnection="1200" />
  532.     </connectionManagement>
  533. </system.net>Task.WaitAll(tasks.ToArray());<system.net>
  534.     <connectionManagement>
  535.         <add address="*" maxconnection="1200" />
  536.     </connectionManagement>
  537. </system.net>string time = DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000");<system.net>
  538.     <connectionManagement>
  539.         <add address="*" maxconnection="1200" />
  540.     </connectionManagement>
  541. </system.net>Log("并发插入 完成,耗时:" + time + "秒");            });        }        #endregion    }}
复制代码
部分测试结果

1. 循环插入、循环修改和查询

真的差不多。

2. 并发插入,全部通过

Fast.Framework还在优化,我用的临时版本

补充:Fast.Framework优化后正常了

(也就三两行代码的事,但不测试不知道):

SqlSugar和Fast.Framework的测试结果

就是我这篇博客要讲的的重点了,并发,肯定要加锁,并发大的时候,锁们可能在相互等待,干一会活,歇一会,导致白白浪费几秒,甚至几十秒。这种问题很难发现,看代码可能发现不了,因为从代码看可能代码写的没什么问题,你总不能不加锁吧?
真实原因是什么?线程池耗尽了

在这种情况下,.NET默认线程池1秒增加一个线程,刚增加的线程又被拿去跑任务了,然后MySqlConnection的Open方法就卡住了!是不是Open这个方法需要线程?而线程池中没有线程给它用了?
正常的是类似这样的,任务来了,就干活:


不正常的是类似这样的

(我写的Dapper.LiteSql,以前是使用using的没问题,后来为了干掉using,就加了连接池,就用到锁,锁的颗粒度大,就是像下面这样样子,比这还过份,后来把锁的粒度搞小一点,有改善,但还是不行,最后接着优化,除了用线程安全队列外(它底层也是锁),能不用锁就不用锁(有时候不用锁实现不了功能,又必须用),就解决了这个问题),下面是Fast.Framework优化前的测试:

耗时的全是外部调用:

3. 并发查询测试


我怀疑MySqlConnector的异步有BUG

Fast.Framework测试程序,明明用了Task.WaitAll(tasks.ToArray());,别人“并发查询 完成”都是最后输出,它还没完就输出了,这个3秒是不对的,实际是5秒,确实快,是不是有一部分是MySqlConnector真异步的功劳呢?
FreeSql报错(后续发现是数据库连接配置问题)

卡死了,报错。我之前大致看过它这个ObjectPool源码,没仔细研究,没太看懂,就觉得写的挺牛X,我还不太会。
补充:

上面是我坑了FreeSql,其它并发查询都是查200次,FreeSql是查了1000次,所以卡死,后改为查200次,不会卡死了,但是报错:


4. Winform确实会容易卡

主要是日志输出到界面导致的,减少输出日志的频率会好一些,查询1000次我刚试了一下Dapper.LiteSql,50多秒结束,其它就没测了。


结论

有问题很正常

我也只是测试了一种恶劣情况,说不定别人测试其它的恶劣情况,Dapper.LiteSql就报错了。
后续

这种写法是不是会好一点

try catch要加的,不过try catch可能也会有影响吧。下面是Dapper.LiteSql测试:
  1. List<Task> tasks = new List<Task>();
  2. foreach (SysUser item in userList)
  3. {
  4.     var task = Task.Factory.StartNew(async user =>
  5.     {
  6.         try
  7.         {
  8.             await _db.GetSession().InsertAsync(user);
  9.         }
  10.         catch (Exception ex)
  11.         {
  12.             Log(ex.ToString());
  13.         }
  14.     }, item);
  15.     tasks.Add(task);
  16. }
  17. Task.WaitAll(tasks.ToArray());
复制代码

Fast.Framework的测试
  1. List<Task> tasks = new List<Task>();
  2. foreach (SysUser item in userList)
  3. {
  4.     var task = Task.Factory.StartNew(async user =>
  5.     {
  6.         try
  7.         {
  8.             await GetDbContext().Insert((SysUser)user).ExceuteAsync();
  9.         }
  10.         catch (Exception ex)
  11.         {
  12.             Log(ex.ToString());
  13.         }
  14.     }, item);
  15.     tasks.Add(task);
  16. }
  17. Task.WaitAll(tasks.ToArray());
复制代码

题外话:

为什么.NET程序员喜欢写ORM?为什么.NET程序员ORM的话题经久不衰?
因为大家都自称增删改查程序员,增删改查做的多,但凡.NET老程序员,哪个没写过ADO.NET?ADO.NET的“脏活”都干的很多,非常熟悉了。
试想一下,写一写Redis的?ElasticSearch的?Kafka的?真没干过“脏活”,都是拿库来调用,简单的很,要写的话,就要去看文档,而且是英文文档,怎么写?其实大家并不是喜欢造轮子,只是对特别熟悉的东西造轮子,大家对ADO.NET就特别熟悉,增删改查的各种“脏活”没少写。
说到“脏活”,Socket的我写过一点,当时做一个.NET的WebApi,给Android端推消息(内网),没有用SuperSocket这种现成的,就自己写的Socket,项目专用,不通用,就没法做成开源库给大家用。因为应对的是特定需求,要写个通用的,就很费事了。我写个通用的给谁用?我自己可能都不愿意用。但ORM这东西,就算别人不用,我真还自己用!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

数据人与超自然意识

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表