Learning ColdFusion 9: Virtual File System vs. Actual File System

Posted July 17, 2009 at 5:55 PM

Tags: ColdFusion

Earlier today, I blogged about the new Virtual File System available in ColdFusion 9. I explained that the virtual file system was just like the actual file system except that you were writing to the "ram://" disk rather than to the hard drive. I wrote that the biggest benefit of this new virtual file system was the performance gain - that it was much less expensive to read and write to RAM than it was to an actual disk. I knew this was true, but I didn't have any numbers to back up the statement. As such, I decided to run a very simple speed test comparing the performance of the virtual file system to the actual file system.

This is not a scientific test by any means; nor, did I run this under any kind of load. But, as both file systems were tested in the same environment, I believe that we can still take away some meaning from the results:

 Launch code in new window » Download code as text file »

  • <!---
  • Param the url variable for determining which file system
  • to be testing with this execution.
  • --->
  • <cfparam name="url.vfs" type="boolean" default="false" />
  •  
  • <!--- Depending on the URL, figure out the directory. --->
  • <cfif url.vfs>
  •  
  • <!--- Use the virtual file system. --->
  • <cfset workDirectoryPath = "ram://temp/" />
  •  
  • <cfelse>
  •  
  • <!--- Use the actual file system. --->
  • <cfset workDirectoryPath = (
  • getDirectoryFromPath( getCurrentTemplatePath() ) &
  • "temp/"
  • ) />
  •  
  • </cfif>
  •  
  •  
  • <!--- Check to make sure our target directory exists. --->
  • <cfif !directoryExists( workDirectoryPath )>
  •  
  • <!--- Create it so that our file writes don't fail. --->
  • <cfdirectory
  • action="create"
  • directory="#workDirectoryPath#"
  • />
  •  
  • </cfif>
  •  
  •  
  • <!---
  • Read in a file from the actual file system. This is a
  • moderetly sized file at just under 700KB.
  • --->
  • <cfset dataFile = fileRead( expandPath( "./target.jpg" ) ) />
  •  
  •  
  • <!--- ----------------------------------------------------- --->
  • <!--- ----------------------------------------------------- --->
  •  
  • <!--- Get the start time. --->
  • <cfset startTickCount = getTickCount() />
  •  
  • <!--- Loop over the given file system a number of --->
  • <cfloop
  • index="iteration"
  • from="1"
  • to="1000"
  • step="1">
  •  
  • <!--- Create a path variable for a new file. --->
  • <cfset newFilePath = "#workDirectoryPath##iteration#.jpg" />
  •  
  • <!--- If this new file currently exists, then delete it. --->
  • <cfif fileExists( newFilePath )>
  •  
  • <!--- Delete the existing file. --->
  • <cfset fileDelete( newFilePath ) />
  •  
  • </cfif>
  •  
  • <!--- Write the data file to target file system. --->
  • <cfset fileWrite( newFilePath, dataFile ) />
  •  
  • <!--- Read the new file into memory. --->
  • <cfset newFile = fileRead( newFilePath ) />
  •  
  • <!--- Delete the new file. --->
  • <cfset fileDelete( newFilePath ) />
  •  
  • </cfloop>
  •  
  • <!--- Get the end time. --->
  • <cfset endTickCount = getTickCount() />
  •  
  • <!--- ----------------------------------------------------- --->
  • <!--- ----------------------------------------------------- --->
  •  
  •  
  • <!--- Output results. --->
  • <cfoutput>
  •  
  • <p>
  • <cfif url.vfs>
  • Virtual File System
  • <cfelse>
  • Actual File System
  • </cfif>
  • </p>
  •  
  • <p>
  • <strong>Results:</strong>
  •  
  • #numberFormat( (endTickCount - startTickCount), "," )#
  • </p>
  •  
  • </cfoutput>

As you can see from the code, I am running several file checks, reads, and writes over a thousand iterations. I ran this 5 times with the actual file system and another 5 times with the virtual file system (discarding the first few results for compilation and optimization overhead). Here are my results:

Actual File System

  1. Results: 22,407
  2. Results: 23,969
  3. Results: 22,703
  4. Results: 23,860
  5. Results: 20,922
  6. Average: 22,772

Virtual File System

  1. Results: 5,500
  2. Results: 5,594
  3. Results: 5,844
  4. Results: 5,860
  5. Results: 6,000
  6. Average: 5,759

As you can see, accessing ColdFusion 9's Virtual File System is about 4 times faster than accessing the actual file system. Not a bad performance boost for transient files.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jul 17, 2009 at 7:12 PM // reply »
6 Comments

Only 4 times faster? I was expecting several orders of magnitude faster.. this is somewhat surprising.


Jul 17, 2009 at 7:48 PM // reply »
102 Comments

@Drew,
My sentiments exactly. The gains are nice but I figured they would be more.


Jul 18, 2009 at 2:23 AM // reply »
34 Comments

4 times faster is extremely fast. Imagine if a Lambo could go 4 times faster. WOW!

But I have a question how could you put this to practical use? What information could you store in the RAM?


Jul 18, 2009 at 3:36 AM // reply »
2 Comments

Why is the RAM time increasing. Does it keep increasing if it ran 10 times ?


Jul 18, 2009 at 8:37 AM // reply »
2 Comments

@ Drew & Gareth

I agree with Jody....4 times faster is fast! Not sure what else you would want or could expect.

@ Jody

I am building a back end photo system for a shopping cart admin. Meaning I am building a system that allows admin users with the lack of Photoshop skills to crop, resize, add borders, text and other enhancements to there photos as they upload them into the front end of the store. (all with cfimage and functions!!) Now with the virtual file system I can easily add a small preview window for the user to see their results before finalizing the work. Yes...I could have done this before the virtual way, but it will have a little more zoom zoom this way.


Jul 18, 2009 at 10:03 AM // reply »
102 Comments

You're writing to RAM though (measured in nanoseconds) vs writing to disk (measured in milliseconds). That is a 1000 times faster access. So, yes, I agree that 4 times faster is "faster", but accessing it from ram vs accessing it from the hard drive isn't showing the gains I was expecting.


Jul 18, 2009 at 12:02 PM // reply »
6,516 Comments

Also, to keep in mind, I am running ColdFusion on my work laptop as a stand alone install. This is probably not the most powerful machine.


Jul 18, 2009 at 12:27 PM // reply »
64 Comments

Hi Ben
I'd be interested in seeing the results if you individually time each discrete action (so separate timings for the deletes, writes and reads), rather than timing all four actions together.

--
Adam


Jul 18, 2009 at 12:33 PM // reply »
6,516 Comments

@Adam,

That could be interesting. I'll see if I can run some tests.


Jul 18, 2009 at 2:30 PM // reply »
41 Comments

I'm curious if this is a faster approach to image processing/resizing.

Anyone run image resizing tests?


Jul 18, 2009 at 3:43 PM // reply »
6,516 Comments

@David,

Only if it requires intermediary file writes. There's nothing about the file system that will speed up the image processing itself.


Jul 18, 2009 at 11:52 PM // reply »
3 Comments

Speaking of file systems, I'm working on doing a fix to your system where you can view images, and possibly edit text. All in good time thou.


Jul 19, 2009 at 1:25 AM // reply »
45 Comments

I concur with Mikkel, is there a memory leak in the VFS functionality?


Jul 19, 2009 at 11:46 AM // reply »
6,516 Comments

@Andrew,

I don't think so. If you look at the second test, it's actually lower than the first. I think the ups and downs are just the consequence of other background processes taking processing power.


Jul 22, 2009 at 1:00 AM // reply »
6 Comments

You're using a mapping for ease and cleaner syntax but this is only accessable from one instance (instance specific) and not across CF servers in a cluster right? CF instances in a multi-node enviornment wouldn't be able to share these "files" written to RAM if they had the same mappings would they?


Jul 22, 2009 at 7:48 AM // reply »
6,516 Comments

@Ted,

I don't know much about clustering, but you're probably correct.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »
Nov 20, 2009 at 4:53 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, you can ramp up the security by turning on J2EE session which gives you a third set of numbers other than CFID/CFTOKEN. There's a reason why ACF put this in place (other than just session replic ... read »
Nov 20, 2009 at 4:52 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Case in point, Ben, you may not be aware of this, but in Railo - OnApplicationStart() & OnSessionStart() act differently than in ACF. ACF does: OnApplicationStart (1st hit) OnSessionStart (1st and e ... read »