-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (87 loc) · 3.01 KB
/
Copy pathProgram.cs
File metadata and controls
103 lines (87 loc) · 3.01 KB
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
namespace TestPerformance
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length != 1)
{
usage();
Application.Exit();
return;
}
if (args[0].StartsWith("cpu"))
{
int threads = 2;
if (args[0].Split(':').Length > 1 )
{
if (!int.TryParse(args[0].Split(':')[1], out threads)) threads = 2;
}
Console.WriteLine("Starting CPU test with {0}: ", threads);
var watch = System.Diagnostics.Stopwatch.StartNew();
testPerformance(threads);
var elapsed = watch.ElapsedMilliseconds;
Console.WriteLine("Elapsed: " + elapsed);
} else if (args[0].StartsWith("form"))
{
int forms = 100;
Console.WriteLine("Starting form test with {0}: ", forms);
var watch = System.Diagnostics.Stopwatch.StartNew();
testWindowsForm(forms);
var elapsed = watch.ElapsedMilliseconds;
Console.WriteLine("Elapsed: " + elapsed);
}
}
private static void usage()
{
Console.WriteLine("Usage: ");
Console.WriteLine("");
Console.WriteLine("program.exe cpu");
Console.WriteLine("program.exe cpu:num_threads");
Console.WriteLine("program.exe form");
}
private static Random rand = new Random();
static List<Thread> threads = new List<Thread>();
static List<WaitHandle> waitHandles = new List<WaitHandle>();
private static int f = 0;
private static void testWindowsForm(int forms)
{
Application.Run(new Form1(forms));
}
private static void testPerformance(int threads_num)
{
int i = 0;
while (i < threads_num)
{
var handle = new EventWaitHandle(false, EventResetMode.ManualReset);
var thread = new Thread(new ParameterizedThreadStart(Run));
thread.Start(handle);
threads.Add(thread);
waitHandles.Add(handle);
i++;
}
WaitHandle.WaitAll(waitHandles.ToArray());
}
private static void Run(object handler)
{
long num = 0;
string r = "";
while (num < 100000)
{
var rnd = rand.Next(100, 1000);
r += rnd.ToString();
num++;
if (num % 10000 == 0)
{
r = "";
Console.WriteLine("Progress: " + num);
}
}
((EventWaitHandle) handler).Set();
}
}
}