Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 Commits

Repository files navigation

chess-bench

A comprehensive multi-language benchmark suite for chess game analysis, comparing Python, Node.js, Rust, and Go implementations.

What It Does

Fetches games from Chess.com public API and analyzes them using Stockfish engine to calculate move accuracy based on WDL (Win-Draw-Loss) probabilities.

Features:

  • Fetches games from Chess.com API for any username
  • Parses PGN (Portable Game Notation) files
  • Analyzes each position with Stockfish at configurable depth
  • Calculates accuracy scores using WDL probabilities
  • Supports parallel processing with configurable workers/threads
  • Benchmarks both Stockfish analysis and pure PGN parsing

Benchmark Results

Test Configuration:

  • Player: hikaru (1000 games)
  • Stockfish: depth 4, WDL enabled
  • Parallelization: 4 workers × 1 thread each
  • Hardware: Apple Silicon (M-series)

Stockfish Analysis (1000 games)

RankLanguageLibraryGames/secMoves/secTime
🥇Rustshakmaty18.731,64351.5s
🥈Pythonpython-chess18.671,62453.6s
🥉Node.jschess.js17.731,50356.4s
4Gonotnil/chess14.651,27966.8s

Pure PGN Parsing (1000 games, no Stockfish)

RankLanguageLibraryGames/secMoves/secTimevs Rust
🥇Rustshakmaty35,1213,001,4310.03s1x
🥈Pythonpython-chess25121,8173.98s140x slower
🥉Node.jschess.js21418,2714.64s164x slower
4Gonotnil/chess403,48324.5s878x slower

Parallelization Strategy Comparison

StrategyGames/secResult
4 workers × 1 SF thread20.03✅ Best for depth 4
2 workers × 2 SF threads11.7341% slower
1 worker × 4 SF threads5.9870% slower

At shallow depths, game-level parallelism beats Stockfish multi-threading.


Key Findings

1. Rust Wins Both Benchmarks

After optimizing I/O buffering (256 bytes vs 8KB default) and eliminating unnecessary allocations, Rust achieves:

  • Fastest Stockfish analysis (18.73 games/sec)
  • 878x faster PGN parsing than Go

2. Python is Surprisingly Competitive

Despite being interpreted, Python nearly matches Rust for Stockfish analysis due to:

  • Mature subprocess module with optimized IPC
  • Excellent python-chess library
  • GIL irrelevant (each worker has its own Stockfish process)

3. Go's Library is the Bottleneck

The notnil/chess library is critically slow:

  • 4x slower than Python for PGN parsing
  • 878x slower than Rust
  • This is a library problem, not a Go problem

4. The Stockfish Bottleneck Effect

Time breakdown per game:
├── Stockfish analysis: ~95%
├── PGN parsing: ~3%
├── IPC overhead: ~1.5%
└── FEN generation: ~0.5%

When Stockfish dominates, language speed matters less—but library quality still matters!


Installation

Prerequisites

  • Stockfish - Install and note the path (default: /opt/homebrew/bin/stockfish)
  • Python 3.8+ with pip
  • Node.js 18+ with npm
  • Rust 1.70+ with cargo
  • Go 1.21+

Setup

git clone https://github.com/Bot-Rakshit/chess-bench.git
cd chess-bench
# Pythoncd python && pip install -r requirements.txt &&cd ..
# Node.jscd node && npm install &&cd ..
# Rustcd rust && cargo build --release &&cd ..
# Gocd go && go build -o benchmark benchmark.go && go build -o pgn_benchmark pgn_benchmark.go &&cd ..

Usage

Stockfish Analysis

Analyze games with full Stockfish evaluation:

# Python
python python/benchmark.py <username><games> --workers 4 --threads 1 --depth 4
# Node.js
node node/benchmark.js <username><games> --workers 4 --threads 1 --depth 4
# Rust
./rust/target/release/benchmark <username><games> --workers 4 --threads 1 --depth 4
# Go
./go/benchmark <username><games> -workers 4 -threads 1 -depth 4

Example:

python python/benchmark.py hikaru 100 --workers 4 --threads 1 --depth 4

Pure PGN Parsing

Test library parsing speed without Stockfish:

# Rust (fastest)
./rust/target/release/pgn_benchmark hikaru 1000
# Python
python python/pgn_benchmark.py hikaru 1000
# Node.js
node node/pgn_benchmark.js hikaru 1000
# Go
./go/pgn_benchmark hikaru 1000

Project Structure

chess-bench/
├── README.md
├── .gitignore
├── python/
│ ├── benchmark.py # Stockfish analysis
│ ├── pgn_benchmark.py # Pure PGN parsing
│ └── requirements.txt
├── node/
│ ├── benchmark.js # Stockfish analysis
│ ├── pgn_benchmark.js # Pure PGN parsing
│ └── package.json
├── rust/
│ ├── Cargo.toml
│ └── src/
│ ├── main.rs # Stockfish analysis
│ └── bin/
│ └── pgn_benchmark.rs
└── go/
├── go.mod
├── benchmark.go # Stockfish analysis
└── pgn_benchmark.go # Pure PGN parsing

How It Works

Chess.com API

The benchmark fetches games using Chess.com's public API:

  • Archives endpoint: https://api.chess.com/pub/player/{username}/games/archives
  • Games endpoint: https://api.chess.com/pub/player/{username}/games/{YYYY}/{MM}

WDL Accuracy Calculation

  1. Get WDL (Win/Draw/Loss) probabilities from Stockfish for each position
  2. Convert to win probability: P = (W + D×0.5) / 1000
  3. Calculate accuracy per move:
    • If position improved: accuracy = 100%
    • If position worsened: accuracy = max(0, 100 × (1 - loss × 2))
  4. Average all move accuracies for the target player

Libraries Used

LanguageLibraryVersionNotes
Pythonpython-chess1.10+Mature, excellent Stockfish integration
Node.jschess.js1.0.0-betaEasy to use, TypeScript support
Rustshakmaty0.28Zero-copy, SIMD optimized, blazingly fast
Gonotnil/chess1.9.0Simple API, but very slow PGN parsing

Configuration

Stockfish Path

Default path is /opt/homebrew/bin/stockfish. To change:

  • Python: Edit STOCKFISH_PATH in benchmark.py
  • Node.js: Edit STOCKFISH_PATH in benchmark.js
  • Rust: Edit STOCKFISH_PATH in src/main.rs
  • Go: Edit StockfishPath in benchmark.go

Parameters

ParameterDescriptionDefault
usernameChess.com usernamehikaru
gamesNumber of games to analyze1000
--workersParallel workers4
--threadsStockfish threads per worker1
--depthStockfish search depth4

Contributing

PRs welcome! Especially interested in:

  • Alternative Go chess libraries (to replace slow notnil/chess)
  • Performance optimizations
  • Additional language implementations (C++, Zig, Java, etc.)
  • Higher depth analysis comparisons

License

MIT

About

Cross-language benchmark of chess libraries in Rust, Go, Python, and JavaScript.

Topics

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages