# SimCluster

## Play The Demo

### Requirements

- `git` is installed, if not, check [Git install instructions](https://git-scm.com/downloads)
- `tiup` is installed, if not, check [TiUP install instructions](https://docs.pingcap.com/tidb/stable/tiup-overview/)
- `python` (v3) is installed, if not, check [python download instructions](https://wiki.python.org/moin/BeginnersGuide/Download)
- `mysql` client is installed, if not, check [MySQL Community Downloads](https://dev.mysql.com/downloads/installer/)

### Clone Code

```sh
git clone git@github.com:tidbcloud/SimCluster.git

cd SimCluster
```

### Setup Python Env

```sh
# create venv
python -m venv ~/.simcluster_venv

# active venv
source ~/.simcluster_venv/bin/activate

# install dependencies
python -m pip install .
```

> If you use VSCode, open the Command Palette (⇧⌘P), search for the Python: Create Environment command, and select it. Then select `Enter interpreter path...` and input `~/.simcluster_venv/bin/python` to select correct python interpreter.

### Setup TiDB Cluster

```sh
tiup playground v8.5.1 --tiproxy 1

...
🎉 TiDB Playground Cluster is started, enjoy!

Connect TiDB:    mysql --comments --host 127.0.0.1 --port 4000 -u root
Connect TiProxy: mysql --comments --host 127.0.0.1 --port 6000 -u root
TiDB Dashboard:  http://127.0.0.1:2379/dashboard
Grafana:         http://127.0.0.1:3000
```

### Create Views

```sh
mysql --comments --host 127.0.0.1 --port 6000 -u root < views.sql
```

### Statement Summary Configuration

Set statement summary refresh interval to 1 min:

```sh
echo "set global tidb_stmt_summary_refresh_interval = 60" | mysql --comments --host 127.0.0.1 --port 6000 -u root
```

### Prepare TPCC Data

```sh
tiup bench tpcc --warehouses 4 --parts 4 prepare
```

### Capture Statistical Information

```sh
python capture.py statistics
```

It will create a `simcluster.db` file.

### Run TPCC Workload

```sh
tiup bench tpcc --warehouses 4 --time 10m run
```

### Capture Workload

```sh
python capture.py statements
```

It will update `simcluster.db` to add statements history.

### Replay Workload

```sh
python app.py prepare
python app.py replay
```

## Contribute

### Submit Issue

For bugs:

- Paste the detail commands you have executed
- Paste the detail error messages (include full error stack, if any)

For questions:

- Describes what you have done (if any)
- Describes what is blocking you from going futher

### Submit PR

For developer, please install required dependencies:

```sh
python -m pip install '.[dev]'
```

Before you create a pull request:

- please make sure you have execute `make check` and the command doesn't print any error
- please execute `make fmt` to format code before commit

If you have added new dependencies to the project, please remember to add them into `pyproject.toml`:

- add the dependencies to `project.dependencies` if they are runtime dependencies
- otherwise，to `project.optional-dependencies`

If you want to know which version should be added, you can execute `python -m pip freeze` to check, example:

```sh
# If you installed matplotlib and want to know it's version
python -m pip freeze | grep matplotlib
matplotlib==3.10.1
```