|
| 1 | +// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// These micro benchmarks track the speed of reading and writing C memory from |
| 6 | +// Dart with a specific marshalling and unmarshalling of data. |
| 7 | + |
| 8 | +import 'dart:ffi'; |
| 9 | +import 'dart:io'; |
| 10 | + |
| 11 | +import 'package:args/args.dart'; |
| 12 | + |
| 13 | +import 'dlopen_helper.dart'; |
| 14 | + |
| 15 | +// The native library that holds all the native functions being called. |
| 16 | +DynamicLibrary ffiTestFunctions = dlopenPlatformSpecific( |
| 17 | + 'native_functions', |
| 18 | + path: Platform.script.resolve('../native/out/').path, |
| 19 | +); |
| 20 | + |
| 21 | +abstract class FfiCallbackBenchmark { |
| 22 | + final String name; |
| 23 | + |
| 24 | + FfiCallbackBenchmark(this.name); |
| 25 | + |
| 26 | + // Returns ns per callback. |
| 27 | + double measureFor(Duration duration) { |
| 28 | + const int batchSize = 100000; |
| 29 | + |
| 30 | + int numberOfCalls = 0; |
| 31 | + int totalMicroseconds = 0; |
| 32 | + |
| 33 | + final sw = Stopwatch()..start(); |
| 34 | + final durationInMicroseconds = duration.inMicroseconds; |
| 35 | + |
| 36 | + do { |
| 37 | + run(batchSize); |
| 38 | + numberOfCalls += batchSize; |
| 39 | + totalMicroseconds = sw.elapsedMicroseconds; |
| 40 | + } while (totalMicroseconds < durationInMicroseconds); |
| 41 | + |
| 42 | + final totalNanoSeconds = totalMicroseconds * 1000; |
| 43 | + return totalNanoSeconds / numberOfCalls; |
| 44 | + } |
| 45 | + |
| 46 | + // Runs warmup phase, runs benchmark and reports result. |
| 47 | + void report({bool verbose = false}) { |
| 48 | + // Warmup for 100 ms. |
| 49 | + measureFor(const Duration(milliseconds: 100)); |
| 50 | + |
| 51 | + // Run benchmark for 2 seconds. |
| 52 | + final double nsPerCall = measureFor(const Duration(seconds: 2)); |
| 53 | + |
| 54 | + // Report result. |
| 55 | + print('$name(RunTimeRaw): $nsPerCall ns.'); |
| 56 | + if (verbose) { |
| 57 | + final callsPerSecond = (1000 * 1000 * 1000 / nsPerCall).toInt(); |
| 58 | + print('$name: $callsPerSecond calls per second.'); |
| 59 | + } |
| 60 | + |
| 61 | + shutdown(); |
| 62 | + } |
| 63 | + |
| 64 | + void run(int batchSize); |
| 65 | + |
| 66 | + void shutdown(); |
| 67 | + |
| 68 | + void expectEquals(actual, expected) { |
| 69 | + if (actual != expected) { |
| 70 | + throw Exception('$name: Unexpected result: $actual, expected $expected'); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +final class Uint8x1 extends FfiCallbackBenchmark { |
| 76 | + Uint8x1() : super('FfiCallbackBenchmark.Uint8x1'); |
| 77 | + |
| 78 | + final function = ffiTestFunctions.lookupFunction< |
| 79 | + Void Function(Pointer<NativeFunction<Void Function(Uint8)>>, Uint32), |
| 80 | + void Function(Pointer<NativeFunction<Void Function(Uint8)>>, int) |
| 81 | + >('CallFunction1Uint8'); |
| 82 | + |
| 83 | + static int x = 0; |
| 84 | + |
| 85 | + static void callback(int value) { |
| 86 | + x += value; |
| 87 | + } |
| 88 | + |
| 89 | + static final nativeCallable = |
| 90 | + NativeCallable<Void Function(Uint8)>.isolateLocal(callback); |
| 91 | + |
| 92 | + static final pointer = nativeCallable.nativeFunction; |
| 93 | + |
| 94 | + @override |
| 95 | + void run(int batchSize) { |
| 96 | + x = 0; |
| 97 | + function(pointer, batchSize); |
| 98 | + expectEquals(x, batchSize); |
| 99 | + } |
| 100 | + |
| 101 | + @override |
| 102 | + void shutdown() { |
| 103 | + nativeCallable.close(); |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +final argParser = |
| 108 | + ArgParser()..addFlag( |
| 109 | + 'verbose', |
| 110 | + abbr: 'v', |
| 111 | + help: 'Verbose output', |
| 112 | + defaultsTo: false, |
| 113 | + ); |
| 114 | + |
| 115 | +void main(List<String> args) { |
| 116 | + final results = argParser.parse(args); |
| 117 | + final benchmarks = [Uint8x1.new]; |
| 118 | + |
| 119 | + final filter = results.rest.firstOrNull; |
| 120 | + for (var constructor in benchmarks) { |
| 121 | + final benchmark = constructor(); |
| 122 | + if (filter == null || benchmark.name.contains(filter)) { |
| 123 | + benchmark.report(verbose: results['verbose']); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments