JS Rules validator using cadvisor
You can run the code in a VSCODE devcontainer, which will launch the PostGres database and the metrics service cAdvisor
You need to have Docker, Node, VSCode, and the Dev Container extension installed on your machine.
The tests run in a docker container. The database is running in another container on the docker network. The monitoring is realized through another container running cAdvisor
Running containers:
- the Node.js container with the test script, and uses pg as a driver to connect to the database,
- the Postgres container with the database,
- the cAdvisor container to monitor the execution and get the relevant data.
graph TD
A[Node.js container] -->|Connects to| B(Postgres container)
A -->|Calls API| C(cAdvisor container)
B -->|Stores data in| D(Postgres docker volume)
C -->|Returns data| A
The code is run in a devcontainer for VSCode. If you do not use VSCode and want to run the code in another container, you need to:
- Create a Dockerfile to build your project and run it in a Node container.
- Use the Dockerfile in the
docker-compose.yml
file instead of the devcontainer's image.
There is a tutorial available here that you can read and adapt to your needs: Use containers for Node.js development
The default export is the driver that connects to the the database. Just use db.query()
to run a SQL query.
This contains the code to connect to the cAdvisor API using node-fetch
.
setStartDate
: You need to set a start time BEFORE the testgetMonitoringData
: get the raw statistic events from cAdvisor AFTER the test
Using stats you can:
getStats
: pass the raw events you got fromgetMonitoringData
to get usable statisticsdiffStats
: pass two statistic objects to get the difference between the two. Useful to compare test scenariidisplayStats
: pass the statistic object to log it in the console
This is where you should put the files for your test.
delay
: pass a number in milliseconds to wait in the process
You can edit the postgres user in the .dockercontainer/.env
file.
You should do the following steps for your test:
- Write the code you need in
src
a. Do not forget to set a time withsetStartDate
before the test. b. Run the test. c. Get the monitoring data withgetMonitoringData
after the test, then get the statistics withgetStats
. d. Use the pino logger to log the results both in console and in file. - Execute the code in the root
index.js
file. - Run the code in the devcontainer with
npm start
to see the results. - Find the logs in the
logs
folder or in the console.
The data is fetched through the REST API of the cAdvisor container. The 1.3 API's documentation is here: cAdvisor API. It is a bit limited, but you can observe what data is returned if you need more, or dig through the GO code here: container.go . There is also a web UI available at http://localhost:8080
where you can see more data: cAdvisor Web UI.
We measure the following data:
- CPU total usage: Amount of time for which the CPU was used for processing instructions in nanoseconds.
- CPU user usage: Same as befor but in the user space.
- Memory usage: Memory usage of the container in bytes.
- Network RX: Measure the amount of network traffic received by the container in bytes.
- Network TX: Measure the amount of network traffic transmitted by the container in bytes.