[tests] Add stress testing and reduce sgen normal testing time (#4653)
[mono.git] / mono / tests / sgen-new-threads-collect.cs
index 5deee6bc2c36115d2191904ece8388e67ca4fb9c..ed6e75c7d51f7f0dfe2031337268aae0d3b89da4 100644 (file)
@@ -5,47 +5,56 @@ using System.Threading;
 
 class Driver
 {
+       static TestTimeout timeout;
+
        public static void Main ()
        {
-               BlockingCollection<Thread> threads = new BlockingCollection<Thread> (new ConcurrentQueue<Thread> (), 128);
-
-               bool finished = false;
+               int gcCount = 0;
+               int joinCount = 0;
+               TestTimeout timeout = TestTimeout.Start(TimeSpan.FromSeconds(TestTimeout.IsStressTest ? 60 : 1));
 
                Thread gcThread = new Thread (() => {
-                       while (!finished) {
+                       while (timeout.HaveTimeLeft) {
                                GC.Collect ();
-                               Thread.Yield ();
-                       }
-               });
-
-               Thread joinThread = new Thread (() => {
-                       for (int i = 0; ; ++i) {
-                               Thread t = threads.Take ();
-                               if (t == null)
-                                       break;
-                               t.Join ();
-                               if ((i + 1) % (50) == 0)
-                                       Console.Write (".");
-                               if ((i + 1) % (50 * 50) == 0)
-                                       Console.WriteLine ();
+                               gcCount++;
+                               Thread.Sleep (1);
                        }
                });
 
                gcThread.Start ();
-               joinThread.Start ();
 
-               for (int i = 0; i < 10 * 1000; ++i) {
-                       Thread t = new Thread (() => { Thread.Yield (); });
-                       t.Start ();
-
-                       threads.Add (t);
-               }
-
-               threads.Add (null);
+               // Create threads then join them for 1 seconds (120 for stress tests) nonstop while GCs occur once per ms
+               while (timeout.HaveTimeLeft) {
+                       BlockingCollection<Thread> threads = new BlockingCollection<Thread> (new ConcurrentQueue<Thread> (), 128);
+
+                       Thread joinThread = new Thread (() => {
+                               for (int i = 0; ; ++i) {
+                                       Thread t = threads.Take ();
+                                       if (t == null)
+                                               break;
+                                       t.Join ();
+
+                                       // Uncomment this and run with MONO_LOG_LEVEL=info MONO_LOG_MASK=gc
+                                       // to see GC/join balance in real time
+                                       //Console.Write ("*");
+                               }
+                       });
+                       joinThread.Start ();
+                       
+                       const int makeThreads = 10*1000;
+                       for (int i = 0; i < makeThreads; ++i) {
+                               Thread t = new Thread (() => { Thread.Yield (); });
+                               t.Start ();
+
+                               threads.Add (t);
+                       }
 
-               joinThread.Join ();
+                       threads.Add (null);
+                       joinThread.Join ();
 
-               finished = true;
+                       joinCount += makeThreads;
+                       Console.WriteLine("Performed {0} GCs, created {1} threads. Finished? {2}", gcCount, joinCount, !timeout.HaveTimeLeft);
+               }
                gcThread.Join ();
        }
-}
\ No newline at end of file
+}