コード例 #1
0
        public void GivenWrongLengthWillFix()
        {
            string newFile    = DownloadTests.SafePath("testing.test");
            var    parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1);

            Assert.AreEqual(Constants.ONE_GIG_FILE_LENGTH, parameters.FileSize);
        }
コード例 #2
0
        public void IdIsAssignedWhenNotSupplied()
        {
            var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1);

            Assert.NotNull(parameters.Id);
            Assert.True(parameters.Id.Length > 10);
        }
コード例 #3
0
        public void DownloadSmallFilesWithNonOptimizedStream(int threadCount)
        {
            var             uri    = new Uri(Constants.TWENTY_MEG_FILE);
            var             path   = DownloadTests.SafePath("sites_vcf.gz");
            Action <string> logger = (message) => { };
            var             timer  = new Stopwatch();

            timer.Start();
            var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) });

            LargeFileDownloadParameters.EnsureCleanFile(path, true);
            using (var stream = new FileStream(path, FileMode.OpenOrCreate))
            {
                ILargeFileDownloadParameters parameters = new LargeFileDownloadWithStreamParameters(uri, stream,
                                                                                                    Constants.TWENTY_MEG_FILE_LENGTH,
                                                                                                    maxThreads:
                                                                                                    threadCount);
                Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager);
                task.Wait(TimeSpan.FromMinutes(5));
                timer.Stop();

                Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds);
                //try to open the file
                DownloadTests.ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM);
            }
        }
コード例 #4
0
        public void GivenWrongLengthWillNotFixIfTurnedOff()
        {
            string newFile    = DownloadTests.SafePath("testing.test");
            var    parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1, verifyLength: false);

            Assert.AreEqual(1, parameters.FileSize);
        }
コード例 #5
0
        public void SparseFileIsCreatedOnWindows()
        {
            string newFile = DownloadTests.SafePath("testing.test");
                try
                {
                    var timer = new Stopwatch();
                    timer.Start();
                    var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile,
                                                                     Constants.ONE_GIG_FILE_LENGTH);
                    Stream s = parameters.GetOutputStream();
                    //write to the end of the file
                    s.Seek((Constants.ONE_GIG_FILE_LENGTH) - 100, SeekOrigin.Begin);
                    var encoding = new System.Text.ASCIIEncoding();
                    Byte[] bytes = encoding.GetBytes("hello");
                    s.Write(bytes, 0, bytes.Length);
                    s.Close();

                    var info = new FileInfo(newFile);
                    timer.Stop();
                    //we should've pre-allocated file length
                    Assert.AreEqual(info.Length, Constants.ONE_GIG_FILE_LENGTH );
                    Assert.True(timer.ElapsedMilliseconds < 1500);  //should happen in less than a second since it's sparse

                }
                finally
                {
                   File.Delete(newFile);
                }
        }
コード例 #6
0
        public void MultipleParallelChunkedDownload(int threadCount, int parallelFactor)
        {
            Action <string> download = (prefix) => {
                var             uri    = new Uri(Constants.TWENTY_MEG_FILE);
                var             path   = SafePath(prefix + "sites_vcf.gz");
                Action <string> logger = (message) => Trace.WriteLine(message);
                var             timer  = new Stopwatch();
                timer.Start();
                ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(
                    uri, path, 29996532, null, maxThreads: threadCount);
                Task task = parameters.DownloadAsync(logger: logger);
                task.Wait(TimeSpan.FromMinutes(5));
                timer.Stop();
                Debug.WriteLine("Took {0} threads {1} ms", threadCount,
                                timer.ElapsedMilliseconds);
                //try to open the file
                ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM);
            };
            var tasks = new List <Task>();

            for (int i = 0; i < parallelFactor; i++)
            {
                int  i1 = i;
                Task t  = Task.Factory.StartNew(() => download(i1.ToString()));
                tasks.Add(t);
            }
            Task.WaitAll(tasks.ToArray());
        }
コード例 #7
0
        public void SparseFileIsCreatedOnWindows()
        {
            string newFile = DownloadTests.SafePath("testing.test");

            try
            {
                var timer = new Stopwatch();
                timer.Start();
                var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile,
                                                                 Constants.ONE_GIG_FILE_LENGTH);
                Stream s = parameters.GetOutputStream();
                //write to the end of the file
                s.Seek((Constants.ONE_GIG_FILE_LENGTH) - 100, SeekOrigin.Begin);
                var    encoding = new System.Text.ASCIIEncoding();
                Byte[] bytes    = encoding.GetBytes("hello");
                s.Write(bytes, 0, bytes.Length);
                s.Close();

                var info = new FileInfo(newFile);
                timer.Stop();
                //we should've pre-allocated file length
                Assert.AreEqual(info.Length, Constants.ONE_GIG_FILE_LENGTH);
                Assert.True(timer.ElapsedMilliseconds < 1500);      //should happen in less than a second since it's sparse
            }
            finally
            {
                File.Delete(newFile);
            }
        }
コード例 #8
0
        public DownloadFileCommand(Uri uri, string targetFileName, string fileId = null, CancellationToken token = new CancellationToken(), bool enableLogging = true)
        {
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, targetFileName, 0,
                                                                                      fileId ?? targetFileName);

            _parameters    = parameters;
            _token         = token;
            _enableLogging = enableLogging;
        }
コード例 #9
0
        public DownloadFileCommand(BaseSpaceClient client, FileCompact file, string targetFileName,
                                   IClientSettings settings, CancellationToken token = new CancellationToken(), bool enableLogging = true, int threadCount = DEFAULT_THREADS)
        {
            DateTime expiration;
            string   url = GetFileContentUrl(client, file.Id, out expiration);
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(new Uri(url), targetFileName, maxThreads: threadCount, maxChunkSize: (int?)settings.FileDownloadMultipartSizeThreshold, id: file.Id);

            _parameters    = parameters;
            _token         = token;
            _enableLogging = enableLogging;
        }
コード例 #10
0
        public DownloadFileCommand(BaseSpaceClient client, FileCompact file, string targetFileName, IClientSettings settings, int threadCount, int maxChunkSize, CancellationToken token = new CancellationToken(), bool enableLogging = true)
        {
            DateTime expiration;
            string   url = GetFileContentUrl(client, file.Id, out expiration);

            _fileName = string.Format("[{0}],{1}", file.Id, file.Name);
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(new Uri(url), targetFileName, maxThreads: threadCount, maxChunkSize: maxChunkSize, id: file.Id);

            _parameters    = parameters;
            _token         = token;
            _enableLogging = enableLogging;
        }
コード例 #11
0
        public void ParallelChunkedDownload(int threadCount)
        {
            var             uri    = new Uri(Constants.TWENTY_MEG_FILE);
            var             path   = SafePath("sites_vcf.gz");
            Action <string> logger = (message) => Trace.WriteLine(message);
            var             timer  = new Stopwatch();

            timer.Start();
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 29996532, null, maxThreads: threadCount);
            Task task = parameters.DownloadAsync(logger: logger);

            task.Wait(TimeSpan.FromMinutes(5));
            timer.Stop();
            Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds);
            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM);
        }
コード例 #12
0
        public void DownloadZeroByteFile()
        {
            var             uri    = new Uri(Constants.ZERO_BYTE_FILE);
            string          path   = SafePath("sites_vcf.gz");
            Action <string> logger = (message) => { };
            var             timer  = new Stopwatch();

            timer.Start();
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, null, null);
            Task task = parameters.DownloadAsync(logger: logger);

            task.Wait(TimeSpan.FromMinutes(15));
            timer.Stop();
            Debug.WriteLine("Took {0} threads {1} ms", parameters.MaxThreads, timer.ElapsedMilliseconds);
            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.ZERO_BYTE_CHECKSUM);
        }
コード例 #13
0
        public void DownloadSmallerInSingleChunk()
        {
            var             uri    = new Uri(Constants.FIVE_MEG_FILE);
            var             path   = SafePath("sites_vcf.gz");
            Action <string> logger = (message) => { };
            var             timer  = new Stopwatch();

            timer.Start();

            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, maxChunkSize: Constants.FIVE_MEG_LENGTH, maxThreads: 1);
            Task task = parameters.DownloadAsync(logger: logger, progress: new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(s => logger("progress")));

            task.Wait(TimeSpan.FromMinutes(5));
            timer.Stop();
            Debug.WriteLine("Took {0} threads {1} ms", 1, timer.ElapsedMilliseconds);
            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM);
        }
コード例 #14
0
        public void ParallelChunkedOneGig(int threadCount)
        {
            string path = "";

            try
            {
                var uri = new Uri(Constants.ONE_GIG_FILE_S_SL);
                path = SafePath("sites_vcf.gz");
                Action <string> logger = (message) => { };
                var             timer  = new Stopwatch();
                //a list to hold measured transfer rates
                var transferRateList = new List <double>();
                var asyncProgress    = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(
                    obj =>
                {
                    transferRateList.Add(obj.DownloadBitRate);
                    logger("progress");
                });

                var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) });
                ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1297662912, null, maxThreads: threadCount);

                timer.Start();

                Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager, progress: asyncProgress);

                task.Wait(TimeSpan.FromMinutes(25));
                timer.Stop();

                var averageTransferRate = transferRateList.Average();
                Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", threadCount, timer.ElapsedMilliseconds, averageTransferRate);
                //try to open the file
                ValidateGZip(path, parameters.FileSize, Constants.ONE_GIG_CHECKSUM);

                //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 30% (accuracy of atleast 70%)
                Assert.AreEqual(parameters.FileSize, averageTransferRate * timer.Elapsed.TotalSeconds, 0.30 * parameters.FileSize);
            }
            finally
            {
                File.Delete(path);
            }
        }
コード例 #15
0
        public void CancellationTokenWillCancel()
        {
            var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false);
            var writeQueue = new ConcurrentQueue <ChunkedFilePart>();

            byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world");
            var    mockClient     = new Mock <ISimpleHttpGetByRangeClient>();

            mockClient.Setup(x => x.Get(It.IsAny <Uri>(), It.IsAny <long>(), It.IsAny <long>()))
            .Returns(() =>
            {
                Thread.Sleep(200);
                return(new SimpleHttpResponse(206, sampleResponse, null));
            });

            int timesAskedForSlow = -1;
            var readStack         = new ConcurrentStack <int>();

            //add all of the chunks to the stack
            readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray());
            Func <int, bool> shouldSlw = i =>
            {
                timesAskedForSlow++;
                return(false);
            };
            var tokenSource   = new CancellationTokenSource();
            var failureToken  = new FailureToken();
            var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) });
            var task          = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token);

            task.Start();
            Thread.Sleep(500);
            tokenSource.Cancel();
            task.Wait(TimeSpan.FromMinutes(2));
            int current;

            readStack.TryPop(out current);
            Assert.True(current != 10); //we shouldn't get to 10 before the cancel works
        }
コード例 #16
0
        public void DownloadSmallerFile()
        {
            //8 threads
            //MaxChunkSize = 1048576
            //FileSize = 5242880

            var             uri    = new Uri(Constants.FIVE_MEG_FILE);
            var             path   = SafePath("sites_vcf.gz");
            Action <string> logger = (message) => { };

            var transferRateList = new List <double>();
            var asyncProgress    = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(
                obj =>
            {
                transferRateList.Add(obj.DownloadBitRate);
                Debug.WriteLine(obj.DownloadBitRate);
                logger("progress");
            });

            var timer = new Stopwatch();

            timer.Start();
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, null, maxThreads: 8);
            Task task = parameters.DownloadAsync(logger: logger, progress: asyncProgress);

            task.Wait(TimeSpan.FromMinutes(1));
            timer.Stop();
            var averageTransferRate = transferRateList.Average();

            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM);

            if (averageTransferRate > 0)// sometimes tx rate is 0, possibly due to caching
            {
                Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", 8, timer.ElapsedMilliseconds, averageTransferRate);
                //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 40% (accuracy of atleast 60%) because it is a small file and txrate is calculated once every two seconds.
                Assert.GreaterOrEqual(averageTransferRate * timer.Elapsed.TotalSeconds, 0.40 * parameters.FileSize);
            }
        }
コード例 #17
0
        public void DownloadSmallerInSingleChunk()
        {
            var uri = new Uri(Constants.FIVE_MEG_FILE);
            var path = SafePath("sites_vcf.gz");
            Action<string> logger = (message) => { };
            var timer = new Stopwatch();
            timer.Start();

            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, maxChunkSize: Constants.FIVE_MEG_LENGTH, maxThreads: 1);
            Task task = parameters.DownloadAsync(logger: logger, progress: new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>( s=> logger("progress")) );
            task.Wait(TimeSpan.FromMinutes(5));
            timer.Stop();
            Debug.WriteLine("Took {0} threads {1} ms", 1, timer.ElapsedMilliseconds);
            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM);
        }
コード例 #18
0
 public void DownloadZeroByteFile()
 {
     var uri = new Uri(Constants.ZERO_BYTE_FILE);
       string path = SafePath("sites_vcf.gz");
       Action<string> logger = (message) => { };
       var timer = new Stopwatch();
       timer.Start();
       ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path,null, null);
       Task task = parameters.DownloadAsync(logger: logger);
       task.Wait(TimeSpan.FromMinutes(15));
       timer.Stop();
       Debug.WriteLine("Took {0} threads {1} ms", parameters.MaxThreads, timer.ElapsedMilliseconds);
       //try to open the file
       ValidateGZip(path, parameters.FileSize, Constants.ZERO_BYTE_CHECKSUM);
 }
コード例 #19
0
        private static bool ProcessArgs(string[] args)
        {
            var options = new DownloadOptions();

            try
            {
                var optionSet = CreateOptions(options);
                optionSet.Parse(args);

                if (options.ShowHelp || (options.Uri == null))
                {
                    optionSet.WriteOptionDescriptions(Console.Out);
                }
                else if (options.HasException)
                {
                    Console.WriteLine(options.Exception.Message);
                }
                else
                {
                    //go get file size
                    GetSizeAndSource(options);
                    LargeFileDownloadParameters parameters = null;
                    if (options.Uri != null && options.FileSize.HasValue && options.FileSize.Value > 0)
                    {
                        parameters = new LargeFileDownloadParameters(options.Uri, options.OutputFile,
                                                                     options.FileSize.Value,
                                                                     options.MaxThreads, options.MaxChunkSize);
                    }


                    if (parameters != null)
                    {
                        Action <string> logger = (message) => Debug.WriteLine(message);
                        var             token  = new CancellationToken();
                        var             watch  = new Stopwatch();
                        watch.Start();
                        IAsyncProgress <LargeFileDownloadProgressChangedEventArgs> progress = new AsyncProgress <LargeFileDownloadProgressChangedEventArgs>(Handler);
                        var task = parameters.DownloadAsync(token, progress, logger);
                        task.Wait();
                        watch.Stop();
                        ClearCurrentConsoleLine();

                        Console.WriteLine("done in {0}ms ({1}m {2}s {3}ms", watch.ElapsedMilliseconds, watch.Elapsed.Minutes, watch.Elapsed.Seconds, watch.Elapsed.Milliseconds);
                        if (options.IsInteractive)
                        {
                            return(true);
                        }
                        Environment.Exit(0);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (options.IsInteractive)
                {
                    return(true);
                }
                Environment.Exit(-1);
            }
            return(!options.ShouldExit);
        }
コード例 #20
0
        public void MultipleParallelChunkedDownload(int threadCount, int parallelFactor)
        {
            Action<string> download = (prefix) => {
                                          var uri = new Uri(Constants.TWENTY_MEG_FILE);
                                          var path = SafePath(prefix +"sites_vcf.gz");
                                          Action<string> logger = (message) => Trace.WriteLine(message);
                                          var timer = new Stopwatch();
                                          timer.Start();
                                          ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(
                                              uri, path, 29996532,null, maxThreads: threadCount);
                                          Task task = parameters.DownloadAsync(logger: logger);
                                          task.Wait(TimeSpan.FromMinutes(5));
                                          timer.Stop();
                                          Debug.WriteLine("Took {0} threads {1} ms", threadCount,
                                                          timer.ElapsedMilliseconds);
                                          //try to open the file
                                          ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM);

              };
              var tasks = new List<Task>();
              for (int i = 0; i < parallelFactor; i++)
              {
                  int i1 = i;
                  Task t = Task.Factory.StartNew(() => download(i1.ToString()));
                  tasks.Add(t);
              }
              Task.WaitAll(tasks.ToArray());
        }
コード例 #21
0
 public void MaxChunkSizeCalculation(int fileSize, int chunkSize, int expected)
 {
     var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", fileSize, verifyLength: false, maxChunkSize: chunkSize);
     Assert.AreEqual(expected, parameters.MaxChunkSize);
 }
コード例 #22
0
        public void ParallelChunkedOneGig(int threadCount)
        {
            string path = "";
              try
              {
                  var uri = new Uri(Constants.ONE_GIG_FILE_S_SL);
                  path = SafePath("sites_vcf.gz");
                  Action<string> logger = (message) => { };
                  var timer = new Stopwatch();
                  //a list to hold measured transfer rates
                  var transferRateList = new List<double>();
                  var asyncProgress = new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>(
                      obj =>
                      {
                          transferRateList.Add(obj.DownloadBitRate);
                          logger("progress");
                      });

                  var manager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, (uint)threadCount), new BufferQueueSetting(LargeFileDownloadParameters.DEFAULT_MAX_CHUNK_SIZE) });
                  ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1297662912, null, maxThreads: threadCount);

                  timer.Start();

                  Task task = parameters.DownloadAsync(logger: logger, bufferManager: manager, progress: asyncProgress);

                  task.Wait(TimeSpan.FromMinutes(25));
                  timer.Stop();

                  var averageTransferRate = transferRateList.Average();
                  Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", threadCount, timer.ElapsedMilliseconds, averageTransferRate);
                  //try to open the file
                  ValidateGZip(path, parameters.FileSize, Constants.ONE_GIG_CHECKSUM);

                  //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 30% (accuracy of atleast 70%)
                  Assert.AreEqual(parameters.FileSize, averageTransferRate * timer.Elapsed.TotalSeconds, 0.30 * parameters.FileSize);
              }
              finally
              {
                  File.Delete(path);
              }
        }
コード例 #23
0
 public void IdIsAssignedWhenNotSupplied()
 {
     var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1);
     Assert.NotNull( parameters.Id);
     Assert.True(parameters.Id.Length > 10);
 }
コード例 #24
0
 public void GivenWrongLengthWillNotFixIfTurnedOff()
 {
     string newFile = DownloadTests.SafePath("testing.test");
     var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile, 1,verifyLength: false);
     Assert.AreEqual(1, parameters.FileSize);
 }
コード例 #25
0
 public void GivenWrongLengthWillFix()
 {
     string newFile = DownloadTests.SafePath("testing.test");
     var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), newFile,1);
     Assert.AreEqual(Constants.ONE_GIG_FILE_LENGTH, parameters.FileSize);
 }
コード例 #26
0
 public void ParallelChunkedDownload(int threadCount)
 {
     var uri = new Uri(Constants.TWENTY_MEG_FILE);
     var path = SafePath("sites_vcf.gz");
     Action<string> logger = ( message) => Trace.WriteLine(message);
     var timer = new Stopwatch();
       timer.Start();
     ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 29996532, null, maxThreads: threadCount);
     Task task = parameters.DownloadAsync(logger: logger);
     task.Wait(TimeSpan.FromMinutes(5));
     timer.Stop();
     Debug.WriteLine("Took {0} threads {1} ms", threadCount, timer.ElapsedMilliseconds);
     //try to open the file
     ValidateGZip(path, parameters.FileSize, Constants.TWENTY_CHECKSUM);
 }
コード例 #27
0
        public void DownloadSmallerFile()
        {
            //8 threads
            //MaxChunkSize = 1048576
            //FileSize = 5242880

            var uri = new Uri(Constants.FIVE_MEG_FILE);
            var path = SafePath("sites_vcf.gz");
            Action<string> logger = (message) => { };

            var transferRateList = new List<double>();
            var asyncProgress = new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>(
                obj =>
                {
                    transferRateList.Add(obj.DownloadBitRate);
                    Debug.WriteLine(obj.DownloadBitRate);
                    logger("progress");
                });

            var timer = new Stopwatch();
            timer.Start();
            ILargeFileDownloadParameters parameters = new LargeFileDownloadParameters(uri, path, 1048576, null, maxThreads: 8);
            Task task = parameters.DownloadAsync(logger: logger, progress:asyncProgress);
            task.Wait(TimeSpan.FromMinutes(1));
            timer.Stop();
            var averageTransferRate = transferRateList.Average();

            //try to open the file
            ValidateGZip(path, parameters.FileSize, Constants.FIVE_MEG_CHECKSUM);

            if (averageTransferRate > 0)// sometimes tx rate is 0, possibly due to caching
            {
                Debug.WriteLine("Took {0} threads {1} ms with average transfer rate of {2}", 8, timer.ElapsedMilliseconds, averageTransferRate);
                //We expect that the approximate bytes transferred (calculated using average transfer rate) is with maximim error of 40% (accuracy of atleast 60%) because it is a small file and txrate is calculated once every two seconds.
                Assert.GreaterOrEqual(averageTransferRate * timer.Elapsed.TotalSeconds, 0.40 * parameters.FileSize);
            }
        }
コード例 #28
0
 public void IdIsAssignedWhenSupplied()
 {
     var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1, id:"test");
     Assert.True(parameters.Id == "test");
 }
コード例 #29
0
        public void MaxChunkSizeCalculation(int fileSize, int chunkSize, int expected)
        {
            var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", fileSize, verifyLength: false, maxChunkSize: chunkSize);

            Assert.AreEqual(expected, parameters.MaxChunkSize);
        }
コード例 #30
0
ファイル: Program.cs プロジェクト: basespace/TerminalVelocity
        private static bool ProcessArgs(string[] args)
        {
            var options = new DownloadOptions();
            try
            {

                var optionSet = CreateOptions(options);
                optionSet.Parse(args);

                if (options.ShowHelp || (options.Uri == null))
                {
                    optionSet.WriteOptionDescriptions(Console.Out);
                }
                else if (options.HasException)
                {
                    Console.WriteLine(options.Exception.Message);
                }
                else
                {
                    //go get file size
                    GetSizeAndSource(options);
                    LargeFileDownloadParameters parameters = null;
                    if (options.Uri != null && options.FileSize.HasValue && options.FileSize.Value > 0)
                    {
                        parameters = new LargeFileDownloadParameters(options.Uri, options.OutputFile,
                                                                     options.FileSize.Value,
                                                                     options.MaxThreads, options.MaxChunkSize);
                    }

                    if (parameters != null)
                    {
                        Action<string> logger = (message) => Debug.WriteLine(message);
                        var token = new CancellationToken();
                        var watch = new Stopwatch();
                        watch.Start();
                        IAsyncProgress<LargeFileDownloadProgressChangedEventArgs> progress =new AsyncProgress<LargeFileDownloadProgressChangedEventArgs>(Handler);
                        var task = parameters.DownloadAsync(token, progress, logger);
                        task.Wait();
                        watch.Stop();
                       ClearCurrentConsoleLine();

                        Console.WriteLine("done in {0}ms ({1}m {2}s {3}ms", watch.ElapsedMilliseconds, watch.Elapsed.Minutes, watch.Elapsed.Seconds, watch.Elapsed.Milliseconds);
                        if (options.IsInteractive)
                        {
                            return true;
                        }
                        Environment.Exit(0);

                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (options.IsInteractive)
                {
                    return true;
                }
                Environment.Exit(-1);
            }
            return !options.ShouldExit;
        }
コード例 #31
0
        public void IdIsAssignedWhenSupplied()
        {
            var parameters = new LargeFileDownloadParameters(new Uri("http://blah.com"), "", 20, verifyLength: false, maxChunkSize: 1, id: "test");

            Assert.True(parameters.Id == "test");
        }
コード例 #32
0
        public void CancellationTokenWillCancel()
        {
            var parameters = new LargeFileDownloadParameters(new Uri(Constants.ONE_GIG_FILE_S_SL), "blah", 1000, verifyLength: false);
            var writeQueue = new ConcurrentQueue<ChunkedFilePart>();

            byte[] sampleResponse = Encoding.UTF8.GetBytes("hello world");
            var mockClient = new Mock<ISimpleHttpGetByRangeClient>();

            mockClient.Setup(x => x.Get(It.IsAny<Uri>(), It.IsAny<long>(), It.IsAny<long>()))
                      .Returns(() =>
            {
                Thread.Sleep(200);
                return new SimpleHttpResponse(206, sampleResponse, null);
            });

            int timesAskedForSlow = -1;
            var readStack = new ConcurrentStack<int>();
            //add all of the chunks to the stack
            readStack.PushRange(Enumerable.Range(0, 5).Reverse().ToArray());
            Func<int, bool> shouldSlw = i =>
            {
                timesAskedForSlow++;
                return false;
            };
            var tokenSource = new CancellationTokenSource();
            var failureToken = new FailureToken();
            var bufferManager = new BufferManager(new[] { new BufferQueueSetting(SimpleHttpGetByRangeClient.BUFFER_SIZE, 1), new BufferQueueSetting((uint)parameters.MaxChunkSize) });
            var task = new Downloader(bufferManager, parameters, writeQueue, readStack, shouldSlw, Downloader.ExpectedDownloadTimeInSeconds(parameters.MaxChunkSize), clientFactory: (x) => mockClient.Object, failureToken: failureToken, cancellation: tokenSource.Token);
            task.Start();
            Thread.Sleep(500);
            tokenSource.Cancel();
            task.Wait(TimeSpan.FromMinutes(2));
            int current;
            readStack.TryPop(out current);
            Assert.True(current != 10); //we shouldn't get to 10 before the cancel works
        }