An open API service indexing awesome lists of open source software.

https://github.com/serapath/scenario-simulator

start multiple `<appfile>.js` processes based on a `<scenario>.json` file
https://github.com/serapath/scenario-simulator

Last synced: about 1 year ago
JSON representation

start multiple `<appfile>.js` processes based on a `<scenario>.json` file

Awesome Lists containing this project

README

          

# scenario-simulator
start multiple `.js` processes based on a `.json` file

# define
`npm install scenario-simulator`

for example make a new project folder ``
```js
// /package.json
{
"private": true,
"name": "example",
"version": "0.0.0",
"description": "scenario-simulator demo",
"dependencies": {
"scenario-simulator": "^0.0.0"
},
"scripts": {
"start": "simulate"
}
}

```

all file paths further below are prefixed with:
* `/demo` (where `./demo` is the default)

This can be changed to something else (e.g. `./examples`),
by setting an environment variable explicitly:
```js
{
"scripts": {
"start": "SIM=examples simulate"
}
}
```

Instead of the default (`./demo`) we are using `./examples`

---

with `/demo/node_modules/_start.js`
```js
const firestarter = 5
module.exports = function start (index) {
if (index === firestarter) { // e.g. to choose init logic
console.log(`
i am the fire starter
edit logic to kick off the scenario if you want :-)`)
}
}
```

with `/demo/scenario/cmd/app1.js`

```js
const start = require('_start')
const simulate = require('scenario-simulator')
const { pid, list } = simulate(chunk => {
console.log({ message: chunk.toString() }) // e.g. { message: 'asdf' }
})
console.log({ pid, list })
// e.g.
// { pid: 'app1:34955', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }
start(list.indexOf(pid))
```

and `/demo/scenario/cmd/app2.js`
```js
const start = require('_start')
const simulate = require('scenario-simulator')
const { pid, list } = simulate(chunk => console.log('hello'))
start(list.indexOf(pid))
```
and a first scenario file

```js
// /scenario/1.json
{
"app1.js": 5, // amount of instances of `app1.js`
"app2.js": 3, // amount of instances of `app2.js`
// ...
}
```

# use
**usage** `simulate []`

The following describes how to start the simulator, which launches multiple process instances according to the scenario file.
It then shows how to send messages to individual processes, which they can listen and react to.
The purpose of sending messages is to simulate local user input in one particular process.

```bash
npm start
# [ROOT] ERROR:
# missing `scenario_name` argument
npm start foo
# [ROOT] ERROR:
# Error: Cannot find module '/scenario/foo.json'
# Require stack:
# - /node_modules/scenario-simulator/src/scenario-simulator.js
# ...
npm start 1
# [ROOT] COMMANDS:
# "/help": {
# "args": "",
# "demo": "/help",
# "info": "(to see this message)"
# },
# "/ ": {
# "args": {
# "": {
# "0": "app1:36659",
# "1": "app1:36660",
# "2": "app1:36661",
# "3": "app1:36662",
# "4": "app1:36663",
# "5": "app2:36664",
# "6": "app2:36665",
# "7": "app2:36666"
# },
# "": "string"
# },
# "demo": "/0 hello world",
# "info": "send to with a process name"
# }

# [ROOT] ----------------------------------------
# [app1:34955] { pid: 'app1:34955', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }
# [app1:34957] { pid: 'app1:34957', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }
# [app1:34956] { pid: 'app1:34956', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }
# [app1:34958] { pid: 'app1:34958', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }
# [app1:34959] { pid: 'app1:34959', list: ['app1:36659','app1:36660','app1:36661','app1:36662','app1:36663','app2:36664','app2:36665','app2:36666'] }

npm start 1 foo
# [ROOT] ERROR:
# optional `port` argument must be a number

npm start 1 999111
# [ROOT] ERROR:
# try: 0 < port < 65534

npm start 1 9000
# [ROOT] COMMANDS:
# "/help": {
# "args": "",
# "demo": "/help",
# "info": "(to see this message)"
# },
# "/ ": {
# "args": {
# "": {
# "0": "app1:9000",
# "1": "app1:9001",
# "2": "app1:9002",
# "3": "app1:9003",
# "4": "app1:9004",
# "5": "app2:9005",
# "6": "app2:9006",
# "7": "app2:9007"
# },
# "": "string"
# },
# "demo": "/0 hello world",
# "info": "send to with a process name"
# }

# [ROOT] ----------------------------------------
# [app1:9000] { pid: 'app1:9000', list: ['app1:9000','app1:9001','app1:9002','app1:9003','app1:9004','app2:9005','app2:9006','app2:9007'] }
# [app1:9002] { pid: 'app1:9002', list: ['app1:9000','app1:9001','app1:9002','app1:9003','app1:9004','app2:9005','app2:9006','app2:9007'] }
# [app1:9001] { pid: 'app1:9001', list: ['app1:9000','app1:9001','app1:9002','app1:9003','app1:9004','app2:9005','app2:9006','app2:9007'] }
# [app1:9003] { pid: 'app1:9003', list: ['app1:9000','app1:9001','app1:9002','app1:9003','app1:9004','app2:9005','app2:9006','app2:9007'] }
# [app1:9004] { pid: 'app1:9004', list: ['app1:9000','app1:9001','app1:9002','app1:9003','app1:9004','app2:9005','app2:9006','app2:9007'] }
# [app2:45450] i am the fire starter , edit logic to kick off the scenario if you want :-)

asdf
# [ROOT] type: `/help`

/help
# [ROOT] COMMANDS:
# "/help": {
# "args": "",
# "demo": "/help",
# "info": "(to see this message)"
# },
# "/ ": {
# "args": {
# "": {
# "0": "app1:9000",
# "1": "app1:9001",
# "2": "app1:9002",
# "3": "app1:9003",
# "4": "app1:9004",
# "5": "app2:9005",
# "6": "app2:9006",
# "7": "app2:9007"
# },
# "": "string"
# },
# "demo": "/0 hello world",
# "info": "send to with a process name"
# }

/10 asdf
# [ROOT] not a valid number: /10

/0 asdf
# [app1:9000] { message: 'asdf' }

```

# contribute
```bash
git clone https://github.com/serapath/scenario-simulator.git
cd scenario-simulator
npm install
npm link # adds itself as a global command for npm scripts
npm link scenario-simulator # installs itself as a local dependency to be requireable
npm start # follow README instructions above
# edit `./app` and `./scenario` for better testing data
```