# Benchmark Camera

This example showcases how the
[BenchmarkIn](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/benchmark_in.md) node works. It measures the
FPS and latency of some message stream, in this case, the video stream from the camera.

By default, the node will aggregate 50 messages and then print (firmware warning) the FPS and latency. In our case, that's 33.3ms
(30fps) * 50 messages = 1.665 seconds.

## Demo

```bash
Benchmark $ python3.9 benchmark_camera.py
[18443010211F850E00] [1.1] [3.367] [BenchmarkIn(1)] [warning] FPS: 30.019516
[18443010211F850E00] [1.1] [3.367] [BenchmarkIn(1)] [warning] Messages took 1.6655831 s
[18443010211F850E00] [1.1] [3.367] [BenchmarkIn(1)] [warning] Average latency: 0.010152339 s
[18443010211F850E00] [1.1] [5.067] [BenchmarkIn(1)] [warning] FPS: 30.000103
[18443010211F850E00] [1.1] [5.067] [BenchmarkIn(1)] [warning] Messages took 1.6666609 s
[18443010211F850E00] [1.1] [5.067] [BenchmarkIn(1)] [warning] Average latency: 0.010102791 s
[18443010211F850E00] [1.1] [6.767] [BenchmarkIn(1)] [warning] FPS: 30.000868
```

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

### examples/benchmark_camera.pipeline.json

```json
{
  "pipeline": {
    "connections": [
      {
        "node1Id": 0,
        "node1Output": "0",
        "node1OutputGroup": "dynamicOutputs",
        "node2Id": 1,
        "node2Input": "input",
        "node2InputGroup": ""
      }
    ],
    "globalProperties": {
      "calibData": null,
      "cameraTuningBlobSize": null,
      "cameraTuningBlobUri": "",
      "leonCssFrequencyHz": 700000000.0,
      "leonMssFrequencyHz": 700000000.0,
      "pipelineName": null,
      "pipelineVersion": null,
      "sippBufferSize": 18432,
      "sippDmaBufferSize": 16384,
      "xlinkChunkSize": -1
    },
    "nodes": [
      [
        1,
        {
          "alias": "",
          "id": 1,
          "ioInfo": [
            [
              [
                "",
                "input"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 3,
                "name": "input",
                "queueSize": 4,
                "type": 3,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "passthrough"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 4,
                "name": "passthrough",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "report"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 5,
                "name": "report",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 3,
          "name": "BenchmarkIn",
          "parentId": -1,
          "properties": {
            "attachLatencies": false,
            "logReportsAsWarnings": true,
            "reportEveryNMessages": 50
          }
        }
      ],
      [
        0,
        {
          "alias": "",
          "id": 0,
          "ioInfo": [
            [
              [
                "",
                "raw"
              ],
              {
                "blocking": false,
                "group": "",
                "id": 1,
                "name": "raw",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "dynamicOutputs",
                "0"
              ],
              {
                "blocking": false,
                "group": "dynamicOutputs",
                "id": 2,
                "name": "0",
                "queueSize": 8,
                "type": 0,
                "waitForMessage": false
              }
            ],
            [
              [
                "",
                "inputControl"
              ],
              {
                "blocking": true,
                "group": "",
                "id": 0,
                "name": "inputControl",
                "queueSize": 3,
                "type": 3,
                "waitForMessage": false
              }
            ]
          ],
          "logLevel": 3,
          "name": "Camera",
          "parentId": -1,
          "properties": {
            "boardSocket": 0,
            "calibAlpha": null,
            "cameraName": "",
            "colorOrder": 0,
            "fp16": false,
            "fps": -1.0,
            "imageOrientation": -1,
            "initialControl": {
              "aeLockMode": false,
              "aeMaxExposureTimeUs": 26241370,
              "aeRegion": {
                "height": 400,
                "priority": 26240090,
                "width": 25709,
                "x": 25434,
                "y": 400
              },
              "afRegion": {
                "height": 400,
                "priority": 26240621,
                "width": 25946,
                "x": 25965,
                "y": 400
              },
              "antiBandingMode": 144,
              "autoFocusMode": 3,
              "awbLockMode": false,
              "awbMode": 90,
              "brightness": 1,
              "captureIntent": 1,
              "chromaDenoise": 109,
              "cmdMask": 0,
              "contrast": 90,
              "controlMode": 109,
              "effectMode": 103,
              "expCompensation": -112,
              "expManual": {
                "exposureTimeUs": 26219364,
                "frameDurationUs": 26239853,
                "sensitivityIso": 26239596
              },
              "frameSyncMode": 144,
              "lensPosAutoInfinity": 1,
              "lensPosAutoMacro": 0,
              "lensPosition": 0,
              "lensPositionRaw": 0.0,
              "lowPowerNumFramesBurst": 90,
              "lowPowerNumFramesDiscard": 107,
              "lumaDenoise": 1,
              "miscControls": [],
              "saturation": 106,
              "sceneMode": 102,
              "sharpness": 144,
              "strobeConfig": {
                "activeLevel": 90,
                "enable": 1,
                "gpioNumber": 103
              },
              "strobeTimings": {
                "durationUs": 26241389,
                "exposureBeginOffsetUs": 26241133,
                "exposureEndOffsetUs": 26241114
              },
              "wbColorTemp": 400
            },
            "interleaved": true,
            "isp3aFps": 0,
            "ispScale": {
              "horizDenominator": 0,
              "horizNumerator": 0,
              "vertDenominator": 0,
              "vertNumerator": 0
            },
            "mockIspHeight": -1,
            "mockIspWidth": -1,
            "numFramesPoolIsp": 3,
            "numFramesPoolPreview": 4,
            "numFramesPoolRaw": 3,
            "numFramesPoolStill": 4,
            "numFramesPoolVideo": 4,
            "outputRequests": [
              {
                "enableUndistortion": null,
                "fps": {
                  "value": null
                },
                "resizeMode": 0,
                "size": {
                  "value": {
                    "index": 0,
                    "value": [
                      4056,
                      3040
                    ]
                  }
                },
                "type": null
              }
            ],
            "previewHeight": 300,
            "previewKeepAspectRatio": false,
            "previewWidth": 300,
            "rawPacked": null,
            "resolutionHeight": -1,
            "resolutionWidth": -1,
            "sensorCropX": -1.0,
            "sensorCropY": -1.0,
            "sensorType": -1,
            "stillHeight": -1,
            "stillWidth": -1,
            "videoHeight": -1,
            "videoWidth": -1,
            "warpMeshHeight": 0,
            "warpMeshSource": -1,
            "warpMeshStepHeight": 32,
            "warpMeshStepWidth": 32,
            "warpMeshUri": "",
            "warpMeshWidth": 0
          }
        }
      ]
    ]
  }
}
```

## Source code

#### Python

```python
#!/usr/bin/env python3
import depthai as dai
import time

# Create pipeline
with dai.Pipeline() as pipeline:
    # Create the nodes
    cam = pipeline.create(dai.node.Camera).build()
    benchmarkIn = pipeline.create(dai.node.BenchmarkIn)
    # benchmarkIn.setRunOnHost(True) # The node can also run on host and include the transfer limitation, default is False
    output = cam.requestFullResolutionOutput()
    output.link(benchmarkIn.input)

    pipeline.start()
    while pipeline.isRunning():
        time.sleep(1) # Let the logger print out the FPS
```

#### C++

```cpp
#include <atomic>
#include <chrono>
#include <csignal>
#include <depthai/depthai.hpp>
#include <thread>

std::atomic<bool> quitEvent(false);

void signalHandler(int) {
    quitEvent = true;
}

int main() {
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    // Create pipeline
    dai::Pipeline pipeline;

    // Create the nodes
    auto cam = pipeline.create<dai::node::Camera>()->build();
    auto benchmarkIn = pipeline.create<dai::node::BenchmarkIn>();
    // benchmarkIn->setRunOnHost(true); // The node can also run on host and include the transfer limitation, default is False
    auto* output = cam->requestFullResolutionOutput();
    output->link(benchmarkIn->input);

    pipeline.start();
    while(pipeline.isRunning() && !quitEvent) {
        std::this_thread::sleep_for(std::chrono::seconds(1));  // Let the logger print out the FPS
    }

    pipeline.stop();
    pipeline.wait();

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
