public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); var ms = new HighwaySettings(1024, 1, 1024); // Allocates 100 fragments and continuously resets one, while // new fragments are allocated. void allocAndManualReset(IMemoryHighway hw) { var F = new List <MemoryFragment>(); for (int i = 0; i < 100; i++) { F.Add(hw.AllocFragment(4)); } } if (opt.Contains("mh")) { using (var hw = new HeapHighway(ms, 1024)) allocAndManualReset(hw); } if (opt.Contains("nh")) { using (var hw = new MarshalHighway(ms, 1024)) allocAndManualReset(hw); } if (opt.Contains("mmf")) { using (var hw = new MappedHighway(ms, 1024)) allocAndManualReset(hw); } if (!Passed.HasValue) { Passed = true; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); var ms = new HighwaySettings(1024); // so that no refs are held ms.RegisterForProcessExitCleanup = false; // The unmanaged resource var MMF_FileID = string.Empty; // Must be in a separate non-async function so that no refs are holding it. void fin() { // Create without 'using' and forget to dispose var mmh = new MappedHighway(ms, 1024); MMF_FileID = mmh[0].FileID; mmh.Alloc(100); } fin(); GC.Collect(2); Thread.Sleep(5000); if (!File.Exists(MMF_FileID)) { Print.AsSuccess($"MappedHighway: the underlying file {MMF_FileID} was cleaned up by the finalizer."); Passed = true; } else { Print.AsTestFailure("MappedHighway: the finalizer was not triggered or the file deletion failed."); Passed = false; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); var allocArgs = new AllocTestArgs() { Count = 20, Size = 180000, InParallel = 10, RandomizeAllocDelay = true, RandomizeFragDisposal = true, RandomizeLength = true, AllocDelayMS = 0, AllocTries = 1, FragmentDisposeAfterMS = 60 }; if (args.ContainsKey("-count")) { allocArgs.Count = int.Parse(args["-count"][0]); } if (args.ContainsKey("-size")) { allocArgs.Size = int.Parse(args["-size"][0]); } if (allocArgs.Count * allocArgs.Size > 12_000_000) { Passed = false; FailureMessage = "The default highway capacity is not enough if all fragments live forever."; return; } Print.Trace(allocArgs.FullTrace(), ConsoleColor.Cyan, ConsoleColor.Black, null); if (opt.Contains("mh")) { using (var hw = new HeapHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The HeapHighway has active fragments after the AllocAndWait()"; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (opt.Contains("nh")) { using (var hw = new MarshalHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The MarshalHighway has active fragments after the AllocAndWait()"; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (opt.Contains("mmf")) { using (var hw = new MappedHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The MappedHighway has active fragments after the AllocAndWait()"; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (!Passed.HasValue) { Passed = true; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); var allocArgs = new AllocTestArgs() { Count = 5, Size = 5000_000, InParallel = 4, RandomizeAllocDelay = true, RandomizeFragDisposal = false, RandomizeLength = false, AllocDelayMS = 0, FragmentDisposeAfterMS = 4000 // long enough }; if (args.ContainsKey("-count")) { allocArgs.Count = int.Parse(args["-count"][0]); } if (args.ContainsKey("-size")) { allocArgs.Count = int.Parse(args["-size"][0]); } var defHwCap = HighwaySettings.DefaultLaneCapacity * 1.5; if (allocArgs.Count * allocArgs.Size < defHwCap) { Passed = false; FailureMessage = "The default highway capacity can handle all fragments. Should test out of the capacity bounds."; return; } Print.Trace(allocArgs.FullTrace(), ConsoleColor.Cyan, ConsoleColor.Black, null); if (opt.Contains("mh")) { using (var hw = new HeapHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The HeapHighway has active fragments after the AllocAndWait()"; return; } if (hw.GetLanesCount() < 3) { Passed = false; FailureMessage = "The HeapHighway has less than 3 lanes. "; return; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (opt.Contains("nh")) { using (var hw = new MarshalHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The MarshalHighway has active fragments after the AllocAndWait()"; return; } if (hw.GetLanesCount() < 3) { Passed = false; FailureMessage = "The HeapHighway has less than 3 lanes. "; return; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (opt.Contains("mmf")) { using (var hw = new MappedHighway()) { hw.AllocAndWait(allocArgs); if (hw.GetTotalActiveFragments() > 0) { Passed = false; FailureMessage = "The MappedHighway has active fragments after the AllocAndWait()"; return; } if (hw.GetLanesCount() < 3) { Passed = false; FailureMessage = "The HeapHighway has less than 3 lanes. "; return; } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } if (!Passed.HasValue) { Passed = true; } IsComplete = true; } }
async Task Process(AllocType at, Socket client, Action <int> onmessage, bool stop = false) { Print.AsInfo(at.ToString() + Environment.NewLine); try { Print.AsInfo("New client" + Environment.NewLine); IMemoryHighway hw = null; var lanes = new int[] { 1025, 2048 }; switch (at) { case AllocType.Heap: hw = new HeapHighway(lanes); break; case AllocType.MMF: hw = new MappedHighway(lanes); break; case AllocType.Marshal: hw = new MarshalHighway(lanes); break; default: throw new ArgumentNullException(); } using (hw) using (var ns = new NetworkStream(client)) { var header = new byte[4]; var spoon = new byte[16000]; var total = 0; var read = 0; var frameLen = 0; while (!stop) { total = 0; read = await ns.ReadAsync(header, 0, 4).ConfigureAwait(false); Print.AsInfo("Received header bytes: {0}.{1}.{2}.{3}", header[0], header[1], header[2], header[3]); // The other side is gone. // As long as the sender is not disposed/closed the ReadAsync will wait if (read < 1) { Print.AsError("The client is gone."); break; } frameLen = BitConverter.ToInt32(header, 0); if (frameLen < 1) { Print.AsError("Bad header, thread {0}", Thread.CurrentThread.ManagedThreadId); break; } using (var frag = hw.AllocFragment(frameLen)) { Print.AsInfo("Frame length:{0}", frameLen); // The sip length guards against jumping into the next frame var sip = 0; while (total < frameLen && !stop) { sip = frameLen - total; if (sip > spoon.Length) { sip = spoon.Length; } // the read amount could be smaller than the sip read = await ns.ReadAsync(spoon, 0, sip).ConfigureAwait(false); frag.Write(spoon, total, read); total += read; Print.AsInnerInfo("read {0} on thread {1}", read, Thread.CurrentThread.ManagedThreadId); if (total >= frameLen) { onmessage?.Invoke(total); break; } } } } } } catch (MemoryLaneException mex) { Print.AsError(mex.Message); Print.AsError(mex.ErrorCode.ToString()); Print.AsError(mex.StackTrace); } catch (Exception ex) { Print.AsError(ex.Message); } }