Skip to main content

High-performance InfluxDB query interface for Python

Project description

InfluxDB Rust Proof of Concept

This is a performance comparison POC to test if Rust can outperform Python for InfluxDB queries.

Objective

Replicate the Python get_influx_data_async function in Rust to measure performance improvements, especially in:

  • JSON serialization/deserialization
  • Query execution overhead
  • Memory usage

Setup

1. Install Rust (if not already installed)

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

2. Build the project

cd influx-rust
cargo build --release

The --release flag enables optimizations for accurate performance testing.

Usage

Method 1: Using environment variables

# Set environment variables
export INFLUXDB_URL="https://your-influxdb-url.com"
export INFLUXDB_TOKEN="your-token-here"
export INFLUXDB_ORG="your-org"

# Run with a query
cargo run --release 'from(bucket: "your-bucket") |> range(start: -1h) |> limit(n: 100)'

Method 2: Using command-line arguments

cargo run --release 'from(bucket: "your-bucket") |> range(start: -1h)' \
  --url https://your-influxdb-url.com \
  --token your-token-here \
  --org your-org

Example with real query

cargo run --release \
  'from(bucket: "agua_mar")
   |> range(start: -24h)
   |> filter(fn: (r) => r._measurement == "oxygen")
   |> filter(fn: (r) => r.centro == "Centro1")' \
  --url $INFLUXDB_URL \
  --token $INFLUXDB_TOKEN \
  --org $INFLUXDB_ORG

Performance Output

The program outputs timing information similar to the Python version:

[PERF][InfluxDB] Client initialization: 0.0012s
[PERF][InfluxDB] Query execution: 0.1234s
[PERF][InfluxDB] Process records: 0.0045s
[PERF][InfluxDB] TOTAL InfluxDB operation: 0.1291s
[PERF][InfluxDB] Records returned: 1000

Comparison with Python

To compare performance:

Python version timing:

# From msw-agua-mar/src/app/database/influxdb.py
# Example output:
# [PERF][InfluxDB] Client initialization: 0.0234s
# [PERF][InfluxDB] Query execution: 0.1567s
# [PERF][InfluxDB] JSON serialization: 0.0456s
# [PERF][InfluxDB] JSON deserialization: 0.0389s
# [PERF][InfluxDB] Process records: 0.0123s
# [PERF][InfluxDB] TOTAL InfluxDB operation: 0.2769s

Expected improvements in Rust:

  • No JSON serialization/deserialization overhead (Python does it twice!)
  • Faster record processing (no Python interpreter overhead)
  • Lower memory usage (no intermediate JSON strings)
  • Better async performance (Tokio vs asyncio)

Next Steps (if successful)

If Rust shows significant performance improvements:

  1. Refactor as library: Extract get_influx_data_async as a standalone function
  2. Add PyO3 bindings: Create Python bindings using PyO3
  3. Package as Python module: Make it installable via pip
  4. Replace Python implementation: Use the Rust implementation in existing services

PyO3 Integration Example

use pyo3::prelude::*;

#[pyfunction]
fn get_influx_data_async_rust(
    url: String,
    token: String,
    org: String,
    query: String,
) -> PyResult<Vec<HashMap<String, String>>> {
    // Call the Rust implementation
    // Return results to Python
}

#[pymodule]
fn influx_rust(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(get_influx_data_async_rust, m)?)?;
    Ok(())
}

Dependencies

  • influxdb2 - Official InfluxDB 2.x Rust client
  • tokio - Async runtime
  • serde/serde_json - JSON serialization
  • anyhow - Error handling

Testing

Quick Test

Run with a sample query to test connectivity:

cargo run --release 'buckets()' --url $INFLUXDB_URL --token $INFLUXDB_TOKEN --org $INFLUXDB_ORG

Production Query Test ⭐ RECOMMENDED

Test with the real production query (oxygen levels across 40+ sources):

./test_real_query.sh

This will measure performance on a complex query with:

  • Multiple data sources (40+ sources)
  • Aggregations (first, min, mean)
  • Joins
  • Time window aggregation (30m)

Compare with Python

To benchmark against the Python implementation:

./compare_performance.sh '<your_query>' ../msw-agua-mar

Troubleshooting

SSL/TLS errors

If you encounter SSL errors, the influxdb2 crate should handle SSL verification automatically. Make sure your InfluxDB instance has a valid certificate.

Connection timeout

The client has no explicit timeout in this POC. For production, you may want to add timeout configuration.

Query syntax errors

Make sure your Flux query is valid. Test it in the InfluxDB UI first.

License

Internal use for AquaChile performance testing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

influx_rust-0.1.0.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

influx_rust-0.1.0-cp38-abi3-macosx_11_0_arm64.whl (1.6 MB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file influx_rust-0.1.0.tar.gz.

File metadata

  • Download URL: influx_rust-0.1.0.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for influx_rust-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e4d8893e7f6a7ccbe59845195e4ab4c5448c7f54eee18b93d1ce6ff7454954e
MD5 25d6e172238ec2ea8b9cc5e3aa8a8743
BLAKE2b-256 8769db7b1347cdee315e2daa926d8504a7eaed1847d41b9bcb4cb96af18296ee

See more details on using hashes here.

File details

Details for the file influx_rust-0.1.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for influx_rust-0.1.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b1db89792377622a7800b27a32563855dcd375be0be24f4be0fc3afbfacde833
MD5 4c8c1ed29d4a4e50184848ce536299b7
BLAKE2b-256 2443a124d9877ecd9f54b94242813d499051c9fb9274c49018aa429d429f9752

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page