public void MessageAfterExceptionTest() { Exception e = null; AutoResetEvent even = new AutoResetEvent(false); int y = 0; var a = new Arbitr<Message, NOPMessage>( (z) => { y = 1 / z.x; even.Set(); }, (z) => { e = z; even.Set(); }); a.Send(new Message() { x = 0 }); bool completed = even.WaitOne(500); Assert.IsTrue(completed); Assert.IsNotNull(e); Assert.AreEqual(y, 0); e = null; a.Send(new Message() { x = 1 }); completed = even.WaitOne(500); Assert.IsTrue(completed); Assert.IsNull(e); Assert.AreEqual(y, 1); }
public void MultiThreadBenhmark() { Exception e = null; Arbitr<Message, NOPMessage> a = null; a = new Arbitr<Message, NOPMessage>((x) => { MessageProc(x, a); }, (x) => { e = x; }); Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr1 = MsgSender; Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr2 = MsgSender; st.Reset(); st.Start(); var t1 = tr1.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null); var t2 = tr2.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null); tr1.EndInvoke(t1); tr2.EndInvoke(t2); Console.WriteLine("Осталось {0} сообщений", ActiveMsgCount); int prev = ActiveMsgCount; while (ActiveMsgCount > 0) { if (prev != ActiveMsgCount) { Console.WriteLine("Осталось {0} сообщений", ActiveMsgCount); prev = ActiveMsgCount; continue; } Thread.SpinWait(0); } st.Stop(); Console.WriteLine("Скорость обработки чистых сообщений {0} в мс", TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1)); Assert.AreEqual(SharedVar, 0); Assert.IsNull(e); Assert.IsNotNull(a); }
public void BasicSuspendResumeTest() { Exception e = null; AutoResetEvent even = new AutoResetEvent(false); int y = 0; var a = new Arbitr<Message, NOPMessage>( (z) => { y += z.x; even.Set(); }, (z) => { e = z; even.Set(); }); a.Send(new Message() { x = 1 }); bool completed = even.WaitOne(500); Assert.AreEqual(y, 1); Assert.IsTrue(completed); Assert.IsNull(e); a.Suspend(); y = 0; a.Send(new Message() { x = 1 }); completed = even.WaitOne(100); Assert.AreEqual(y, 0); Assert.IsFalse(completed); Assert.IsNull(e); a.Resume(); completed = even.WaitOne(500); Assert.AreEqual(y, 1); Assert.IsTrue(completed); Assert.IsNull(e); }
public void MessageProcExceptionTest() { Exception e = null; AutoResetEvent even = new AutoResetEvent(false); var a = new Arbitr<Message, NOPMessage>( (z) => { int q = 1 / z.x; even.Set(); }, (z) => { e = z; even.Set(); }); a.Send(new Message() { x = 0 }); bool completed = even.WaitOne(500); Assert.IsTrue(completed); Assert.IsNotNull(e); }
public void UserTaskTest() { Exception e = null; AutoResetEvent even = new AutoResetEvent(false); AutoResetEvent evenT = new AutoResetEvent(false); int y = 0; int t = 0; Arbitr<Message, NOPMessage> a = null; a = new Arbitr<Message, NOPMessage>( (z) => { y += z.x; if (a != null) a.AddUserTaskToLine(new Task(() => { t++; evenT.Set(); })); even.Set(); }, (z) => { e = z; even.Set(); }); a.Send(new Message() { x = 1 }); bool completed = even.WaitOne(500); Assert.AreEqual(y, 1); Assert.IsTrue(completed); Assert.IsNull(e); completed = evenT.WaitOne(500); Assert.AreEqual(y, 1); Assert.IsTrue(completed); Assert.IsNull(e); Assert.AreEqual(t, 1); a.Send(new Message() { x = -1 }); completed = even.WaitOne(500); Assert.AreEqual(y, 0); Assert.IsTrue(completed); Assert.IsNull(e); completed = evenT.WaitOne(500); Assert.AreEqual(y, 0); Assert.IsTrue(completed); Assert.IsNull(e); Assert.AreEqual(t, 2); }
public void MessageSendAndRecieveTest() { Exception e = null; AutoResetEvent even = new AutoResetEvent(false); int y = -1; var a = new Arbitr<Message, NOPMessage>( (z) => { y += z.x; even.Set(); }, (z) => { e = z; even.Set(); }); a.Send(new Message() { x = 1 }); bool completed = even.WaitOne(500); Assert.AreEqual(y, 0); Assert.IsTrue(completed); Assert.IsNull(e); }
public void MultiThreadSuspendTest() { Exception e = null; Arbitr<Message, NOPMessage> a = null; a = new Arbitr<Message, NOPMessage>((x) => { MessageProc(x, a); }, (x) => { e = x; }); int SuspendCount = 0; Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr1 = MsgSender; Action<Func<Message>, Arbitr<Message, NOPMessage>, int> tr2 = MsgSender; Action tr3 = () => { while (ActiveMsgCount > 0) { a.Resume(); SuspendCount++; Thread.Sleep(10); a.Suspend(); Thread.Sleep(10); } a.Resume(); }; st.Reset(); st.Start(); a.Suspend(); TSend(a, new Message()); var t3 = tr3.BeginInvoke(null, null); var t1 = tr1.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null); var t2 = tr2.BeginInvoke(() => { return new Message(); }, a, TotalMessageCount, null, null); tr1.EndInvoke(t1); tr2.EndInvoke(t2); tr3.EndInvoke(t3); while (ActiveMsgCount > 0) { Thread.SpinWait(0); } st.Stop(); Console.WriteLine("Произведенно {0} суспендов", SuspendCount); Console.WriteLine("Скорость обработки сообщений {0} в мс", TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1)); Assert.AreEqual(SharedVar, 0); Assert.IsNull(e); }
void TSend(Arbitr<Message, NOPMessage> a, Message msg) { Interlocked.Increment(ref ActiveMsgCount); a.Send(msg); }
private void MsgSender(Func<Message> GenFunc, Arbitr<Message, NOPMessage> arb, int count) { int i = 0; while (i < count) { if (ActiveMsgCount < MaxActiveMsgCount) { TSend(arb, GenFunc()); i++; continue; } Thread.SpinWait(0); } Console.WriteLine("Отправленно {0} сообщений",i); }
void MessageProc(Message msg, Arbitr<Message, NOPMessage> a) { if (msg is TaskMessage) a.AddUserTaskToLine(new Task(() => { ManipulateSharedVar(); Interlocked.Decrement(ref ActiveMsgCount); })); else { ManipulateSharedVar(); Interlocked.Decrement(ref ActiveMsgCount); } }
public void SerialOneThreadTest() { Exception e = null; Arbitr<Message, NOPMessage> a = null; a = new Arbitr<Message, NOPMessage>((x) => { MessageProc(x, a); }, (x) => { e = x; }); int i = 0; while (i < TotalMessageCount) { if (ActiveMsgCount < MaxActiveMsgCount) { if (rand.NextDouble() > 0.5) { TSend(a, new Message()); } else { TSend(a, new TaskMessage()); } i++; continue; } Thread.SpinWait(0); } while (ActiveMsgCount > 0) { Thread.SpinWait(0); } Assert.AreEqual(SharedVar, 0); Assert.IsNull(e); }
public void SerialOneThreadBenhmark() { Exception e = null; Arbitr<Message, NOPMessage> a = null; a = new Arbitr<Message, NOPMessage>((x) => { MessageProc(x, a); }, (x) => { e = x; }); int i = 0; //сначала померим скорость чистых сообщений st.Reset(); st.Start(); i = 0; while (i < TotalMessageCount) { if (ActiveMsgCount < MaxActiveMsgCount) { TSend(a, new Message()); i++; continue; } Thread.SpinWait(0); } while (ActiveMsgCount > 0) { Thread.SpinWait(0); } st.Stop(); Console.WriteLine("Скорость обработки чистых сообщений {0} в мс", TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1)); // теперь скорость чистых сообщений-тэсков st.Reset(); st.Start(); i = 0; while (i < TotalMessageCount) { if (ActiveMsgCount < MaxActiveMsgCount) { TSend(a, new TaskMessage()); i++; continue; } Thread.SpinWait(0); } while (ActiveMsgCount > 0) { Thread.SpinWait(0); } st.Stop(); Console.WriteLine("Скорость обработки чистых тэсков {0} в мс", TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1)); // а теперь смешанный режим 50\50 st.Reset(); st.Start(); i = 0; while (i < TotalMessageCount) { if (ActiveMsgCount < MaxActiveMsgCount) { if (i % 2 > 0) { TSend(a, new Message()); } else { TSend(a, new TaskMessage()); } i++; continue; } Thread.SpinWait(0); } while (ActiveMsgCount > 0) { Thread.SpinWait(0); } st.Stop(); Console.WriteLine("Скорость обработки сообщений в смешанном режиме 50х50 {0} в мс", TotalMessageCount / (st.ElapsedMilliseconds != 0 ? st.ElapsedMilliseconds : 1)); Assert.AreEqual(SharedVar, 0); Assert.IsNull(e); }