你的位置:首页 > ASP.net教程

[ASP.net教程]测试EntityFramework,Z.EntityFramework.Extensions,原生语句在不同表现

Insert测试,只测试1000条的情况,多了在实际的项目中应该就要另行处理了。

using System;using System.Collections.Generic;using System.Configuration;using System.Diagnostics;using System.Linq;using EE.Service.DbAccess;using EE.Service.DbEntity;using MySql.Data.MySqlClient;namespace EE.Services.ConsoleTest.DbAccessTests{  public class EFPerformanceTest  {    public static void Run()    {      InsertTest();    }    static void Watcher<T>(Stopwatch stopwatch,Func<IEnumerable<T>,int> func, IEnumerable<T> list)    {      if(stopwatch == null) stopwatch = new Stopwatch();      stopwatch.Reset();      Console.WriteLine($"[{DateTime.Now:HH:mm:ss ffffff}] {func.Method.Name} begin");      stopwatch.Start();      var val = func(list);      stopwatch.Stop();      Console.WriteLine($"[{DateTime.Now:HH:mm:ss ffffff}] process count: {list.Count()} effectd:{val} elapsed:{stopwatch.Elapsed}  {func.Method.Name} end");      //Console.WriteLine($"[{DateTime.Now:HH:mm:ss ffffff}] {func.Method.Name} end");    }    static void InsertTest()    {      var list = new List<SmsLog>();      for (int i = 0; i < 1000; i++)      {        list.Add(new SmsLog() { AppId = $"{i}", CreateTime = DateTime.Now });      }      var sw = new Stopwatch();      Watcher(sw, EFInit, list.GetRange(0, 10));      Watcher(sw, EFBulkInsert, list);      Watcher(sw, EFInsertWithNoState, list);      Watcher(sw,EFInsert,list);      Watcher(sw, MySqlInsert, list);      //Watcher(sw, EFInsert, list);    }    /// <summary>    /// 初始化一下,尽量减少EF初始化时间的影响    /// </summary>    /// <param name="items"></param>    /// <returns></returns>    static int EFInit(IEnumerable<SmsLog> items)    {      using (var db = new ServiceLogDbContent())      {        foreach (var item in items)          db.SmsLogs.Add(item);        var val = db.SaveChanges();        return val;      }    }    static int EFInsert(IEnumerable<SmsLog> items)    {      using (var db = new ServiceLogDbContent())      {        foreach (var item in items)          db.SmsLogs.Add(item);        var val = db.SaveChanges();        return val;      }    }    static int EFInsertWithNoState(IEnumerable<SmsLog> items)    {      using (var db = new ServiceLogDbContent())      {        db.Configuration.AutoDetectChangesEnabled = false;        db.Configuration.ValidateOnSaveEnabled = false;        db.Configuration.LazyLoadingEnabled = false;        db.Configuration.ProxyCreationEnabled = false;        foreach (var item in items)          db.SmsLogs.Add(item);        var val = db.SaveChanges();        return val;      }    }    static int EFBulkInsert(IEnumerable<SmsLog> items)    {      using (var db = new ServiceLogDbContent())      {        //db.Configuration.AutoDetectChangesEnabled = false;        //db.Configuration.ValidateOnSaveEnabled = false;        foreach (var item in items)          db.SmsLogs.Add(item);        db.BulkSaveChanges();        return 0;      }    }    static int MySqlInsert(IEnumerable<SmsLog> items)    {      var val = 0;      var connStr = ConfigurationManager.ConnectionStrings["EEServiceLogDb"].ConnectionString;      using (var conn = new MySqlConnection(connStr))      {        conn.Open();        foreach (var item in items)        {          val += MySqlHelper.ExecuteNonQuery(conn, "Insert into SmsLogs(AppId,CreateTime)values(@appId,@createTime)", new MySqlParameter("@appId", item.AppId), new MySqlParameter("@createTime", item.CreateTime));        }        conn.Close();      }      return val;    }  }}

View Code

本机开发机,DB在内网服务器上。测试结果:

可以看到,EF的初始执行还是蛮耗时间的。扩展的BuilInsert要比循环的MySqlInsert语句效率还好一些。关掉状态跟踪与默认情况,差别不大(不知是不是我写的有问题)。