How do I check a model in continuous integration?¶
In short
- You build: a model file with three assertions, a
naga checkrun that fails the build when one is false, and the workflow file that runs it on every push.- You need: Python 3.11 or later,
pip install nagasim, and a GitHub repository for the last step. You can run the rest on your own machine.- Time: about ten minutes.
A model changes as people edit it, and an edit can quietly break what it was built to show. By the end you pin three claims about a queue in the model file. You read a pass, a fail and the exit code that continuous integration uses. Then you generate a workflow that runs the checks on every push.
This page has three parts: Step by step, Check yourself and If stuck. The last section lists the reference pages for the elements used.
Step by step¶
- Save this script as
goal-ci.py. It writesmodel.jsonwith three assertions on a one-server queue, then callsnaga checkon it, as a CI job would. The third assertion is false on purpose, so you can see a failure.
from nagasim import build as nb, cli
# A queue with three claims about how it behaves. Each is an assertion: a KPI, a comparison and a
# value, checked over the model's own replications with a confidence interval.
m = nb.Model(time_limit=480, warmup=0, assertions=[
{"name": "server_not_saturated", "kpi": "resource/Server/Usage % (snapshot)", "op": "<=",
"value": 95, "replications": 20},
{"name": "wait_under_five_minutes", "kpi": "resource/Server/Time in Queue Avg", "op": "<=",
"value": 5, "replications": 20},
{"name": "wait_under_two_minutes", "kpi": "resource/Server/Time in Queue Avg", "op": "<=",
"value": 2, "replications": 20},
])
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=1.0))
arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.25)) >> m.dispose()
m.save("model.json")
# What a CI job runs. The exit code is the job's verdict: 0 passes the build, 1 fails it.
print("$ naga check model.json --junit report.xml")
code = cli.main(["check", "model.json", "--junit", "report.xml"])
print("exit code:", code)
# The workflow file that runs it on every push.
print("$ naga ci-template --path models/")
cli.main(["ci-template", "--path", "models/"])
- Run it.
python goal-ci.py
You should see this. The script seeds itself, so the numbers are the same on your machine.
$ naga check model.json --junit report.xml
model.json:
[PASS] gate opens with no errors and runs one replication
[PASS] assertion server_not_saturated: ok
[PASS] assertion wait_under_five_minutes: ok
[FAIL] assertion wait_under_two_minutes: assertion 'wait_under_two_minutes' failed: resource/Server/Time in Queue Avg = 2.86501 [2.30459, 3.42543] <= 2.0
laws: 5 passed, 0 warning(s)
exit code: 1
$ naga ci-template --path models/
# Generated by `naga ci-template`. Runs every model's assertions and
# reality checks on push/PR: a merge that regresses a KPI
# an assertion pins, or breaks a reality-check constraint, fails CI.
# `naga check` is engine-side, so installing the engine alone is enough;
# change the install step with `naga ci-template --install "<command>"`.
name: Model checks
on:
push:
pull_request:
jobs:
naga-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install naga
run: |
pip install .
- name: naga check
run: |
shopt -s globstar nullglob
models=("models/"/**/*.json)
if [ ${#models[@]} -eq 0 ]; then
echo "no *.json models under models/"
exit 0
fi
naga check "${models[@]}" --junit naga-check.xml
- name: Upload JUnit report
if: always()
uses: actions/upload-artifact@v4
with:
name: naga-check-results
path: naga-check.xml
-
Read the assertions. Each one names a KPI, a comparison and a value, and runs over its own
replications. The check computes a 95% confidence interval across them. -
Read the three verdicts. An assertion passes when the whole interval satisfies the claim and fails when the whole interval breaks it. The first two pass. The third fails, with the mean wait at 2.87 minutes and an interval from 2.30 to 3.43 against a limit of 2.
-
Read the exit code. It is 1 because one assertion failed. A CI job treats a non-zero exit as a failed build, so a merge that makes the model break its own claim is stopped. The third verdict an assertion can have is
inconclusive: the interval straddles the limit, so more replications are needed. It fails the build only with--strict. -
Read the
lawsline.naga checkalso runs five built-in sanity checks, such as Little's law, on the same runs. -
Read the workflow.
naga ci-templateprints a GitHub Actions file that installs the engine, runsnaga checkon every model undermodels/, and uploads the JUnit report as a build artifact. Save it as.github/workflows/model-checks.yml, and put your models undermodels/. -
Fix the failing claim. Change the third limit from 2 to 3.5, or fix the model, and run the script again. The exit code becomes 0.
Check yourself¶
- Predict, then run: if you lower
replicationsfor the third assertion from 20 to 3, will it still fail, or turn inconclusive? Try it, then runnaga check model.json --strict. - Explain it back: why does the check judge the whole interval and not just the mean?
Done when¶
- [ ] The script prints two passes, one failure, exit code 1 and a workflow file.
- [ ] You can say what
inconclusivemeans and what--strictdoes with it. - [ ] You can say which file you commit to run the checks on every push.
If stuck¶
| What happens | Likely cause | Fix |
|---|---|---|
unknown KPI in a verdict |
The KPI key is misspelt | The message lists close matches. The KPI reference has every key. |
| An assertion flips between runs | Its interval straddles the limit, so it is inconclusive or close to it |
Raise replications, or move the limit to what you can defend. |
| The CI job fails to install the engine | The default install step is pip install ., which installs the repository itself |
Pass --install "pip install nagasim" to naga ci-template in a repository that only holds models. |
| The job passes with "no models found" | No *.json file is under the --path folder |
Move the models there, or pass the right --path. |
| Exit code 2 | A file is missing or is not valid JSON | Open the file named in the message. |
Elements this page uses¶
- Python:
Modelwithassertions, andassert_modelfor a pytest suite. - Blocks: CREATE, PROCESS and DISPOSE.
- KPIs:
Usage % (snapshot)andTime in Queue Avgin the resource summary. - Command line:
naga check,naga ci-templateandnaga verify, which checks the engine itself against queueing theory. - Background: Model assertions and CI covers reality checks and path checks.