A before and after comparison of my application’s FLV indexing performance between java.io streams and java.nio file channels.
In my application at xtendx AG, there is some code that indexes uploaded FLV (Flash video) files to determine the byte mark of each key-frame which are spaced out every second or so by our encoding process. The index allows client players to request an flash video at an arbitrary second position with in the FLV. This indexing needs to be done, for the sake of simplicity, once and only once before a file is streamed to an end user. Therefor, the first attempt to view a file incurs a non-trivial delay as the server indexes the file. It needs to be fast! OK, that is the “why”.
The indexing process is very straight forward:
- Open the file, validate the header, read in meta data
- Read in key frame meta data (type, temporal position, frame size)
- Take note of temporal position and absolute position of start byte of key frame
- Skip ahead to start of next key frame
- Jump to step 2 until end of file
- Save temporal position / byte position map
As one can guess, the file access is not exactly serial nor is it exactly random in nature. Because of the relatively large distance between key frames (up to thousands of bytes), I am thinking that the file access would be more appropriately categorized as random despite always moving forward.
Before changing the code, I whipped up some quick just-read-every-byte-in-file-quickly micro-benchmarks on my old MacBook to see what I could expect, and understand how to use FileChannels correctly. Every looked good.
In the latest release of Simplex Media Server, v2.3, the indexing code was refactored to use java.nio’s FileChannels. A little bit of strategic logging in both the new and old versions captured some performance metrics from one of our production systems. Real world numbers rock!
It is probably worth documenting the relevant hardware and software involved:
|Make & Model||HP DL380 G5|
|CPU||(1) 1.86GHz Xeon Quad-core|
|Operating System||RHEL4 (64-bit)|
|File System||SAS RAID-10 w/ (4) 10K RPM HDD|
|Java Version||Sun JDK 1.5.12 (64-bit)|
|Relevant JVM Options||-Xms768m -Xmx768m -Xincgc|
y-axis = indexing rate in MBps
- I had graphed the results file size vs. index rate with the expectation of seeing slow rates for smaller files sizes due to the overhead of setting up the i/o. That cost probably is only apparent with very small files relative to what we are working with here.
- As a consequence of the above, both trend lines are relatively flat, with java.io performing ~37MBps and java.nio at ~80MBps. My refactoring has doubled the speed!
- It would be nice to have a larger data set, especially for file sizes larger than 50MB. That would make me comfortable about the general accuracy of y-axis rate values
- The minimum/maximum range for the indexing rate is larger than I expected, even for a production system under load. I suspect it was a mistake to ignore the video characteristics of the media files themselves, namely the bit rate. (A higher bit rates mean larger gaps between key frames.) A follow up post comparing the above values with number of index points on a z-axis would be interesting.