# Alternative performance testing of MongoDB

## In this learning path

- [Introduction](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/)
- [Install MongoDB on Arm](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/run_mongodb/)
- [Creating MongoDB test scenarios](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/create_replica_set/)
- [Benchmark MongoDB on Arm with Yahoo Cloud Serving Benchmark (YCSB)](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/benchmark_mongodb-8.0/)
- [Three node replica set testing with YCSB](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/replica_set_testing/)
- [Alternative performance testing of MongoDB](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/perf_mongodb/)
- [Automate MongoDB Performance Benchmarking Infrastructure Setup with Pulumi](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/automate_setup_pulumi/)
- [Next Steps](https://learn.arm.com/learning-paths/servers-and-cloud-computing/mongodb/_next-steps/)

To measure the performance of MongoDB, use the [MongoDB performance test tool](https://github.com/idealo/mongodb-performance-test).  
This is an open source Java application that tests the MongoDB performance, such as latency and throughput, by running one or more threads executing either all the same or different database operations, such as Inserts, Updates, Deletes, Counts or Finds until a defined number of operations is executed or a defined maximum runtime is reached.

## Install OpenJDK packages

Install the appropriate run-time environment to be able to use the performance test tool.

```
sudo apt install default-jre default-jdk -y
```

```
sudo yum install java-17-openjdk
```

For more information see the [OpenJDK](https://openjdk.org/install/) website.

## Setup the MongoDB performance test tool

On your instance running MongoDB (you may need to start a new terminal), clone the `MongoDB performance test tool` project:

```
git clone https://github.com/idealo/mongodb-performance-test.git
```

Now change into the project folder and execute the JAR file to see its usage instructions:

```
cd mongodb-performance-test
java -jar ./latest-version/mongodb-performance-test.jar
```

This will print a description of how to use the Java application.

## Run Insert test

Run a test that inserts documents on `localhost:27017` (default).

First, set an environment variable for the JAR file path for convenience:

```
export jarfile=./latest-version/mongodb-performance-test.jar
```

Use the following options:
- `-m` defines the test mode (e.g., `insert`, `update_one`).
- `-o` defines the number of operations (iterations).
  - Alternatively, use `-d` to specify a duration limit (in seconds).
- `-t` defines the number of threads.
- `-db` defines the database to use.
- `-c` defines the collection to use.

For example, run an insert test for 1 million operations using 10 threads:

```
java -jar $jarfile -m insert -o 1000000 -t 10 -db test -c perf
```

As the test runs, the progress count will be printed periodically. It will increase until it reaches 1 million, and then the test will end.

## Run Update-one test

Similarly, to run an update test (updating one document per query) using 10, 20, and finally 30 threads for 1 hour each (3 hours total), run the following command:

```
java -jar $jarfile -m update_one -d 3600 -t 10 20 30 -db test -c perf
```

For instructions on running any other tests or more details on the metrics reported, refer to the [github project for the performance tool](https://github.com/idealo/mongodb-performance-test#readme).

## View the results

During each test, statistics over the last second are printed to the console every second. After the test completes, final summary statistics are displayed. The following is example output from the end of the Insert test run:

```
__output__-- Timers ----------------------------------------------------------------------
__output__stats-per-run-INSERT
__output__2022-07-05 19:14:45,894 [main] INFO  d.i.mongodb.perf.MongoDbAccessor - <<< closeConnections localhost:27017
__output__             count = 1000000
__output__         mean rate = 5001.61 calls/second
__output__     1-minute rate = 5042.28 calls/second
__output__     5-minute rate = 3699.92 calls/second
__output__    15-minute rate = 2963.07 calls/second
__output__               min = 0.36 milliseconds
__output__               max = 15.59 milliseconds
__output__              mean = 1.93 milliseconds
__output__            stddev = 0.66 milliseconds
__output__            median = 1.87 milliseconds
__output__              75% <= 1.99 milliseconds
__output__              95% <= 2.22 milliseconds
__output__              98% <= 2.64 milliseconds
__output__              99% <= 3.85 milliseconds
__output__            99.9% <= 15.59 milliseconds
```

Detailed per-second metrics are also output to a CSV file named `stats-per-second-[mode].csv` (e.g., `stats-per-second-INSERT.csv`), located in the same folder as the JAR file. `[mode]` corresponds to the executed mode(s), such as `INSERT`, `UPDATE_ONE`, `DELETE_ONE`, etc.
