ASP.NET MVC 性能优化总结

随着项目中各项功能的增加,系统性能越来越糟糕,于是决定对系统做性能优化。现性能优化的相关工作记录下来。
 
一、如何监测性能问题:
1. dotTrace: 一款性能测试工具,能够记录程序执行过程中各个方法的调用情况及所花时间等,好像不能记录网站加载情况。
2. miniProfiler: StackOverflow的一款开源产品,需要在项目中引用做相应的配置,不光能够记录网站的加载情况,还能记录EF的执行情况。适合在开发过程中应用。网址:http://miniprofiler.com/
3. Chrome的开发工具可监控各项资源的加载情况。
 
二、优化方法:
1. 对静态资源添加客户端缓存。
<staticContent>
      <remove fileExtension=".woff" />
      <!-- In case IIS already has this mime type -->
      <mimeMap fileExtension=".woff" mimeType="application/x-font-woff" />
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
    </staticContent>
 
2. 添加OutputCache。
filters.Add(new OutputCacheAttribute
            {
                NoStore = true,
                Duration = 10,
                VaryByParam = "*"
            });
 
3. 压缩合并JS,CSS:
利用ScriptBundle,StyleBundle,在BundleConfig文件中注册需要引用的静态资源.
 
4. 对EF加二级缓存:
引用DLL包:EFCache.dll
并在项目中添加如下类,具体步骤参考http://blog.3d-logic.com/2014/03/20/second-level-cache-for-ef-6-1/
public class Configuration : DbConfiguration
    {
        internal static readonly InMemoryCache Cache = new InMemoryCache();

        public Configuration()
        {
            var transactionHandler = new CacheTransactionHandler(Cache);

            AddInterceptor(transactionHandler);

            Loaded +=
              (sender, args) => args.ReplaceService<DbProviderServices>(
                (s, _) => new CachingProviderServices(s, transactionHandler));
        }

        public static int GetCountOfCache()
        {
            return Cache.Count;
        }
    }

原文地址:https://www.cnblogs.com/cxp9876/p/3803720.html