summaryrefslogtreecommitdiff
path: root/tests/src/GC/Performance/Tests/ServerSpin.cs
blob: a1e3c147c7caccfe792261ef90c15fdc4c9c57ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using System.Threading;

public class Node
{
    public Node Left, Right;
    private GCHandle gch;
    public static Random rand = new Random(191919);

    public Node()
    {

        GCHandleType type = GCHandleType.Normal;

        if (rand.Next() % 3 == 0)
        {
            type = GCHandleType.Pinned;
        }

        //int size = (int)(Math.Pow(2,(rand.Next()%18)));
        int size = 0;

        gch = GCHandle.Alloc(new byte[size], type);
    }


    ~Node()
    {

        if (gch.IsAllocated)
        {
            gch.Free();
        }
    }


    public static Node CreateTree(int depth)
    {
        if (depth<=0)
        {
            return null;
        }

        Node root = null;
        try
        {
            root = new Node();
        }
        catch (OutOfMemoryException)
        {
            Console.WriteLine("OOM");
            return null;
        }
        root.Left = CreateTree(depth-1);
        root.Right = CreateTree(depth-1);
        return root;
    }


    public static Node FindRandomNode(Node root, int depth)
    {
        int iters = rand.Next() % (depth-1);
        Node cur = root;

        for (int i=0; i<iters; i++)
        {

            if (cur.Left==null || cur.Right== null)
            {
                break;
            }

            if (rand.Next() % 2 == 0)
            {
                cur = cur.Left;
            }
            else
            {
                cur = cur.Right;
            }

        }

        return cur;

    }

}


public class ServerSpin
{

    int depth;
    int numIterations;
    int numThreads;
    int maxObjectSize;

    public ServerSpin(int depth, int numIterations, int numThreads, int maxObjectSize)
    {
        this.depth = depth;
        this.numIterations = numIterations;
        this.numThreads = numThreads;
        this.maxObjectSize = maxObjectSize;
    }

    public ServerSpin(int depth, int numIterations, int numThreads)
        : this(depth, numIterations, numThreads, 1024*1024)
    {
    }


    public static void Usage()
    {
        Console.WriteLine("USAGE: ServerSpin.exe <tree depth> <num iterations> <num threads> [max object size]");
        Console.WriteLine("\tdefaults to: 7, 100, 2");
    }


    public static void Main(string[] args)
    {

        // defaults
        int depth = 7;
        int numIterations = 100;
        int numThreads = 2;

        if (args.Length==3)
        {
            Int32.TryParse(args[0], out depth);
            if (depth<=1)
            {
                Usage();
                return;
            }

            Int32.TryParse(args[1], out numIterations);
            if (numIterations <= 0)
            {
                Usage();
                return;
            }

            Int32.TryParse(args[2], out numThreads);
            if (numThreads < 1)
            {
                Usage();
                return;
            }
        }
        else if (args.Length!=0)
        {
            Usage();
            return;
        }


        ServerSpin test = new ServerSpin(depth, numIterations, numThreads);
        test.RunTest();

    }


    public void RunTest()
    {
        Thread[] threads = new Thread[numThreads];
        for (int i=0; i<numThreads; i++)
        {
            threads[i] = new Thread(new ThreadStart(DoWork));
            threads[i].Start();
            Console.WriteLine("Start thread {0}", i);
        }

        for (int i=0; i<numThreads; i++)
        {
            threads[i].Join();
            Console.WriteLine("Done thread {0}", i);
        }

    }


    private void DoWork()
    {
        // create initial tree
        Node root = Node.CreateTree(depth);

        if (root==null)
        {
            return;
        }

        for (int i=0; i<numIterations; i++)
        {
            // replace subtree
            Node.FindRandomNode(root, depth).Left = Node.CreateTree(depth-1);

            // delete subtree
            Node.FindRandomNode(root, depth).Left = null;

            // replace subtree
            Node.FindRandomNode(root, depth).Right = Node.CreateTree(depth-2);

            // delete subtree
            Node.FindRandomNode(root, depth).Right = null;

            // replace subtree
            Node.FindRandomNode(root, depth).Left = Node.CreateTree(depth-3);
        }
    }
}