コード例 #1
0
        /// <summary>
        /// Saves a stream, retry retryCount number of attempts and wait between the attempts if failed on sharing violation
        /// </summary>
        public static void SaveStream(string filename, Stream stream, int retryCount, TimeSpan wait)
        {
            var path = Path.GetDirectoryName(filename);

            if (!string.IsNullOrEmpty(path) && (!Directory.Exists(path)))
            {
                Directory.CreateDirectory(path);
            }

            var tries = 0;

            while (true)
            {
                try
                {
                    SaveStream(filename, stream);
                    return;
                }
                catch (IOException e)
                {
                    if (!LockingFileSystem.IsFileLocked(e))
                    {
                        throw;
                    }
                    if (++tries > retryCount)
                    {
                        throw;
                    }

                    Thread.Sleep(wait);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// queue processor
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        protected override QueuedExecution.ProcessResult ProcessQueue(IItem item)
        {
            FileOperationItem foi = (FileOperationItem)item;

            if (foi.Type == FileOperationItem.OperationType.Load)
            {
                if (_filenamelock.IsReaderLocked(foi.Filename))
                {
                    return(QueuedExecution.ProcessResult.FailRequeue);
                }

                _filenamelock.EnterReader(foi.Filename);
                foi.output = LockingFileSystem.LoadStream(foi.Filename);//, int.MaxValue, TimeSpan.FromMilliseconds(1));
                _filenamelock.ExitReader(foi.Filename);

                return(ProcessResult.Success);
            }
            else if (foi.Type == FileOperationItem.OperationType.Save)
            {
                _filenamelock.EnterWriter(foi.Filename);
                LockingFileSystem.SaveStream(foi.Filename, foi.input);//, int.MaxValue, TimeSpan.FromMilliseconds(1));
                _filenamelock.ExitWriter(foi.Filename);
                return(ProcessResult.Success);
            }
            throw new NotImplementedException();
        }
コード例 #3
0
        /// <summary>
        /// Reader Locked Load Stream
        /// </summary>
        public static Stream LoadStream(string filename)
        {
            Stream retval;

            _filenamelock.EnterReader(filename);
            retval = LockingFileSystem.LoadStream(filename);
            _filenamelock.ExitReader(filename);
            return(retval);
        }
コード例 #4
0
 /// <summary>
 /// Queue Processor
 /// </summary>
 private void ProcessQueue()
 {
     while (true)
     {
         FileOperationItem foi;
         if (_operations.TryDequeue(out foi))
         {
             if (foi.Type == FileOperationItem.OperationType.Load)
             {
                 if (_filenamelock.IsReaderLocked(foi.Filename))
                 {
                     _operations.Enqueue(foi);
                     continue;
                 }
                 _filenamelock.EnterReader(foi.Filename);
                 foi.stream = LockingFileSystem.LoadStream(foi.Filename);    //, int.MaxValue, TimeSpan.FromMilliseconds(1));
                 _filenamelock.ExitReader(foi.Filename);
                 foi.resetevent.Set();
             }
             else if (foi.Type == FileOperationItem.OperationType.Save)
             {
                 if (_filenamelock.IsWriterLocked(foi.Filename))
                 {
                     _operations.Enqueue(foi);
                     continue;
                 }
                 _filenamelock.EnterWriter(foi.Filename);
                 LockingFileSystem.SaveStream(foi.Filename, foi.stream);    //, int.MaxValue, TimeSpan.FromMilliseconds(1));
                 _filenamelock.ExitWriter(foi.Filename);
                 foi.resetevent.Set();
             }
         }
         else
         {
             Thread.Sleep(1);
         }
     }
 }
コード例 #5
0
 /// <summary>
 /// WriterLocked Save Stream
 /// </summary>
 public static void SaveStream(string filename, Stream stream)
 {
     _filenamelock.EnterWriter(filename);
     LockingFileSystem.SaveStream(filename, stream);
     _filenamelock.ExitWriter(filename);
 }
コード例 #6
0
ファイル: Program.cs プロジェクト: noisecrime/ForBlog
        public static List <payloadresult> PayloadLocking(int threadcount, int iterations, int payloadsize, int filescount, int retry, TimeSpan wait)
        {
            List <payloadresult> retval = new List <payloadresult>();

            byte[]       testobject = new byte[payloadsize];
            MemoryStream teststream = new MemoryStream(testobject);

            //generate short list of filenames
            List <string> filenames = GenerateFilenames(filescount);

            //write all filenames
            for (int i = 0; i < filenames.Count; i++)
            {
                LockingFileSystem.SaveStream(filenames[i], teststream);
            }

            //check read speed
            var sw = TimePayload(threadcount, iterations, () =>
            {
                var stream = LockingFileSystem.LoadStream(filenames[GetRandom(0, filenames.Count - 1)], retry, wait);
            });

            retval.Add(new payloadresult
            {
                Method  = "Locking",
                Action  = "R",
                Threads = threadcount,
                Time    = sw.ElapsedMilliseconds
            });

            Console.WriteLine("Threads: {0} Locking Load took {1}ms fps: {2}", threadcount, sw.ElapsedMilliseconds, ((threadcount * iterations) / ((sw.ElapsedMilliseconds) / 1000f)));

            //check write speed
            sw = TimePayload(threadcount, iterations, () =>
            {
                LockingFileSystem.SaveStream(filenames[GetRandom(0, filenames.Count - 1)], teststream, retry, wait);
            });

            retval.Add(new payloadresult
            {
                Method  = "Locking",
                Action  = "W",
                Threads = threadcount,
                Time    = sw.ElapsedMilliseconds
            });

            Console.WriteLine("Threads: {0} Locking Save took {1}ms fps: {2}", threadcount, sw.ElapsedMilliseconds, ((threadcount * iterations) / ((sw.ElapsedMilliseconds) / 1000f)));


            //check readwrite speed
            sw = TimePayload(threadcount, iterations / 2, () =>
            {
                var stream = LockingFileSystem.LoadStream(filenames[GetRandom(0, filenames.Count - 1)], retry, wait);
                LockingFileSystem.SaveStream(filenames[GetRandom(0, filenames.Count - 1)], teststream, retry, wait);
            });

            retval.Add(new payloadresult
            {
                Method  = "Locking",
                Action  = "R/W",
                Threads = threadcount,
                Time    = sw.ElapsedMilliseconds
            });

            Console.WriteLine("Threads: {0} Locking Save/Load took {1}ms fps: {2}", threadcount, sw.ElapsedMilliseconds, ((threadcount * iterations) / ((sw.ElapsedMilliseconds) / 1000f)));

            //cleanup
            DeleteFiles(filenames);

            return(retval);
        }