system.runtime.caching性能 [英] Performance of system.runtime.caching

查看:331
本文介绍了system.runtime.caching性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比较了system.runtime.caching在.NET 4.0和企业库缓存块,并让我惊讶的是从缓存中读取的物品的大型数据集合时执行可怕相比的性能。

I have compared the performance of system.runtime.caching in .NET 4.0 and the Enterprise Library Caching Block and to my surprise it performs terribly in comparison when fetching large data collections from cache items.

企业库取100个对象大约0,15ms,10000对象约0,25ms。这是快速和自然的一个进程内缓存,因为没有实际的数据需要被复制(仅参考)。

Enterprise Library fetches 100 objects in about 0,15ms, 10000 objects in about 0,25ms. This is fast, and natural for an in-process cache because no data actually needs to be copied (only references).

在.NET 4.0的缓存读取100个对象中约25毫秒,10000对象约1500毫秒!这是非常缓慢相比,这让我怀疑缓存就是这样做出来的进程。

The .NET 4.0 caching fetches 100 objects in about 25ms, 10000 objects in about 1500ms! This is terribly slow in comparison and it makes me suspect the caching is done out-of-process.

我缺少一些配置选项,例如使过程中的缓存,或者是企业库缓存块真的这么多快?

Am I missing some configuration option, for example to enable in-process caching, or is the Enterprise Library Caching Block really this much faster?

更新

下面是我的标杆:

首先,我从数据库中的数据加载到缓存(独立于基准)。

First, I load the data from the database to the cache (separate from the benchmark).

我周围使用get方法的计时器来衡量的毫秒数:

I use a timer around the get methods to measure the time in milliseconds:

EnterpriseLibrary缓存

Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager _cache;

public void InitCache(){
    _cache = CacheFactory.GetCacheManager("myCacheName");
}

public void Benchmark(){
    HighPerformanceTimer timer = new HighPerformanceTimer();
    timer.Start();
    myObject o = (myObject)_cache.GetData(myCacheKey);
    timer.Stop();
    Response.Write(timer.GetAsStringInMilliseconds());
}

.NET 4.0缓存

    System.Runtime.Caching.MemoryCache _cache;

    public void InitCache(){
        _cache = new MemoryCache("myCacheName");
    }

    public void Benchmark(){
        HighPerformanceTimer timer = new HighPerformanceTimer();
        timer.Start();
        myObject o = (myObject)_cache.Get(myCacheKey);
        timer.Stop();
        Response.Write(timer.GetAsStringInMilliseconds());
    }

基准执行1000次,计算平均时间来获取对象,以保证测试的可靠性。计时器是我用的,任何定时器计数毫秒应该做一个自定义的计时器。

The benchmark is executed 1000 times to compute average time to fetch the object to ensure reliability of the test. The timer is a custom timer I use, any timer counting milliseconds should do.

有趣的是,myObject的有多次提及。如果有涉及到我明白了为什么不同的表现该对象(如在分布式缓存)任何序列化,但这些都是在进程缓存理论上应该不会有很多重大分歧的。

The interesting thing is that the "myObject" has numerous references. If there was any serialization involved I'd understand why the performance differs for this object (like in distributed caching), but these are both in-process caches that theoretically should work without many major differences at all.

推荐答案

我的猜测是,你的缓存内容或政策的细节是不一样的。如果没有看到设置,或插入,很难说究竟如何。

My guess is that the details of your cache contents or policies are not the same. Without seeing the setup, or the inserts, it's hard to say exactly how.

无论如何,这两个库具有不同的性能特点,孰优孰劣显然要看具体情况。

Regardless, the two libraries have different performance characteristics, and which one is better clearly depends on the situation.

也许我的测试(code以下)过于简单重新presentative,但它在我的机器上运行,的MemoryCache大约是10倍的更快的。

Probably my test (code below) is too simple to be representative, but with it running on my machine, MemoryCache is roughly 10x faster.

class Program
{        
    const string myCacheKey = "foo";
    static ICacheManager _elCache;        
    static MemoryCache _rtCache;
    public static void InitCache()
    {            
        _elCache = CacheFactory.GetCacheManager();
        _elCache.Add(myCacheKey, new object());

        _rtCache = new MemoryCache("cache");
        _rtCache.Add(myCacheKey, new object(), new CacheItemPolicy());
    }
    public static string ElBenchmark(int n)
    {
        Stopwatch timer = new Stopwatch();
        timer.Start();
        for (int i = 0; i < n; i++)
        {
            object o = _elCache.GetData(myCacheKey);
        }
        timer.Stop();
        return timer.ElapsedTicks.ToString();
    }
    public static string RtBenchmark(int n)
    {
        Stopwatch timer = new Stopwatch();
        timer.Start();
        for (int i = 0; i < n; i++)
        {
            object o = _rtCache.Get(myCacheKey);
        }
        timer.Stop();
        return timer.ElapsedTicks.ToString();
    }
    static void Main(string[] args)
    {
        while (true)
        {
            InitCache();
            StringBuilder sb = new StringBuilder();
            System.Diagnostics.Debug.Write("EL: " + ElBenchmark(10000));
            System.Diagnostics.Debug.Write("\t");
            System.Diagnostics.Debug.Write("RT: " + RtBenchmark(10000));
            System.Diagnostics.Debug.Write("\r\n");
        }
    }
}


<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="cachingConfiguration"
         type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
  </configSections>
  <cachingConfiguration defaultCacheManager="MyCacheManager">
    <cacheManagers>
      <add name="MyCacheManager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       expirationPollFrequencyInSeconds="60"
       maximumElementsInCacheBeforeScavenging="50000"
       numberToRemoveWhenScavenging="1000"
       backingStoreName="NullBackingStore" />
    </cacheManagers>
    <backingStores>
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
       name="NullBackingStore" />
    </backingStores>
  </cachingConfiguration>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>  
</configuration>

这篇关于system.runtime.caching性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆