Appfabric Cache的执行速度比SQL Server 2008快4倍?

use*_*312 8 c# comparison performance distributed-caching appfabric

我正在运行一个测试,我将比较获取时间b/w appfabric和SQL Server 2008,看起来appFabric的执行时间比SQL Server慢4倍.

我有一个SQL Server 2008安装程序,它只包含一个包含4列(全部nvarchar)的表.该表有6000行.我在appfabric缓存中插入相同的行(作为CLR serializable obj).我正在运行一个循环来获取数据x次.

这是代码

public class AppFabricCache
{
readonly DataCache myDefaultCache;

public AppFabricCache()
{
//-------------------------
// Configure Cache Client 
//-------------------------

//Define Array for 1 Cache Host
var servers = new List<DataCacheServerEndpoint>(1);

//Specify Cache Host Details 
//  Parameter 1 = host name
//  Parameter 2 = cache port number
servers.Add(new DataCacheServerEndpoint(@"localhost", 22233));

//Create cache configuration
var configuration = new DataCacheFactoryConfiguration();

//Set the cache host(s)
configuration.Servers = servers;

//Set default properties for local cache (local cache disabled)
configuration.LocalCacheProperties = new DataCacheLocalCacheProperties();

//Disable exception messages since this sample works on a cache aside
DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

//Pass configuration settings to cacheFactory constructor
DataCacheFactory myCacheFactory = new DataCacheFactory(configuration);

//Get reference to named cache called "default"
myDefaultCache = myCacheFactory.GetCache("default");
}

public bool TryGetCachedObject(string key, out object value)
{
value = myDefaultCache.Get(key);
bool result = value != null;
return result;
}

public void PutItemIntoCache(string key, object value)
{
myDefaultCache.Put(key, value, TimeSpan.FromDays(365));
}

}
Run Code Online (Sandbox Code Playgroud)

这是从缓存中获取数据的循环

public double RunReadStressTest(int numberOfIterations, out int recordReadCount)
{
recordReadCount = 0;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < numberOfIterations; i++)
{
for (int j = 1; j <= 6000; j++)
{
string posId = "PosId-" + j;
try
{
object value;
if (TryGetCachedObject(posId, out value))
recordReadCount++;
}
catch (Exception e)
{
Trace.WriteLine("AS%% - Exception - " + e.Message);
}
}
}
sw.Stop();
return sw.ElapsedMilliseconds;
}
}
Run Code Online (Sandbox Code Playgroud)

我有完全相同的逻辑从SQL Server检索数据.它创造了一个

sqlCommand = 'Select * from TableName where posId = 'someId'' 
Run Code Online (Sandbox Code Playgroud)

结果如下......

SQL Server 2008 R2  Reading-1(ms)   Reading-2(ms)   Reading-3(ms)   Average Time in Seconds
 Iteration Count = 5    2528              2649            2665                 2.614
 Iteration Count = 10   5280              5445            5343                 5.356
 Iteration Count = 15   7978              8370            7800                 8.049333333
 Iteration Count = 20   9277              9643            10220                9.713333333

AppFabric                 Reading-1         Reading-2   Reading-3   Average Time in Seconds
Iteration Count = 5        10301            10160            10186                10.21566667
Iteration Count = 10       20130            20191            20650                20.32366667
Iteration Count = 15       30747            30571            30647                30.655
Iteration Count = 20       40448            40541            40503                40.49733333
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么吗?为什么这么慢?

Cyb*_*axs 0

我认为你的测试有偏差并且你的结果不是最佳的。

关于分布式缓存

  • 本地缓存:您已禁用本地缓存功能。缓存对象总是从服务器检索;网络传输和反序列化是有成本的。
  • BulkGet:BulkGet与小对象一起使用时可以提高性能,例如,检索许多大小为 1 - 5KB 或更小的对象时。
  • 无数据压缩:AppFabric 和缓存客户端之间无压缩。检查这个

关于您的测试

另一件重要的事情是,您没有测试相同的东西:在一侧您测试 SELECT * ,而另一侧您测试 N x GET ITEM 。