Beispiel #1
0
 public LogItem(LogItem obj) : this(obj.ThreadNo, obj.Iteration, obj.Operation, obj.NodeNo, obj.Begin, obj.End)
 {
 }
Beispiel #2
0
 public void Append(LogItem logItem)
 {
     _log.Add(logItem);
 }
Beispiel #3
0
        static void Run(object param)
        {
            TaskParam taskParam;

            if ((taskParam = param as TaskParam) == null)
            {
                return;
            }

            Console.WriteLine("{0}\t{1} started...", DateTime.Now.ToString(LogItem.FormatString), taskParam.i);

            var rnd = new Random(Thread.CurrentThread.ManagedThreadId);

            for (var i = 0; i < 10; ++i)
            {
                int
                    nodeNo,
                    millisecondsTimeout = taskParam.mSec * rnd.Next(10);

                Console.WriteLine("{0}\t{1} i={2} (will sleep {3}ms)", DateTime.Now.ToString(LogItem.FormatString), taskParam.i, i, millisecondsTimeout);

                if ((nodeNo = rnd.Next(taskParam.nodesMax)) < taskParam.nodesMax)
                {
                    string msg;

                    DateTime
                        dateTimeBeforeLock = DateTime.Now,
                        dateTimeAfterLock;

                    LogItem
                        logItemBeforeLock,
                        logItemAfterLock;

                    bool isNewNode;

                    logItemBeforeLock = new LogItem(taskParam.i, i, $"trying to lock for check node {nodeNo}", nodeNo, dateTimeBeforeLock);
                    Console.WriteLine("{0}\t{1} i={2} trying to lock for check node {3}", dateTimeBeforeLock.ToString(LogItem.FormatString), taskParam.i, i, nodeNo);
                    lock (taskParam.victim)
                    {
                        dateTimeAfterLock = DateTime.Now;
                        logItemAfterLock  = new LogItem(taskParam.i, i, $"has locked for check node {nodeNo} successfully", nodeNo, dateTimeBeforeLock, dateTimeAfterLock);
                        Console.WriteLine("{0}\t{1} i={2} has locked for check node {3} successfully (b4lock {4} wait {5}ms)", dateTimeAfterLock.ToString(LogItem.FormatString), taskParam.i, i, nodeNo, dateTimeBeforeLock.ToString(LogItem.FormatString), (dateTimeAfterLock - dateTimeBeforeLock).TotalMilliseconds);

                        LogItem logItemNodeHasBeenAdded = null;

                        if (isNewNode = !taskParam.victim.ContainsKey(nodeNo))
                        {
                            taskParam.victim.Add(nodeNo, new Log());
                            logItemNodeHasBeenAdded = new LogItem(taskParam.i, i, $"node {nodeNo} has been added", nodeNo, DateTime.Now);
                            Console.WriteLine("{0}\t{1} i={2} node {3} has been added", DateTime.Now.ToString(LogItem.FormatString), taskParam.i, i, nodeNo);
                        }

                        taskParam.victim[nodeNo].Append(logItemBeforeLock);
                        taskParam.victim[nodeNo].Append(logItemAfterLock);

                        if (logItemNodeHasBeenAdded != null)
                        {
                            taskParam.victim[nodeNo].Append(logItemNodeHasBeenAdded);
                        }

                        dateTimeAfterLock = DateTime.Now;
                        taskParam.victim[nodeNo].Append(new LogItem(taskParam.i, i, $"has released for check node {nodeNo} successfully", nodeNo, dateTimeBeforeLock, dateTimeAfterLock));
                        Console.WriteLine("{0}\t{1} i={2} has released for check node {3} successfully (b4lock {4} wait {5}ms)", DateTime.Now.ToString(LogItem.FormatString), taskParam.i, i, nodeNo, dateTimeBeforeLock.ToString(LogItem.FormatString), (dateTimeAfterLock - dateTimeBeforeLock).TotalMilliseconds);
                    }

                    dateTimeBeforeLock = DateTime.Now;
                    logItemBeforeLock  = new LogItem(taskParam.i, i, $"trying to lock node {nodeNo}", nodeNo, dateTimeBeforeLock);
                    Console.WriteLine("{0}\t{1} i={2} trying to lock node {3}", dateTimeBeforeLock.ToString(LogItem.FormatString), taskParam.i, i, nodeNo);

                    lock (taskParam.victim[nodeNo])
                    {
                        dateTimeAfterLock = DateTime.Now;
                        var millisecondsTimeoutForLock = taskParam.mSec * rnd.Next(10);
                        taskParam.victim[nodeNo].Append(logItemBeforeLock);
                        taskParam.victim[nodeNo].Append(new LogItem(taskParam.i, i, $"has locked node {nodeNo} successfully (will sleep {millisecondsTimeoutForLock}ms)", nodeNo, dateTimeBeforeLock, dateTimeAfterLock));
                        Console.WriteLine("{0}\t{1} i={2} has locked node {3} successfully (will sleep {6}ms) (b4lock {4} wait {5}ms)", dateTimeAfterLock.ToString(LogItem.FormatString), taskParam.i, i, nodeNo, dateTimeBeforeLock.ToString(LogItem.FormatString), (dateTimeAfterLock - dateTimeBeforeLock).TotalMilliseconds, millisecondsTimeoutForLock);

                        if (isNewNode)
                        {
                            Foo1(taskParam.victim, nodeNo, taskParam.mSec, new LogItem(taskParam.i, i, string.Empty, nodeNo, DateTime.Now));
                            Foo2(taskParam.victim, nodeNo, taskParam.mSec, new LogItem(taskParam.i, i, string.Empty, nodeNo, DateTime.Now));
                        }
                        else
                        {
                            Foo2(taskParam.victim, nodeNo, taskParam.mSec, new LogItem(taskParam.i, i, string.Empty, nodeNo, DateTime.Now));
                        }

                        Thread.Sleep(millisecondsTimeoutForLock);

                        var tmpDateTime = DateTime.Now;
                        taskParam.victim[nodeNo].Append(new LogItem(taskParam.i, i, $"has released node {nodeNo} successfully", nodeNo, dateTimeAfterLock, tmpDateTime));
                        Console.WriteLine("{0}\t{1} i={2} has released node {3} successfully (b4lock {4} wait {5}ms)", tmpDateTime.ToString(LogItem.FormatString), taskParam.i, i, nodeNo, dateTimeAfterLock.ToString(LogItem.FormatString), (tmpDateTime - dateTimeAfterLock).TotalMilliseconds);
                    }
                }

                Thread.Sleep(millisecondsTimeout);
            }

            Console.WriteLine("{0}\t{1} finished", DateTime.Now.ToString(LogItem.FormatString), taskParam.i);
        }