Ejemplo n.º 1
0
 /// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> and optionally releases the managed resources.</summary>
 /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (Interlocked.Exchange(ref _isDisposed, 1) == 0)
         {
             _currentStream.Dispose();
             if (_tempFile != null)
             {
                 var tmpName = _tempFile.Name;
                 _tempFile = null;
                 var tcs = new TaskCompletionSource <Nothing>();
                 CoreTaskHelper.RunAsyncFuncOnNewThread(async() =>
                 {
                     try
                     {
                         await CloseFile(tmpName);
                         tcs.SetResult(Nothing.Value);
                     }
                     catch (Exception ex)
                     {
                         tcs.SetException(ex);
                     }
                     return(Nothing.Value);
                 });
                 if (!tcs.Task.Wait(TimeSpan.FromSeconds(5)))
                 {
                     throw new TimeoutException();
                 }
             }
         }
     }
     base.Dispose(disposing);
 }
Ejemplo n.º 2
0
 /// <summary>When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.</summary>
 /// <param name="buffer">An array of bytes. This method copies <paramref name="count" /> bytes from <paramref name="buffer" /> to the current stream. </param>
 /// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the current stream. </param>
 /// <param name="count">The number of bytes to be written to the current stream. </param>
 /// <exception cref="T:System.ArgumentException">The sum of <paramref name="offset" /> and <paramref name="count" /> is greater than the buffer length.</exception>
 /// <exception cref="T:System.ArgumentNullException">
 /// <paramref name="buffer" />  is null.</exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 /// <paramref name="offset" /> or <paramref name="count" /> is negative.</exception>
 /// <exception cref="T:System.IO.IOException">An I/O error occured, such as the specified file cannot be found.</exception>
 /// <exception cref="T:System.NotSupportedException">The stream does not support writing.</exception>
 /// <exception cref="T:System.ObjectDisposedException">
 /// <see cref="M:System.IO.Stream.Write(System.Byte[],System.Int32,System.Int32)" /> was called after the stream was closed.</exception>
 /// <filterpriority>1</filterpriority>
 public override void Write(byte[] buffer, int offset, int count)
 {
     _currentStream.Write(buffer, offset, count);
     if (!CoreTaskHelper.RunAsyncFuncOnNewThread(CheckStreamSize).Wait(TimeSpan.FromSeconds(10)))
     {
         throw new TimeoutException();
     }
 }
Ejemplo n.º 3
0
 /// <summary>When overridden in a derived class, sets the length of the current stream.</summary>
 /// <param name="value">The desired length of the current stream in bytes. </param>
 /// <exception cref="T:System.IO.IOException">An I/O error occurs. </exception>
 /// <exception cref="T:System.NotSupportedException">The stream does not support both writing and seeking, such as if the stream is constructed from a pipe or console output. </exception>
 /// <exception cref="T:System.ObjectDisposedException">Methods were called after the stream was closed. </exception>
 /// <filterpriority>2</filterpriority>
 public override void SetLength(long value)
 {
     _currentStream.SetLength(value);
     if (!CoreTaskHelper.RunAsyncFuncOnNewThread(CheckStreamSize).Wait(TimeSpan.FromSeconds(10)))
     {
         throw new TimeoutException();
     }
 }
Ejemplo n.º 4
0
        public async Task BlobStoreUploadBenchmarkParallel()
        {
            long          size;
            Stopwatch     sw;
            List <BlobId> id;

            using (var str = new MemoryStream())
            {
                var wr = new BinaryWriter(str);
                for (int i = 0; i < 1024 * 1024 * 2; i++)
                {
                    wr.Write(i);
                }
                wr.Flush();
                byte[] dataBuf = str.ToArray();
                size         = str.Length;
                str.Position = 0;
                id           = new List <BlobId>();
                sw           = new Stopwatch();
                sw.Start();
                var l = new object();

                async Task <Nothing> DoSave()
                {
                    using (var str2 = new MemoryStream(dataBuf))
                    {
                        var id1 = await _store.SaveBlob(new InputBlob()
                        {
                            BlobStream = str2,
                            Category   = "test",
                            UniqueName = Guid.NewGuid().ToString()
                        }, CancellationToken.None);

                        lock (l)
                        {
                            id.Add(id1);
                        }
                    }
                    return(Nothing.Value);
                }

                var toWait = Enumerable.Range(0, 5).Select(i => (Task)CoreTaskHelper.RunAsyncFuncOnNewThread(DoSave)).ToArray();

                await Task.WhenAll(toWait);

                sw.Stop();
                var sizeMb = (float)size / (1024 * 1024);
                var sec    = sw.Elapsed.TotalSeconds / id.Count;
                sw = new Stopwatch();
                sw.Start();
                Logger.LogMessage($"Время сохранения файла размером {id.Count}x{sizeMb:F2} Мб: {sec} сек, {sizeMb / sec} Мб/Сек");
            }
        }
Ejemplo n.º 5
0
 private async Task <Nothing> CloseFile(string tmpName)
 {
     return(await CoreTaskHelper.RunAsyncFuncOnNewThread(async() =>
     {
         var tempFile2 = await ApplicationData.Current.TemporaryFolder.GetFileAsync(tmpName);
         if (_moveToFolder != null && !string.IsNullOrWhiteSpace(_moveToFileName))
         {
             try
             {
                 await tempFile2.MoveAsync(_moveToFolder, _moveToFileName, NameCollisionOption.ReplaceExisting);
             }
             catch
             {
                 await tempFile2.DeleteAsync();
                 throw;
             }
         }
         else
         {
             await tempFile2.DeleteAsync();
         }
         return Nothing.Value;
     }));
 }
Ejemplo n.º 6
0
        public async Task BoardReferenceParallelQuery()
        {
            var boards = await TestResources.LoadBoardReferencesFromResource();

            var parser = _provider.FindNetworkDtoParser <MobileBoardInfoCollection, IList <IBoardReference> >();
            var result = parser.Parse(boards);
            await _store.UpdateReferences(result, true);

            async Task <Nothing> Query(int cid, int qid)
            {
                Logger.LogMessage($"Query {cid}:{qid} started");
                var links = result.Take(40).Select(r => r.BoardLink);

                foreach (var l in links)
                {
                    var o = await _store.LoadReference(l);

                    Assert.IsNotNull(o, "Не получена информация о доске");
                    Assert.IsTrue(BoardLinkEqualityComparer.Instance.Equals(l, o.BoardLink), "Ссылка не соответствует исходной");
                }
                Logger.LogMessage($"Query {cid}:{qid} finished");
                return(Nothing.Value);
            }

            for (var i = 0; i < 5; i++)
            {
                Logger.LogMessage($"Cycle {i} started");
                await _collection.Suspend();

                Logger.LogMessage("DB suspended");
                await _collection.Resume();

                Logger.LogMessage("DB resumed");
                var cid    = i;
                var toWait = new Task[]
                {
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 1)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 2)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 3)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 4)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 5)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 6)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 7)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 8)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 9)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 10)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 11)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 12)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 13)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 14)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 15)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 16)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 17)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 18)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 19)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 20)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 21)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 22)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 23)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 24)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 25)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 26)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 27)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 28)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 29)),
                    CoreTaskHelper.RunAsyncFuncOnNewThread(() => Query(cid, 30)),
                };

                Logger.LogMessage($"Waiting for all queries (cycle {i})");
                await Task.WhenAll(toWait);

                Logger.LogMessage($"Waited for all queries (cycle {i})");
            }
        }