2012-09-28 52 views
8

我正在運行一個測試,比較獲取時間b/w appfabric和SQL Server 2008,並且看起來appFabric比SQL Server執行速度慢4倍。Appfabric Cache比SQL Server 2008執行速度慢4倍?

我有一個SQL Server 2008安裝程序,它只包含一個包含4列的表(全部爲nvarchar)。該表有6000行。我在appfabric緩存中插入相同的行(如CLR可序列化的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)); 
} 

} 

,這裏是循環從緩存

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; 
} 
} 

獲取數據我有完全一樣的邏輯從SQL Server檢索數據。它創建了一個

sqlCommand = 'Select * from TableName where posId = 'someId'' 

下面是結果...

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 

我失去了一些東西在這裏?爲什麼這麼慢?

回答

0

我認爲你的測試是有偏見的,你的結果是非最優的。

關於分佈式緩存

  • 本地緩存:已禁用local cache功能。緩存對象始終從服務器中檢索;網絡傳輸和反序列化都有成本。
  • BulkGet:BulkGet與小對象一起使用時可以提高性能,例如,檢索大小爲1至5KB或更小的許多對象時的性能。
  • 沒有數據壓縮:AppFabric和緩存客戶端之間沒有壓縮。檢查this

關於你的測試

另一個重要的事情是,你不是測試同樣的事情:一方面你測試SELECT *以及與對方你測試的N×GET項目。

+1

我不認爲你之前使用過d-cache。只知道一些東西是不夠的。 >>如果我啓用本地緩存,那將是一個不公平的測試。 >>散裝得到 - 我可以做同樣的SQL服務器以及我敢肯定,這將是顯着快。 Sql Server只不過是檢索一行而已。 >>爲什麼我要啓用壓縮只有6K記錄。每個只有4個字符串存儲。 – user1707312

+0

@ user1707312我不明白你的意見。你能解釋一下嗎?我不是AppFabric的主人。我從一年以後就使用了它。如果您以前使用過d-cache,那麼您應該也知道使用d-cache並不是爲了提高性能,而是提高可伸縮性。使用單個數據表的單個客戶端的for循環中的「加載測試」不是最好的方法。 – Cybermaxs

+0

忘記 - 讓我們不要進入它。從性能角度考慮,AppFabric與Memcache/Couchbase進行比較。 – user1707312

1

這可能是由.Net的內置序列化引起的。

.Net序列化利用反射,反過來又有非常差性能差。我建議查看使用自定義書面序列化代碼。

2

區別在於網絡開銷。在您的SQL示例中,您跳過網絡一次並選擇N行。在您的AppFabric示例中,您跳過網絡PER RECORD而不是批量。這是不同的。爲了證明這一點,請暫時將您的記錄作爲列表存儲在AppFabric中,並且一次只獲取列表,或者使用AppFabric Bulk API在一次請求中選擇所有記錄 - 這應該解釋很多差異。

+1

不要以爲他這樣做:他似乎是逐行檢索數據:sqlCommand ='Select * from TableName where posId ='someId'' –