-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCBench.cs
192 lines (180 loc) · 3.94 KB
/
CBench.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
using RapLog;
using NSUci;
namespace NSRapchess
{
class CBench
{
public bool start = false;
bool modeFen = true;
bool modeScore = true;
bool modeSort = false;
public ulong nodes = 0;
string modeGo = "go infinite";
int index = 0;
readonly Stopwatch timer = new Stopwatch();
List<string> list;
readonly CRapLog log = new CRapLog();
readonly List<string> fenList = new List<string>();
string GetTimeElapsed()
{
DateTime dt = new DateTime();
dt = dt.AddMilliseconds(timer.Elapsed.TotalMilliseconds);
return dt.ToString("HH:mm:ss.fff ");
}
void Sort()
{
fenList.Sort();
string last = String.Empty;
for (int n = fenList.Count - 1; n >= 0; n--)
{
string cur = fenList[n];
if (cur == last)
fenList.RemoveAt(n);
last = cur;
}
fenList.Insert(0, modeGo);
fenList.Insert(0, "sort");
File.WriteAllLines("bench.txt", fenList);
}
void BenchStart()
{
index = 0;
start = true;
nodes = 0;
fenList.Clear();
timer.Restart();
Console.WriteLine("bench start");
Next();
}
void History()
{
List<string> list = log.List();
for (int n = 2; n >= 0; n--)
if (n < list.Count)
Console.WriteLine(list[n]);
}
void BenchFinish()
{
if (!start)
return;
start = false;
timer.Stop();
double ms = timer.Elapsed.TotalMilliseconds;
double nps = ms > 0 ? (nodes * 1000.0) / ms : 0;
string msg = $"time {GetTimeElapsed()} nodes {nodes:N0} nps {nps:N0}";
log.Add(msg);
if (!modeScore)
History();
Console.WriteLine("finish");
Console.Beep();
if (modeSort)
Sort();
}
void Read()
{
if (index >= list.Count)
{
BenchFinish();
return;
}
CUci uci2 = new CUci();
while (index < list.Count)
{
string order = list[index++];
uci2.SetMsg(order);
switch (uci2.command)
{
case "//":
continue;
case "finish":
if (fenList.Count == 0)
CEvaluate.PrintEval();
BenchFinish();
return;
case "fen":
modeFen = true;
continue;
case "moves":
modeFen = false;
continue;
case "score":
modeScore = true;
continue;
case "go":
modeScore = false;
modeGo = order;
continue;
case "sort":
modeSort = true;
continue;
}
if (modeFen)
Program.engine.SetFen(order);
else
{
Program.engine.SetFen();
Program.engine.MakeMoves(order);
}
string fen = Program.engine.GetFen();
fenList.Add(fen);
if (modeScore)
CEvaluate.PrintEval();
else
{
CTranspositionTable.Clear();
Program.uci.SetMsg(modeGo);
Program.engine.UciGo();
break;
}
}
}
public void Next()
{
if (start)
Read();
}
void Bench()
{
Program.dataIn.post = false;
Console.WriteLine("Benchmark test");
CSearch engine = Program.engine;
MList mo = new MList();
ulong totalMs = 0;
ulong totalNodes = 0;
for (int n = 1; n < 11; n++)
{
engine.Start(mo, n, 0, 0);
ulong ms = (ulong)engine.stopwatch.ElapsedMilliseconds;
ulong nodes = engine.nodeCur;
totalMs += ms;
totalNodes += nodes;
string sMs = $"{ms:N0}";
string sNodes = $"{nodes:N0}";
Console.WriteLine($"{n}. {new string(' ', 20 - (n < 10 ? 1 : 2) - sMs.Length)} {sMs} {new string(' ', 20 - sNodes.Length)} {sNodes}");
}
ulong nps = totalMs > 0 ? (totalNodes * 1000) / totalMs : 0;
Console.WriteLine();
Console.WriteLine(new string('=', 32));
Console.WriteLine($"Time {totalMs:N0}");
Console.WriteLine($"Nodes {totalNodes:N0}");
Console.WriteLine($"Nps {nps:N0}");
Console.WriteLine(new string('=', 32));
}
public void Start()
{
string fn = "bench.txt";
if (File.Exists(fn))
{
string[] aList = File.ReadAllLines(fn);
list = new List<string>(aList);
BenchStart();
}
else
Bench();
}
}
}