Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / domain-stress.cs
1 using System;
2 using System.Threading;
3 using System.Collections;
4
5 class T {
6         /* each thread will create n domains */
7         static int threads = 5;
8         static int domains = 100;
9         static int allocs = 1000;
10         static int loops = 1;
11         static int errors = 0;
12
13         public static void worker () {
14                 Console.WriteLine ("Domain start " + AppDomain.CurrentDomain.FriendlyName + " " + Thread.CurrentThread.GetHashCode ());
15                 ArrayList list = new ArrayList ();
16                 for (int i = 0; i < allocs; ++i) {
17                         list.Add (new object ());
18                         list.Add (new ArrayList ());
19                         list.Add (new String ('x', 34));
20                         int[] a = new int [5];
21                         list.Add (new WeakReference (a));
22                         if ((i % 1024) == 0) {
23                                 list.RemoveRange (0, list.Count / 2);
24                         }
25                 }
26                 Console.WriteLine ("Domain end " + AppDomain.CurrentDomain.FriendlyName + " " + Thread.CurrentThread.GetHashCode ());
27         }
28
29         static void thread_start () {
30                 Console.WriteLine ("Thread start " + Thread.CurrentThread.GetHashCode ());
31                 for (int i = 0; i < domains; ++i) {
32                         AppDomain appDomain = AppDomain.CreateDomain("Test-" + i);
33                         appDomain.DoCallBack (new CrossAppDomainDelegate (worker));
34                         try {
35                                 AppDomain.Unload (appDomain);
36                         } catch {
37                                 Interlocked.Increment (ref errors);
38                                 Console.WriteLine ("Error unloading " + "Test-" + i);
39                         }
40                 }
41                 Console.WriteLine ("Thread end " + Thread.CurrentThread.GetHashCode ());
42         }
43         static int Main (string[] args) {
44                 if (args.Length > 0)
45                         threads = int.Parse (args [0]);
46                 if (args.Length > 1)
47                         domains = int.Parse (args [1]);
48                 if (args.Length > 2)
49                         allocs = int.Parse (args [2]);
50                 if (args.Length > 3)
51                         loops = int.Parse (args [3]);
52                 for (int j = 0; j < loops; ++j) {
53                         Thread[] ta = new Thread [threads];
54                         for (int i = 0; i < threads; ++i) {
55                                 Thread t = new Thread (new ThreadStart (thread_start));
56                                 ta [i] = t;
57                                 t.Start ();
58                         }
59                         for (int i = 0; i < threads; ++i) {
60                                 ta [i].Join ();
61                         }
62                 }
63                 //thread_start ();
64                 //Console.ReadLine ();
65                 return 0;
66         }
67 }
68