public int RunServer(IList <string> args)
 {
     using (TestSignals signals = new TestSignals(args[0]))
         using (StartServer(int.Parse(args[1])))
         {
             Console.WriteLine("{0} started.", GetType().Name);
             signals.Ready();
             signals.ExitWait();
         }
     Console.WriteLine("{0} stopped.", GetType().Name);
     return(0);
 }
 public int RunServer(IList<string> args)
 {
     using (TestSignals signals = new TestSignals(args[0]))
     using (StartServer(int.Parse(args[1])))
     {
         Console.WriteLine("{0} started.", GetType().Name);
         signals.Ready();
         signals.ExitWait();
     }
     Console.WriteLine("{0} stopped.", GetType().Name);
     return 0;
 }
        public int RunClient(IList <string> args)
        {
            using (TestSignals signals = new TestSignals(args[0]))
                using (signals.AcquireSemaphore())
                {
                    int[] counter  = new int[2];
                    int   runCount = int.Parse(args[3]);
                    for (int run = 0; run <= runCount; run++)
                    {
                        bool     bStop         = false;
                        int      repeatedCount = int.Parse(args[4]);
                        bool     bReport       = run > 0;
                        int      nThreads      = int.Parse(args[2]);
                        Thread[] threads       = new Thread[nThreads];
                        counter[0] = 0;

                        for (int i = 0; i < nThreads; i++)
                        {
                            string[] info = args.ToArray();
                            info[0]    = GetType().Name;
                            info[2]    = i.ToString();
                            info[3]    = run.ToString();
                            threads[i] = new Thread(
                                () =>
                            {
                                Interlocked.Increment(ref counter[0]);
                                if (signals.BeginWait())
                                {
                                    Stopwatch timer = Stopwatch.StartNew();
                                    int success     = 0;
                                    RunClient(
                                        repeatedCount >= 0 ? repeatedCount : int.MaxValue,
                                        ref bStop,
                                        int.Parse(args[5]), out success);
                                    timer.Stop();

                                    if (repeatedCount != success)
                                    {
                                        Interlocked.Increment(ref counter[1]);
                                    }

                                    if (bReport)
                                    {
                                        if (repeatedCount < 0)
                                        {
                                            info[4] = (-repeatedCount).ToString();
                                        }
                                        lock (typeof(Console))
                                            Console.WriteLine("{0} \t{1} \t{2:n0} \t{3:n2}", String.Join(" \t", info),
                                                              success, timer.ElapsedMilliseconds,
                                                              success / (timer.ElapsedMilliseconds / 1000.0));
                                    }
                                }
                            }
                                );
                            threads[i].Start();
                        }

                        while (counter[0] != nThreads)
                        {
                            Thread.Sleep(1);
                        }

                        signals.Ready(int.Parse(args[1]));

                        bStop = signals.BeginWait() == false;
                        if (!bStop && repeatedCount < 0)
                        {
                            Thread.Sleep(-repeatedCount);
                            bStop = true;
                        }

                        foreach (Thread t in threads)
                        {
                            t.Join();
                        }

                        //Collect and cool-down
                        GC.Collect(2, GCCollectionMode.Forced);
                        GC.GetTotalMemory(true);
                        GC.WaitForPendingFinalizers();

                        long stop = DateTime.UtcNow.AddMilliseconds(100).Ticks;
                        while (DateTime.UtcNow.Ticks < stop)
                        {
                            Thread.Sleep(1);
                        }
                    }
                    return(counter[1]);
                }
        }
        public int RunClient(IList<string> args)
        {
            using (TestSignals signals = new TestSignals(args[0]))
            using (signals.AcquireSemaphore())
            {
                int[] counter = new int[2];
                int runCount = int.Parse(args[3]);
                for (int run = 0; run <= runCount; run++)
                {
                    bool bStop = false;
                    int repeatedCount = int.Parse(args[4]);
                    bool bReport = run > 0;
                    int nThreads = int.Parse(args[2]);
                    Thread[] threads = new Thread[nThreads];
                    counter[0] = 0;

                    for (int i = 0; i < nThreads; i++)
                    {
                        string[] info = args.ToArray();
                        info[0] = GetType().Name;
                        info[2] = i.ToString();
                        info[3] = run.ToString();
                        threads[i] = new Thread(
                            () =>
                                {
                                    Interlocked.Increment(ref counter[0]);
                                    if (signals.BeginWait())
                                    {
                                        Stopwatch timer = Stopwatch.StartNew();
                                        int success = 0;
                                        RunClient(
                                            repeatedCount >= 0 ? repeatedCount : int.MaxValue,
                                            ref bStop, 
                                            int.Parse(args[5]), out success);
                                        timer.Stop();

                                        if (repeatedCount != success)
                                            Interlocked.Increment(ref counter[1]);

                                        if (bReport)
                                        {
                                            if (repeatedCount < 0)
                                                info[4] = (-repeatedCount).ToString();
                                            lock (typeof (Console))
                                                Console.WriteLine("{0} \t{1} \t{2:n0} \t{3:n2}", String.Join(" \t", info),
                                                                  success, timer.ElapsedMilliseconds,
                                                                  success / (timer.ElapsedMilliseconds / 1000.0));
                                        }
                                    }
                                }
                            );
                        threads[i].Start();
                    }

                    while (counter[0] != nThreads)
                        Thread.Sleep(1);

                    signals.Ready(int.Parse(args[1]));

                    bStop = signals.BeginWait() == false;
                    if (!bStop && repeatedCount < 0)
                    {
                        Thread.Sleep(-repeatedCount);
                        bStop = true;
                    }

                    foreach (Thread t in threads)
                        t.Join();

                    //Collect and cool-down
                    GC.Collect(2, GCCollectionMode.Forced);
                    GC.GetTotalMemory(true);
                    GC.WaitForPendingFinalizers();

                    long stop = DateTime.UtcNow.AddMilliseconds(100).Ticks;
                    while (DateTime.UtcNow.Ticks < stop)
                        Thread.Sleep(1);
                }
                return counter[1];
            }
        }
Example #5
0
 private static void Stop(TestSignals signal)
 {
     signal.Exit();
     foreach (Process p in Running)
         p.WaitForExit();
     Running.Clear();
 }
Example #6
0
        private static int RunAll(ICollection<string> args)
        {
            if(args.Count == 0)
                args = new List<string>(Tests.Keys);

            int failures = 0;
            foreach(string key in args)
            {
                try
                {
                    Tests[key]();
                    int clientRuns = 3;
                    foreach (int numclients in new[] { 5 })
                    foreach (int numthreads in new[] { 3 })
                    foreach (int repeated in new[] { -50000 })
                    foreach (int recordSize in new[] { 1000 })
                    {
                        TestSignals signal = new TestSignals(Guid.NewGuid().ToString("N"));
                        try
                        {
                            using (Semaphore clients = new Semaphore(numclients, numclients, signal.Name + ".clients"))
                            {
                                Start("server", key, signal, recordSize);
                                signal.ReadyWait();

                                for (int ixclient = 0; ixclient < numclients; ixclient++)
                                {
                                    Start("client", key, signal, ixclient, numthreads, clientRuns, repeated, recordSize);
                                    signal.ReadyWait(ixclient);
                                }
                                signal.Begin();

                                int lockCount = 0;
                                try
                                {
                                    for (int ixclient = 0; ixclient < numclients; ixclient++)
                                    {
                                        clients.WaitOne();
                                        lockCount++;
                                    }
                                }
                                finally
                                {
                                    for (int ixclient = 0; ixclient < lockCount; ixclient++)
                                        clients.Release();
                                }
                            }
                        }
                        finally
                        {
                            Stop(signal);
                            signal.Dispose();
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("test '{0}' failed: {1}", key, e.Message);
                    failures++;
                }
            }
            return failures;
        }