npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scenario-simulator

v0.0.18

Published

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

Downloads

2

Readme

scenario-simulator

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

define

npm install scenario-simulator

for example make a new project folder <project>

// <project>/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:

  • <project>/demo (where ./demo is the default)

This can be changed to something else (e.g. ./examples), by setting an environment variable explicitly:

{
  "scripts": {
    "start": "SIM=examples simulate"
  }
}

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


with <project>/demo/node_modules/_start.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 <node ${firestarter}>
      edit logic to kick off the scenario if you want :-)`)
  }
}

with <project>/demo/scenario/cmd/app1.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 <project>/demo/scenario/cmd/app2.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

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

use

usage simulate <scenario-name> [<port>]

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.

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

<ctrl-c>

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)"
#   },
#   "/<node> <text message>": {
#     "args": {
#       "<node>": {
#         "0": "app1:9000",
#         "1": "app1:9001",
#         "2": "app1:9002",
#         "3": "app1:9003",
#         "4": "app1:9004",
#         "5": "app2:9005",
#         "6": "app2:9006",
#         "7": "app2:9007"
#       },
#       "<text message>": "string"
#     },
#     "demo": "/0 hello world",
#     "info": "send <text message> to <node> 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 <node 5>, 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)"
#   },
#   "/<node> <text message>": {
#     "args": {
#       "<node>": {
#         "0": "app1:9000",
#         "1": "app1:9001",
#         "2": "app1:9002",
#         "3": "app1:9003",
#         "4": "app1:9004",
#         "5": "app2:9005",
#         "6": "app2:9006",
#         "7": "app2:9007"
#       },
#       "<text message>": "string"
#     },
#     "demo": "/0 hello world",
#     "info": "send <text message> to <node> with a process name"
#   }

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

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

<ctrl-c>

contribute

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