Created
January 12, 2026 21:34
-
-
Save FranckPachot/f8350ff0d0a9a5ffc20b548ff6c62fc6 to your computer and use it in GitHub Desktop.
Compare time to encode and decode BSON and OSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import bson | |
| import oracledb | |
| def generate_large_document(num_fields=1000, field_length=100): | |
| """Generate a document with num_fields, each containing a string of length field_length.""" | |
| long_str = "a" * field_length | |
| return {f"field_{i+1}": long_str for i in range(num_fields)} | |
| def benchmark_encode_decode(document, iterations=1000): | |
| """Benchmark BSON and OSON encode/decode in the same loop.""" | |
| # Look like we can decode/encode only when connected to a database | |
| oracledb.init_oracle_client(config_dir="/home/opc/My_Wallet") | |
| connection = oracledb.connect( | |
| user="demo", | |
| password="ChangeMe", | |
| dsn="adb_tp" | |
| ) | |
| # Determine source sizes for info | |
| bson_data_sample = bson.encode(document) | |
| bson_size = len(bson_data_sample) # bytes | |
| oson_data_sample = connection.encode_oson(document) | |
| oson_size = len(oson_data_sample) # bytes | |
| results = { | |
| 'bson_encode_time': 0.0, | |
| 'bson_decode_time': 0.0, | |
| 'oson_encode_time': 0.0, | |
| 'oson_decode_time': 0.0, | |
| 'bson_size': bson_size, | |
| 'oson_size': oson_size, | |
| 'field_count': len(document) | |
| } | |
| # Run all in one loop (fair comparison) | |
| bson_encode_total = 0.0 | |
| bson_decode_total = 0.0 | |
| oson_encode_total = 0.0 | |
| oson_decode_total = 0.0 | |
| for _ in range(iterations): | |
| # BSON encode | |
| start = time.perf_counter() | |
| bson_data = bson.encode(document) | |
| bson_encode_total += (time.perf_counter() - start) | |
| # BSON decode | |
| start = time.perf_counter() | |
| _ = bson.decode(bson_data) | |
| bson_decode_total += (time.perf_counter() - start) | |
| # OSON encode | |
| start = time.perf_counter() | |
| oson_data = connection.encode_oson(document) | |
| oson_encode_total += (time.perf_counter() - start) | |
| # OSON decode | |
| start = time.perf_counter() | |
| _ = connection.decode_oson(oson_data) | |
| oson_decode_total += (time.perf_counter() - start) | |
| connection.close() | |
| # Assign total times | |
| results['bson_encode_time'] = bson_encode_total | |
| results['bson_decode_time'] = bson_decode_total | |
| results['oson_encode_time'] = oson_encode_total | |
| results['oson_decode_time'] = oson_decode_total | |
| return results | |
| def run_comparison(): | |
| # Build large document with 1000 fields x 100 chars each | |
| document = generate_large_document(1000, 100) | |
| iterations = 10000 # Adjust iteration count for runtime | |
| results = benchmark_encode_decode(document, iterations) | |
| # Display metadata | |
| print("=" * 70) | |
| print(f"Benchmark: {results['field_count']} fields x 100 chars") | |
| print(f"BSON size: {results['bson_size']} bytes | OSON size: {results['oson_size']} bytes") | |
| print(f"Iterations: {iterations}") | |
| print("=" * 70) | |
| # Display results | |
| print(f"{'Operation':<15} {'BSON (s)':<12} {'OSON (s)':<12} {'Ratio (OSON/BSON)':<18}") | |
| print("-" * 70) | |
| for op_pair in [ | |
| ('Encode', 'bson_encode_time', 'oson_encode_time'), | |
| ('Decode', 'bson_decode_time', 'oson_decode_time') | |
| ]: | |
| label, bson_key, oson_key = op_pair | |
| bson_time = results[bson_key] | |
| oson_time = results[oson_key] | |
| ratio = oson_time / bson_time if bson_time > 0 else 0 | |
| print(f"{label:<15} {bson_time:<12.4f} {oson_time:<12.4f} {ratio:<18.2f}") | |
| if __name__ == "__main__": | |
| run_comparison() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment