My web service writes several thousands of transactions per minute and we save them on the hd.
I was testing different ways to save these files and I made some tests with standard IO and with MemoryMapped files. In my results, writing files (20 k text files) with MemoryMapped files is about 4x faster than standard IO and I was not able to find any disadvantages.
As I have not so much experience with this technology, do you think I may face any problem using them or you don't see any disadvantage?
Thanks!
EDIT 1, here the source:
namespace FileWritingTests.Writers {
public class MappedFileWriter : ITestWriter {
public void Write(string content, string path,string id) {
Byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
using (var fileStream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
using (MemoryMappedFile memoryMapped = MemoryMappedFile.CreateFromFile(fileStream, id, data.Count(),
MemoryMappedFileAccess.ReadWrite, new MemoryMappedFileSecurity(), HandleInheritability.Inheritable, true)) {
var viewStream = memoryMapped.CreateViewStream();
viewStream.Write(data, 0, data.Length);
}
}
}
}
and this is the tester:
public TimeSpan Run(int iterations, Writers.ITestWriter tester, String subfolder) {
Console.WriteLine(" -- Starting test {0} with {1} file writings",subfolder, iterations.ToString());
Stopwatch stopWatch = new Stopwatch();
stopWatch.Reset();
stopWatch.Start();
for (int i = 1; i <= iterations; i++) {
tester.Write(transaction, this.path + "\\" + subfolder + "\\" + "file_" + i.ToString() + ".txt", i.ToString());
}
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Console.WriteLine(" -- finish test {0} with {1} file writings. Time Elapsed: {2}", subfolder, iterations.ToString(), ts.TotalMilliseconds);
return ts;
}
the tester is called several times and there are several types of tester called for comparison.