-
-} // namespace
-
-/// benchmark() runs a simple benchmark by letting Stockfish analyze a set
-/// of positions for a given limit each. There are five parameters: the
-/// transposition table size, the number of search threads that should
-/// be used, the limit value spent for each position (optional, default is
-/// depth 13), an optional file name where to look for positions in FEN
-/// format (defaults are the positions defined above) and the type of the
-/// limit value: depth (default), time in secs or number of nodes.
-
-void benchmark(const Position& current, istream& is) {
-
- string token;
- Search::LimitsType limits;
- vector<string> fens;
-
- // Assign default values to missing arguments
- string ttSize = (is >> token) ? token : "16";
- string threads = (is >> token) ? token : "1";
- string limit = (is >> token) ? token : "13";
- string fenFile = (is >> token) ? token : "default";
- string limitType = (is >> token) ? token : "depth";
-
- Options["Hash"] = ttSize;
- Options["Threads"] = threads;
- TT.clear();
-
- if (limitType == "time")
- limits.movetime = 1000 * atoi(limit.c_str()); // movetime is in ms
-
- else if (limitType == "nodes")
- limits.nodes = atoi(limit.c_str());
-
- else if (limitType == "mate")
- limits.mate = atoi(limit.c_str());
-
- else
- limits.depth = atoi(limit.c_str());
-
- if (fenFile == "default")
- fens.assign(Defaults, Defaults + 30);
-
- else if (fenFile == "current")
- fens.push_back(current.fen());
-
- else
- {
- string fen;
- ifstream file(fenFile.c_str());
-
- if (!file.is_open())
- {
- cerr << "Unable to open file " << fenFile << endl;
- return;
- }
-
- while (getline(file, fen))
- if (!fen.empty())
- fens.push_back(fen);
-
- file.close();
- }
-
- uint64_t nodes = 0;
- Search::StateStackPtr st;
- Time::point elapsed = Time::now();
-
- for (size_t i = 0; i < fens.size(); ++i)
- {
- Position pos(fens[i], Options["UCI_Chess960"], Threads.main());
-
- cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
-
- if (limitType == "perft")
- nodes += Search::perft<true>(pos, limits.depth * ONE_PLY);
-
- else
- {
- Threads.start_thinking(pos, limits, st);
- Threads.wait_for_think_finished();
- nodes += Search::RootPos.nodes_searched();
- }
- }
-
- elapsed = std::max(Time::now() - elapsed, Time::point(1)); // Avoid a 'divide by zero'
-
- dbg_print(); // Just before to exit
-
- cerr << "\n==========================="
- << "\nTotal time (ms) : " << elapsed
- << "\nNodes searched : " << nodes
- << "\nNodes/second : " << 1000 * nodes / elapsed << endl;
+// clang-format on
+
+} // namespace
+
+namespace Stockfish::Benchmark {
+
+// Builds a list of UCI commands to be run by bench. There
+// are five parameters: TT size in MB, number of search threads that
+// should be used, the limit value spent for each position, a file name
+// where to look for positions in FEN format, and the type of the limit:
+// depth, perft, nodes and movetime (in milliseconds). Examples:
+//
+// bench : search default positions up to depth 13
+// bench 64 1 15 : search default positions up to depth 15 (TT = 64MB)
+// bench 64 1 100000 default nodes : search default positions for 100K nodes each
+// bench 64 4 5000 current movetime : search current position with 4 threads for 5 sec
+// bench 16 1 5 blah perft : run a perft 5 on positions in file "blah"
+std::vector<std::string> setup_bench(const std::string& currentFen, std::istream& is) {
+
+ std::vector<std::string> fens, list;
+ std::string go, token;
+
+ // Assign default values to missing arguments
+ std::string ttSize = (is >> token) ? token : "16";
+ std::string threads = (is >> token) ? token : "1";
+ std::string limit = (is >> token) ? token : "13";
+ std::string fenFile = (is >> token) ? token : "default";
+ std::string limitType = (is >> token) ? token : "depth";
+
+ go = limitType == "eval" ? "eval" : "go " + limitType + " " + limit;
+
+ if (fenFile == "default")
+ fens = Defaults;
+
+ else if (fenFile == "current")
+ fens.push_back(currentFen);
+
+ else
+ {
+ std::string fen;
+ std::ifstream file(fenFile);
+
+ if (!file.is_open())
+ {
+ std::cerr << "Unable to open file " << fenFile << std::endl;
+ exit(EXIT_FAILURE);
+ }
+
+ while (getline(file, fen))
+ if (!fen.empty())
+ fens.push_back(fen);
+
+ file.close();
+ }
+
+ list.emplace_back("setoption name Threads value " + threads);
+ list.emplace_back("setoption name Hash value " + ttSize);
+ list.emplace_back("ucinewgame");
+
+ for (const std::string& fen : fens)
+ if (fen.find("setoption") != std::string::npos)
+ list.emplace_back(fen);
+ else
+ {
+ list.emplace_back("position fen " + fen);
+ list.emplace_back(go);
+ }
+
+ return list;