ASP.NET MVC 性能优化总结

随着项目中各项功能的增长,系统性能愈来愈糟糕,因而决定对系统作性能优化。现性能优化的相关工做记录下来。
 
1、如何监测性能问题:
1. dotTrace: 一款性能测试工具,可以记录程序执行过程当中各个方法的调用状况及所花时间等,好像不能记录网站加载状况。
2. miniProfiler: StackOverflow的一款开源产品,须要在项目中引用作相应的配置,不光可以记录网站的加载状况,还能记录EF的执行状况。适合在开发过程当中应用。网址: http://miniprofiler.com/
3. Chrome的开发工具可监控各项资源的加载状况。
 
2、优化方法:
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;
        }
    }
相关文章
相关标签/搜索