Megosztás a következőn keresztül:


Performance Comparison Between Reading from Disk vs. AppFabric Cache

This sample shows that it is more efficient to store and read from the AppFabric cache than from disk.

The sample reads a number of image files to simulate user requests to access the files from the disk or from the cache, and compares the time taken to serve the image files.

Note

Samples are provided for educational purposes only. They are not intended to be used in a production environment and have not been tested in a production environment. Microsoft does not provide technical support for these samples.

Prerequisites

Ideally, users should have knowledge of Windows PowerShell scripting.

The sample assumes the following:

  • Windows PowerShell 2.0 is installed.

  • Microsoft AppFabric 1.1 for Windows Server is installed. (This also adds the required DLLs to the GAC.)

Configure the AppFabric Cache

If you have not done so already, configure the AppFabric cache by using the Microsoft AppFabric 1.1 for Windows Server configuration tool (use default settings). The sample assumes that an AppFabric cache is configured with default settings (cacheHostName=AppFabricCachingService and cacheName=default).

Start the Cache Cluster

  1. Open a Windows PowerShell command window in elevated mode and run the following command to add the Distributed Cache administration module:

    Import-Module DistributedCacheAdministration
    
  2. Run the following command to grant your user account access to the cache cluster as a client. Supply your user and domain name.

    Grant-CacheAllowedClientAccount domain\username
    
  3. Use Get-CacheAllowedClientAccounts command to verify your user account has been granted access.

  4. Start the cluster by using Start-CacheCluster command.

Build and Run This Sample

The sample automatically links to the AppFabric Cache DLLs (Microsoft.ApplicationServer.Caching.Client.dll and Microsoft.ApplicationServer.Caching.Core.dll) from the GAC.

  1. Open the DiskVersusCachePerformance.sln solution in Visual Studio.

  2. Open a command prompt and change directory to : <Location of the sample>\CacheVersusDiskPerformance\DiskVersusCachePerformance\bin\Debug

  3. This sample needs to be passed one command-line parameter: the path to the fileshare directory, part of the DiskVersusCachePerformance folder. (You can also add this to the project properties, in the section “command line arguments”).

  4. Run the exe to start the sample test:

    <Location of the sample>\CacheVersusDiskPerformance\DiskVersusCachePerformance\bin\Debug> DiskVersusCachePerformance.exe ..\..\..\fileshare
    

The actions performed by the sample are described in the useractions.xml file in the fileshare folder. The following actions are supported.

Action Parameter(s) Description

PageSize

Size

Set the page size (number of files to read)

LogIn

UserName

Log in the user

LogOut

UserName

Log out the user

ViewAlbum

AlbumIndex

Move to folder “album[Index]”

ViewPage

PageIndex

Read files [PageIndex*PageSize - PageIndex*(PageSize+1)-1]

ViewNextPage

N/A

Move to next set of files [(PageIndex+1)*PageSize - PageIndex*(PageSize+2)-1]

ViewPrevPage

N/A

Move to previous set of files

A sequence of actions can be defined as follows.

  <useraction>    <action>PageSize</action>     <data>500</data>      </useraction>
  <useraction>    <action>LogIn</action>        <data>tom</data>      </useraction>
  <useraction>    <action>ViewAlbum</action>    <data>0</data>        </useraction>
  <useraction>    <action>ViewPage</action>     <data>0</data>        </useraction>
  <useraction>    <action>ViewNextPage</action> <data>null</data>     </useraction>
  <useraction>    <action>ViewPrevPage</action> <data>null</data>     </useraction>
  <useraction>    <action>LogOut</action>       <data>tom</data>      </useraction>

The preceding segment is interpreted as:

  1. Set number of items per page as 500

  2. Log in tom (allows the logged-on user to access the page)

  3. Select album0

  4. View first page (read files 0-499 from the disk or cache)

  5. View next page (try to read files 500-999 from disk)

  6. View previous page (re-read files 0-499 from the cache now that files are loaded to the cache)

  7. Log out (revert permission granted)

At each logout, the sample reports a performance comparison of accessing the files from the disk versus from the AppFabric cache.

Note

For each subsequent run of this sample, use the Restart-CacheCluster command to clear the cache, and wait for 10-15 seconds.

Note

During the sample run you may encounter messages such as [size2_il.cur-DiskRead- error 2 Delay (ticks) 7128]. The error shows the difference between file size reported by FileInfo and the actual size of the file read from the disk. This difference can be ignored because data stored in the cache is the same size as data read from the disk.

Troubleshooting

If data is not getting added into the cache, it could be that the cache’s eviction policy is getting in the way. To ensure that the cache does not have any eviction policy set, please run the following commands to remove the default cache and recreate it with no eviction policy:

Remove-Cache -CacheName default
New-Cache -CacheName default -Eviction None

Remove This Sample

Restart the cache cluster by using the Restart-CacheCluster command to clean up data from the cache.

See Also

Other Resources

Samples (AppFabric 1.1 Caching)

  2012-09-12