A Stream that supports reading and writing from a fixed size memory buffer. This provides the ability to test writing and reading from very large streams without using any disk storeage
Inheritance: Stream
Ejemplo n.º 1
0
        public void BigStream()
        {
            window_ = new WindowedStream(0x3ffff);
            outStream_ = new GZipOutputStream(window_);
            inStream_ = new GZipInputStream(window_);

            long target = 0x10000000;
            readTarget_ = writeTarget_ = target;

            Thread reader = new Thread(Reader);
            reader.Name = "Reader";
            reader.Start();

            Thread writer = new Thread(Writer);
            writer.Name = "Writer";

            DateTime startTime = DateTime.Now;
            writer.Start();

            writer.Join();
            reader.Join();

            DateTime endTime = DateTime.Now;

            TimeSpan span = endTime - startTime;
            Console.WriteLine("Time {0}  processes {1} KB/Sec", span, (target / 1024) / span.TotalSeconds);
        }
Ejemplo n.º 2
0
        //Performance tests hamper TDD efforts
        //[Test]
        //[Category("Zip")]
        //[Category("Long Running")]
        public void SingleLargeEntry()
        {
            window_ = new WindowedStream(0x10000);
            outStream_ = new ZipOutputStream(window_);
            inStream_ = new ZipInputStream(window_);

            long target = 0x10000000;
            readTarget_ = writeTarget_ = target;

            Thread reader = new Thread(Reader);
            reader.Name = "Reader";

            Thread writer = new Thread(Writer);
            writer.Name = "Writer";

            DateTime startTime = DateTime.Now;
            reader.Start();
            writer.Start();

            writer.Join();
            reader.Join();

            DateTime endTime = DateTime.Now;
            TimeSpan span = endTime - startTime;
            Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (target / 1024.0) / span.TotalSeconds);
        }
Ejemplo n.º 3
0
		public void Performance()
		{
			window_ = new WindowedStream(0x150000);

			outStream_ = new BZip2OutputStream(window_, 1);

			const long Target = 0x10000000;
			readTarget_ = writeTarget_ = Target;

			Thread reader = new Thread(Reader);
			reader.Name = "Reader";

			Thread writer = new Thread(Writer);
			writer.Name = "Writer";

			DateTime startTime = DateTime.Now;
			writer.Start();

            inStream_ = new BZip2InputStream(window_);

            reader.Start();

			Assert.IsTrue(writer.Join(TimeSpan.FromMinutes(5.0D)));
			Assert.IsTrue(reader.Join(TimeSpan.FromMinutes(5.0D)));

			DateTime endTime = DateTime.Now;
			TimeSpan span = endTime - startTime;
			Console.WriteLine("Time {0} throughput {1} KB/Sec", span, (Target / 1024) / span.TotalSeconds);
			
		}
Ejemplo n.º 4
0
        public static void TestReadWrite(int size, Func <Stream, Stream> input, Func <Stream, Stream> output, Action <Stream> outputClose = null)
        {
            var cts    = new CancellationTokenSource(TimeSpan.FromMinutes(1));
            var window = new WindowedStream(size, cts.Token);

            var readerState = new PerfWorkerState()
            {
                bytesLeft  = size,
                token      = cts.Token,
                baseStream = window,
                streamCtr  = input,
            };

            var writerState = new PerfWorkerState()
            {
                bytesLeft  = size,
                token      = cts.Token,
                baseStream = window,
                streamCtr  = output,
                streamCls  = outputClose
            };



            var reader = new Thread(stateObject =>
            {
                var state = (PerfWorkerState)stateObject;
                try
                {
                    // Run output stream constructor
                    state.InitStream();

                    // Main read loop
                    ReadTargetBytes(ref state);

                    if (!state.token.IsCancellationRequested)
                    {
                        Assert.IsFalse(state.baseStream.CanRead, "Base Stream should be closed");

                        // This shouldnt read any data but should read the footer
                        var buffer    = new byte[1];
                        int readBytes = state.stream.Read(buffer, 0, 1);
                        Assert.LessOrEqual(readBytes, 0, "Stream should be empty");
                    }

                    // Dispose of the input stream
                    state.stream.Close();
                }
                catch (Exception x)
                {
                    state.exception = x;
                }
            });

            Thread writer = new Thread(stateObject =>
            {
                var state = (PerfWorkerState)stateObject;
                try
                {
                    // Run input stream constructor
                    state.InitStream();

                    // Main write loop
                    WriteTargetBytes(ref state);

                    state.DeinitStream();

                    // Dispose of the input stream
                    state.stream.Close();
                }
                catch (Exception x)
                {
                    state.exception = x;
                }
            });

            var sw = Stopwatch.StartNew();

            writer.Name = "Writer";
            writer.Start(writerState);

            // Give the writer thread a couple of seconds to write headers
            Thread.Sleep(TimeSpan.FromSeconds(3));

            reader.Name = "Reader";
            reader.Start(readerState);

            bool      writerJoined = false, readerJoined = false;
            const int timeout = 100;

            while (!writerJoined && !readerJoined)
            {
                writerJoined = writer.Join(timeout);
                if (writerJoined && writerState.exception != null)
                {
                    ExceptionDispatchInfo.Capture(writerState.exception).Throw();
                }

                readerJoined = reader.Join(timeout);
                if (readerJoined && readerState.exception != null)
                {
                    ExceptionDispatchInfo.Capture(readerState.exception).Throw();
                }

                if (cts.IsCancellationRequested)
                {
                    break;
                }
            }

            //Assert.IsTrue(writerJoined, "Timed out waiting for reader thread to join");
            //Assert.IsTrue(readerJoined, "Timed out waiting for writer thread to join");


            Assert.IsFalse(cts.IsCancellationRequested, "Threads were cancelled before completing execution");

            var elapsed  = sw.Elapsed;
            var testSize = size / ByteToMB;

            Console.WriteLine($"Time {elapsed:mm\\:ss\\.fff} throughput {testSize / elapsed.TotalSeconds:f2} MB/s (using test size: {testSize:f2} MB)");
        }