Пример #1
0
        //-------------------------------------------//

        protected override bool OpenConnection()
        {
            switch (FileMode)
            {
            case FileMode.OpenOrCreate:
            case FileMode.CreateNew:
            case FileMode.Create:
                try {
                    _fileStream = new FileStream(Path, FileMode, FileAccess, FileShare, BufferSize);
                } catch (Exception ex) {
                    Log.Warning(this + " FileStream could not be created. " + ex.Messages());
                    State = ConnectionState.Broken;
                }
                break;

            case FileMode.Open:
            case FileMode.Append:
            case FileMode.Truncate:
                try {
                    // ensure the path exists
                    ManagerResources.CreateFilePath(Path);
                    // get the file stream
                    _fileStream = new FileStream(Path, FileMode, FileAccess, FileShare, BufferSize);
                } catch (Exception ex) {
                    Log.Warning(this + " FileStream could not be created. " + ex.Messages());
                    State = ConnectionState.Broken;
                }
                break;
            }
            return(!State.Is(ConnectionState.Broken));
        }
Пример #2
0
        /// <summary>
        /// Get average statistics of the drive of the specified directory path. Resulting value units
        /// are bytes and per second.
        /// Note : This method writes a Megabyte to the Stream and then clears it.
        /// </summary>
        public static void GetStreamStats(string directory, out uint seeks, out uint writes, out uint reads)
        {
            // create a test file
            string filePath = ManagerResources.CreateFilePath(directory, ".drivetest");

            // get a stream to the created test file
            Teple <LockShared, ByteBuffer> resource;

            ManagerConnections.Get <ByteBuffer, ConnectionLocal>(filePath, out resource);

            ByteBuffer stream = resource.ArgB;

            // get a seek, read and write speed for the Author
            Timekeeper time = new Timekeeper();

            // write a Megabyte
            time.Start();
            stream.Write(Generic.Series <byte>((int)Global.Megabyte, 50));
            stream.Stream.Flush();
            time.Stop();

            // derive the number of bytes written per second
            writes = (uint)(Global.Megabyte / (time.Milliseconds / 1000));

            // perform a number of seeks
            time.Start();
            for (int i = 1000; i >= 0; --i)
            {
                stream.Position = 0;
                stream.Write((byte)0);
                stream.Stream.Flush();
                stream.Position = (long)Global.Megabyte - 1L;
                stream.Write((byte)0);
                stream.Stream.Flush();
            }
            time.Stop();

            // derive the number of seeks per second
            seeks           = (uint)(1000 / (time.Milliseconds / 1000));
            stream.Position = 0;

            // read the Megabyte
            time.Start();
            stream.ReadBytes((int)Global.Megabyte);
            stream.Stream.Flush();
            time.Stop();

            // derive the number of bytes read per second
            reads = (uint)(Global.Megabyte / (time.Milliseconds / 1000));

            // release the connection
            resource.ArgA.Release();

            // remove the files
            File.Delete(filePath);
        }