mirror of
https://github.com/dart-lang/sdk
synced 2024-07-01 07:14:29 +00:00
[benchmarks/ffi] Cleanup benchmarks
* Added a name-filter to every benchmark. * Addressed lints. * We don't run the legacy copies anymore, so deleted dart2 dirs. Change-Id: I65705749c5c90aad77d1fb93ea4a0d4e8b656cd4 Cq-Include-Trybots: luci.dart.try:benchmark-linux-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/301522 Reviewed-by: Jonas Termansen <sortie@google.com> Commit-Queue: Daco Harkes <dacoharkes@google.com>
This commit is contained in:
parent
40d99bc981
commit
4af8469493
|
@ -39,11 +39,16 @@ class FromPointerInt8 extends BenchmarkBase {
|
|||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
void main(List<String> args) {
|
||||
final benchmarks = [
|
||||
() => FromPointerInt8(),
|
||||
FromPointerInt8.new,
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// Micro-benchmark for creating TypeData lists from Pointers.
|
||||
//
|
||||
// The FfiMemory benchmark tests accessing memory through TypedData.
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
//
|
||||
// Benchmark fixture.
|
||||
//
|
||||
|
||||
// Number of repeats: 1000
|
||||
const N = 1000;
|
||||
|
||||
class FromPointerInt8 extends BenchmarkBase {
|
||||
Pointer<Int8> pointer = nullptr;
|
||||
FromPointerInt8() : super('FfiAsTypedList.FromPointerInt8');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(1);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
for (var i = 0; i < N; i++) {
|
||||
pointer.asTypedList(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
final benchmarks = [
|
||||
() => FromPointerInt8(),
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
}
|
||||
}
|
|
@ -138,12 +138,17 @@ class DigestDartMemory extends BenchmarkBase {
|
|||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
void main(List<String> args) {
|
||||
final benchmarks = [
|
||||
() => DigestCMemory(),
|
||||
() => DigestDartMemory(),
|
||||
DigestCMemory.new,
|
||||
DigestDartMemory.new,
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,151 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// Macro-benchmark for ffi with boringssl.
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import 'digest.dart';
|
||||
import 'types.dart';
|
||||
|
||||
//
|
||||
// BoringSSL functions
|
||||
//
|
||||
|
||||
Uint8List inventData(int length) {
|
||||
final result = Uint8List(length);
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = i % 256;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Uint8List toUint8List(Pointer<Bytes> bytes, int length) {
|
||||
final result = Uint8List(length);
|
||||
final uint8bytes = bytes.asUint8Pointer();
|
||||
for (int i = 0; i < length; i++) {
|
||||
result[i] = uint8bytes[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void copyFromUint8ListToTarget(Uint8List source, Pointer<Data> target) {
|
||||
final int length = source.length;
|
||||
final uint8target = target.asUint8Pointer();
|
||||
for (int i = 0; i < length; i++) {
|
||||
uint8target[i] = source[i];
|
||||
}
|
||||
}
|
||||
|
||||
String hash(Pointer<Data> data, int length, Pointer<EVP_MD> hashAlgorithm) {
|
||||
final context = EVP_MD_CTX_new();
|
||||
EVP_DigestInit(context, hashAlgorithm);
|
||||
EVP_DigestUpdate(context, data, length);
|
||||
final int resultSize = EVP_MD_CTX_size(context);
|
||||
final Pointer<Bytes> result = calloc<Uint8>(resultSize).cast();
|
||||
EVP_DigestFinal(context, result, nullptr);
|
||||
EVP_MD_CTX_free(context);
|
||||
final String hash = base64Encode(toUint8List(result, resultSize));
|
||||
calloc.free(result);
|
||||
return hash;
|
||||
}
|
||||
|
||||
//
|
||||
// Benchmark fixtures.
|
||||
//
|
||||
|
||||
// Number of repeats: 1 && Length in bytes: 10000000
|
||||
// * CPU: Intel(R) Xeon(R) Gold 6154
|
||||
// * Architecture: x64
|
||||
// * 23000 - 52000000 us (without optimizations)
|
||||
// * 23000 - 30000 us (with optimizations)
|
||||
const int L = 1000; // Length of data in bytes.
|
||||
|
||||
final hashAlgorithm = EVP_sha512();
|
||||
|
||||
// Hash of generated data of `L` bytes with `hashAlgorithm`.
|
||||
const String expectedHash =
|
||||
'bNLtqb+cBZcSkCmwBUuB5DP2uLe0madetwXv10usGUFJg1sdGhTEi+aW5NWIRW1RKiLq56obV74rVurn014Iyw==';
|
||||
|
||||
/// This benchmark runs a digest algorithm on data residing in C memory.
|
||||
///
|
||||
/// This benchmark is intended as macro benchmark with a realistic workload.
|
||||
class DigestCMemory extends BenchmarkBase {
|
||||
DigestCMemory() : super('FfiBoringssl.DigestCMemory');
|
||||
|
||||
Pointer<Data> data; // Data in C memory that we want to digest.
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
data = calloc<Uint8>(L).cast();
|
||||
copyFromUint8ListToTarget(inventData(L), data);
|
||||
hash(data, L, hashAlgorithm);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
calloc.free(data);
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final String result = hash(data, L, hashAlgorithm);
|
||||
if (result != expectedHash) {
|
||||
throw Exception('$name: Unexpected result: $result');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// This benchmark runs a digest algorithm on data residing in Dart memory.
|
||||
///
|
||||
/// This benchmark is intended as macro benchmark with a realistic workload.
|
||||
class DigestDartMemory extends BenchmarkBase {
|
||||
DigestDartMemory() : super('FfiBoringssl.DigestDartMemory');
|
||||
|
||||
Uint8List data; // Data in C memory that we want to digest.
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
data = inventData(L);
|
||||
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
|
||||
copyFromUint8ListToTarget(data, dataInC);
|
||||
hash(dataInC, L, hashAlgorithm);
|
||||
calloc.free(dataInC);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final Pointer<Data> dataInC = calloc<Uint8>(L).cast();
|
||||
copyFromUint8ListToTarget(data, dataInC);
|
||||
final String result = hash(dataInC, L, hashAlgorithm);
|
||||
calloc.free(dataInC);
|
||||
if (result != expectedHash) {
|
||||
throw Exception('$name: Unexpected result: $result');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
final benchmarks = [
|
||||
() => DigestCMemory(),
|
||||
() => DigestDartMemory(),
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
}
|
||||
}
|
|
@ -1,101 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
|
||||
import 'dlopen_helper.dart';
|
||||
import 'types.dart';
|
||||
|
||||
// See:
|
||||
// https://commondatastorage.googleapis.com/chromium-boringssl-docs/digest.h.html
|
||||
|
||||
DynamicLibrary openSsl() {
|
||||
// Force load crypto.
|
||||
dlopenPlatformSpecific('crypto',
|
||||
path: Platform.script.resolve('../native/out/').path);
|
||||
final ssl = dlopenPlatformSpecific('ssl',
|
||||
path: Platform.script.resolve('../native/out/').path);
|
||||
return ssl;
|
||||
}
|
||||
|
||||
final DynamicLibrary ssl = openSsl();
|
||||
|
||||
/// The following functions return EVP_MD objects that implement the named
|
||||
/// hash function.
|
||||
///
|
||||
/// ```c
|
||||
/// const EVP_MD *EVP_sha512(void);
|
||||
/// ```
|
||||
final Pointer<EVP_MD> Function() EVP_sha512 =
|
||||
ssl.lookupFunction<Pointer<EVP_MD> Function(), Pointer<EVP_MD> Function()>(
|
||||
'EVP_sha512');
|
||||
|
||||
/// EVP_MD_CTX_new allocates and initialises a fresh EVP_MD_CTX and returns it,
|
||||
/// or NULL on allocation failure. The caller must use EVP_MD_CTX_free to
|
||||
/// release the resulting object.
|
||||
///
|
||||
/// ```c
|
||||
/// EVP_MD_CTX *EVP_MD_CTX_new(void);
|
||||
/// ```
|
||||
final Pointer<EVP_MD_CTX> Function() EVP_MD_CTX_new = ssl.lookupFunction<
|
||||
Pointer<EVP_MD_CTX> Function(),
|
||||
Pointer<EVP_MD_CTX> Function()>('EVP_MD_CTX_new');
|
||||
|
||||
/// EVP_MD_CTX_free calls EVP_MD_CTX_cleanup and then frees ctx itself.
|
||||
///
|
||||
/// ```c
|
||||
/// void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
|
||||
/// ```
|
||||
final void Function(Pointer<EVP_MD_CTX>) EVP_MD_CTX_free = ssl.lookupFunction<
|
||||
Void Function(Pointer<EVP_MD_CTX>),
|
||||
void Function(Pointer<EVP_MD_CTX>)>('EVP_MD_CTX_free');
|
||||
|
||||
/// EVP_DigestInit acts like EVP_DigestInit_ex except that ctx is initialised
|
||||
/// before use.
|
||||
///
|
||||
/// ```c
|
||||
/// int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
|
||||
/// ```
|
||||
final int Function(Pointer<EVP_MD_CTX>, Pointer<EVP_MD>) EVP_DigestInit =
|
||||
ssl.lookupFunction<Int32 Function(Pointer<EVP_MD_CTX>, Pointer<EVP_MD>),
|
||||
int Function(Pointer<EVP_MD_CTX>, Pointer<EVP_MD>)>('EVP_DigestInit');
|
||||
|
||||
/// EVP_DigestUpdate hashes len bytes from data into the hashing operation
|
||||
/// in ctx. It returns one.
|
||||
///
|
||||
/// ```c
|
||||
/// int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
|
||||
/// size_t len);
|
||||
/// ```
|
||||
final int Function(Pointer<EVP_MD_CTX>, Pointer<Data>, int) EVP_DigestUpdate =
|
||||
ssl.lookupFunction<
|
||||
Int32 Function(Pointer<EVP_MD_CTX>, Pointer<Data>, IntPtr),
|
||||
int Function(
|
||||
Pointer<EVP_MD_CTX>, Pointer<Data>, int)>('EVP_DigestUpdate');
|
||||
|
||||
/// EVP_DigestFinal acts like EVP_DigestFinal_ex except that EVP_MD_CTX_cleanup
|
||||
/// is called on ctx before returning.
|
||||
///
|
||||
/// ```c
|
||||
/// int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out,
|
||||
/// unsigned int *out_size);
|
||||
/// ```
|
||||
final int Function(Pointer<EVP_MD_CTX>, Pointer<Bytes>, Pointer<Uint32>)
|
||||
EVP_DigestFinal = ssl.lookupFunction<
|
||||
Int32 Function(Pointer<EVP_MD_CTX>, Pointer<Bytes>, Pointer<Uint32>),
|
||||
int Function(Pointer<EVP_MD_CTX>, Pointer<Bytes>,
|
||||
Pointer<Uint32>)>('EVP_DigestFinal');
|
||||
|
||||
/// EVP_MD_CTX_size returns the digest size of ctx, in bytes. It will crash if
|
||||
/// a digest hasn't been set on ctx.
|
||||
///
|
||||
/// ```c
|
||||
/// size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
|
||||
/// ```
|
||||
final int Function(Pointer<EVP_MD_CTX>) EVP_MD_CTX_size = ssl.lookupFunction<
|
||||
IntPtr Function(Pointer<EVP_MD_CTX>),
|
||||
int Function(Pointer<EVP_MD_CTX>)>('EVP_MD_CTX_size');
|
|
@ -1,63 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
|
||||
const arm = 'arm';
|
||||
const arm64 = 'arm64';
|
||||
const ia32 = 'ia32';
|
||||
const x64 = 'x64';
|
||||
|
||||
// https://stackoverflow.com/questions/45125516/possible-values-for-uname-m
|
||||
final _unames = {
|
||||
'arm': arm,
|
||||
'aarch64_be': arm64,
|
||||
'aarch64': arm64,
|
||||
'armv8b': arm64,
|
||||
'armv8l': arm64,
|
||||
'i386': ia32,
|
||||
'i686': ia32,
|
||||
'x86_64': x64,
|
||||
};
|
||||
|
||||
String _checkRunningMode(String architecture) {
|
||||
// Check if we're running in 32bit mode.
|
||||
final int pointerSize = sizeOf<IntPtr>();
|
||||
if (pointerSize == 4 && architecture == x64) return ia32;
|
||||
if (pointerSize == 4 && architecture == arm64) return arm;
|
||||
|
||||
return architecture;
|
||||
}
|
||||
|
||||
String _architecture() {
|
||||
final String uname = Process.runSync('uname', ['-m']).stdout.trim();
|
||||
final String architecture = _unames[uname];
|
||||
if (architecture == null) {
|
||||
throw Exception('Unrecognized architecture: "$uname"');
|
||||
}
|
||||
|
||||
// Check if we're running in 32bit mode.
|
||||
return _checkRunningMode(architecture);
|
||||
}
|
||||
|
||||
String _platformPath(String name, {String path = ''}) {
|
||||
if (Platform.isMacOS || Platform.isIOS) {
|
||||
return '${path}mac/${_architecture()}/lib$name.dylib';
|
||||
}
|
||||
|
||||
if (Platform.isWindows) {
|
||||
return '${path}win/${_checkRunningMode(x64)}/$name.dll';
|
||||
}
|
||||
|
||||
// Unknown platforms default to Unix implementation.
|
||||
return '${path}linux/${_architecture()}/lib$name.so';
|
||||
}
|
||||
|
||||
DynamicLibrary dlopenPlatformSpecific(String name, {String path}) {
|
||||
final String fullPath = _platformPath(name, path: path);
|
||||
return DynamicLibrary.open(fullPath);
|
||||
}
|
|
@ -1,33 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// ignore_for_file: camel_case_types
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:ffi';
|
||||
|
||||
/// digest algorithm.
|
||||
class EVP_MD extends Opaque {}
|
||||
|
||||
/// digest context.
|
||||
class EVP_MD_CTX extends Opaque {}
|
||||
|
||||
/// Type for `void*` used to represent opaque data.
|
||||
class Data extends Opaque {
|
||||
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
|
||||
}
|
||||
|
||||
extension DataPointerAsUint8Pointer on Pointer<Data> {
|
||||
Pointer<Uint8> asUint8Pointer() => cast();
|
||||
}
|
||||
|
||||
/// Type for `uint8_t*` used to represent byte data.
|
||||
class Bytes extends Opaque {
|
||||
static Pointer<Data> fromUint8Pointer(Pointer<Uint8> p) => p.cast<Data>();
|
||||
}
|
||||
|
||||
extension BytesPointerAsUint8Pointer on Pointer<Bytes> {
|
||||
Pointer<Uint8> asUint8Pointer() => cast();
|
||||
}
|
|
@ -82,7 +82,7 @@ class Int64Mintx01 extends FfiBenchmarkBase {
|
|||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
void main(List<String> args) {
|
||||
// Force loading the dylib with RLTD_GLOBAL so that the
|
||||
// Native benchmarks below can do process lookup.
|
||||
dlopenGlobalPlatformSpecific('native_functions',
|
||||
|
@ -147,7 +147,12 @@ void main() {
|
|||
Handlex20.new,
|
||||
Handlex20Native.new,
|
||||
];
|
||||
for (var benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@
|
|||
import 'dart:ffi';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
//
|
||||
// Pointer store.
|
||||
|
@ -574,26 +574,31 @@ class PointerInt64Mint extends BenchmarkBase {
|
|||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
void main(List<String> args) {
|
||||
final benchmarks = [
|
||||
() => PointerInt8(),
|
||||
() => PointerInt8TypedDataNew(),
|
||||
() => PointerInt8TypedDataReuse(),
|
||||
() => PointerUint8(),
|
||||
() => PointerInt16(),
|
||||
() => PointerUint16(),
|
||||
() => PointerInt32(),
|
||||
() => PointerUint32(),
|
||||
() => PointerUint32Unaligned(),
|
||||
() => PointerInt64(),
|
||||
() => PointerInt64Mint(),
|
||||
() => PointerUint64(),
|
||||
() => PointerUintPtr(),
|
||||
() => PointerFloat(),
|
||||
() => PointerDouble(),
|
||||
() => PointerPointer(),
|
||||
PointerInt8.new,
|
||||
PointerInt8TypedDataNew.new,
|
||||
PointerInt8TypedDataReuse.new,
|
||||
PointerUint8.new,
|
||||
PointerInt16.new,
|
||||
PointerUint16.new,
|
||||
PointerInt32.new,
|
||||
PointerUint32.new,
|
||||
PointerUint32Unaligned.new,
|
||||
PointerInt64.new,
|
||||
PointerInt64Mint.new,
|
||||
PointerUint64.new,
|
||||
PointerUintPtr.new,
|
||||
PointerFloat.new,
|
||||
PointerDouble.new,
|
||||
PointerPointer.new,
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,599 +0,0 @@
|
|||
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// TODO(37581): Generate this file.
|
||||
|
||||
// Micro-benchmarks for ffi memory stores and loads.
|
||||
//
|
||||
// These micro benchmarks track the speed of reading and writing C memory from
|
||||
// Dart with a specific marshalling and unmarshalling of data.
|
||||
|
||||
// @dart=2.9
|
||||
|
||||
import 'dart:ffi';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
|
||||
//
|
||||
// Pointer store.
|
||||
//
|
||||
|
||||
void doStoreInt8(Pointer<Int8> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreInt8TypedData(Int8List typedData, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
typedData[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreUint8(Pointer<Uint8> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreInt16(Pointer<Int16> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreUint16(Pointer<Uint16> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreInt32(Pointer<Int32> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreUint32(Pointer<Uint32> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreInt64(Pointer<Int64> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreUint64(Pointer<Uint64> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreUintPtr(Pointer<UintPtr> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreFloat(Pointer<Float> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreDouble(Pointer<Double> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
void doStorePointer(
|
||||
Pointer<Pointer<Int8>> pointer, int length, Pointer<Int8> data) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = data;
|
||||
}
|
||||
}
|
||||
|
||||
void doStoreInt64Mint(Pointer<Int64> pointer, int length) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
pointer[i] = 0x7FFFFFFFFFFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Pointer load.
|
||||
//
|
||||
|
||||
int doLoadInt8(Pointer<Int8> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadInt8TypedData(Int8List typedData, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += typedData[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadUint8(Pointer<Uint8> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadInt16(Pointer<Int16> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadUint16(Pointer<Uint16> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadInt32(Pointer<Int32> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadUint32(Pointer<Uint32> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadInt64(Pointer<Int64> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadUint64(Pointer<Uint64> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
int doLoadUintPtr(Pointer<UintPtr> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
double doLoadFloat(Pointer<Float> pointer, int length) {
|
||||
double x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
double doLoadDouble(Pointer<Double> pointer, int length) {
|
||||
double x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
// Aggregates pointers through aggregating their addresses.
|
||||
int doLoadPointer(Pointer<Pointer<Int8>> pointer, int length) {
|
||||
Pointer<Int8> x;
|
||||
int address_xor = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x = pointer[i];
|
||||
address_xor ^= x.address;
|
||||
}
|
||||
return address_xor;
|
||||
}
|
||||
|
||||
int doLoadInt64Mint(Pointer<Int64> pointer, int length) {
|
||||
int x = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
x += pointer[i];
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
//
|
||||
// Benchmark fixtures.
|
||||
//
|
||||
|
||||
// Number of repeats: 1000
|
||||
// * CPU: Intel(R) Xeon(R) Gold 6154
|
||||
// * Architecture: x64
|
||||
// * 48000 - 125000 us (without optimizations)
|
||||
// * 14 - ??? us (expected with optimizations, on par with typed data)
|
||||
const N = 1000;
|
||||
|
||||
class PointerInt8 extends BenchmarkBase {
|
||||
Pointer<Int8> pointer;
|
||||
PointerInt8() : super('FfiMemory.PointerInt8');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt8(pointer, N);
|
||||
final int x = doLoadInt8(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt8TypedDataNew extends BenchmarkBase {
|
||||
Pointer<Int8> pointer;
|
||||
PointerInt8TypedDataNew() : super('FfiMemory.PointerInt8TypedDataNew');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
pointer = calloc(N);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
calloc.free(pointer);
|
||||
pointer = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
final typedData = pointer.asTypedList(N);
|
||||
doStoreInt8TypedData(typedData, N);
|
||||
final int x = doLoadInt8TypedData(typedData, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x, expected $N');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt8TypedDataReuse extends BenchmarkBase {
|
||||
Pointer<Int8> pointer;
|
||||
Int8List typedData;
|
||||
PointerInt8TypedDataReuse() : super('FfiMemory.PointerInt8TypedDataReuse');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
pointer = calloc(N);
|
||||
typedData = pointer.asTypedList(N);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
calloc.free(pointer);
|
||||
pointer = null;
|
||||
typedData = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt8TypedData(typedData, N);
|
||||
final int x = doLoadInt8TypedData(typedData, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x, expected $N');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUint8 extends BenchmarkBase {
|
||||
Pointer<Uint8> pointer;
|
||||
PointerUint8() : super('FfiMemory.PointerUint8');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUint8(pointer, N);
|
||||
final int x = doLoadUint8(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt16 extends BenchmarkBase {
|
||||
Pointer<Int16> pointer;
|
||||
PointerInt16() : super('FfiMemory.PointerInt16');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt16(pointer, N);
|
||||
final int x = doLoadInt16(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUint16 extends BenchmarkBase {
|
||||
Pointer<Uint16> pointer;
|
||||
PointerUint16() : super('FfiMemory.PointerUint16');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUint16(pointer, N);
|
||||
final int x = doLoadUint16(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt32 extends BenchmarkBase {
|
||||
Pointer<Int32> pointer;
|
||||
PointerInt32() : super('FfiMemory.PointerInt32');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt32(pointer, N);
|
||||
final int x = doLoadInt32(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUint32 extends BenchmarkBase {
|
||||
Pointer<Uint32> pointer;
|
||||
PointerUint32() : super('FfiMemory.PointerUint32');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUint32(pointer, N);
|
||||
final int x = doLoadUint32(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUint32Unaligned extends BenchmarkBase {
|
||||
Pointer<Uint32> pointer;
|
||||
Pointer<Uint32> unalignedPointer;
|
||||
PointerUint32Unaligned() : super('FfiMemory.PointerUint32Unaligned');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
pointer = calloc(N + 1);
|
||||
unalignedPointer = Pointer.fromAddress(pointer.address + 1);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUint32(unalignedPointer, N);
|
||||
final int x = doLoadUint32(unalignedPointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt64 extends BenchmarkBase {
|
||||
Pointer<Int64> pointer;
|
||||
PointerInt64() : super('FfiMemory.PointerInt64');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt64(pointer, N);
|
||||
final int x = doLoadInt64(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUint64 extends BenchmarkBase {
|
||||
Pointer<Uint64> pointer;
|
||||
PointerUint64() : super('FfiMemory.PointerUint64');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUint64(pointer, N);
|
||||
final int x = doLoadUint64(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerUintPtr extends BenchmarkBase {
|
||||
Pointer<UintPtr> pointer;
|
||||
PointerUintPtr() : super('FfiMemory.PointerUintPtr');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreUintPtr(pointer, N);
|
||||
final int x = doLoadUintPtr(pointer, N);
|
||||
if (x != N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerFloat extends BenchmarkBase {
|
||||
Pointer<Float> pointer;
|
||||
PointerFloat() : super('FfiMemory.PointerFloat');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreFloat(pointer, N);
|
||||
final double x = doLoadFloat(pointer, N);
|
||||
if (0.99 * N > x || x > 1.01 * N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerDouble extends BenchmarkBase {
|
||||
Pointer<Double> pointer;
|
||||
PointerDouble() : super('FfiMemory.PointerDouble');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreDouble(pointer, N);
|
||||
final double x = doLoadDouble(pointer, N);
|
||||
if (0.99 * N > x || x > 1.01 * N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerPointer extends BenchmarkBase {
|
||||
Pointer<Pointer<Int8>> pointer;
|
||||
Pointer<Int8> data;
|
||||
PointerPointer() : super('FfiMemory.PointerPointer');
|
||||
|
||||
@override
|
||||
void setup() {
|
||||
pointer = calloc(N);
|
||||
data = calloc();
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
calloc.free(pointer);
|
||||
calloc.free(data);
|
||||
}
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStorePointer(pointer, N, data);
|
||||
final int x = doLoadPointer(pointer, N);
|
||||
if (x != 0 || x == data.address) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class PointerInt64Mint extends BenchmarkBase {
|
||||
Pointer<Int64> pointer;
|
||||
PointerInt64Mint() : super('FfiMemory.PointerInt64Mint');
|
||||
|
||||
@override
|
||||
void setup() => pointer = calloc(N);
|
||||
@override
|
||||
void teardown() => calloc.free(pointer);
|
||||
|
||||
@override
|
||||
void run() {
|
||||
doStoreInt64Mint(pointer, N);
|
||||
final int x = doLoadInt64Mint(pointer, N);
|
||||
// Using overflow semantics in aggregation.
|
||||
if (x != -N) {
|
||||
throw Exception('$name: Unexpected result: $x');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
final benchmarks = [
|
||||
() => PointerInt8(),
|
||||
() => PointerInt8TypedDataNew(),
|
||||
() => PointerInt8TypedDataReuse(),
|
||||
() => PointerUint8(),
|
||||
() => PointerInt16(),
|
||||
() => PointerUint16(),
|
||||
() => PointerInt32(),
|
||||
() => PointerUint32(),
|
||||
() => PointerUint32Unaligned(),
|
||||
() => PointerInt64(),
|
||||
() => PointerInt64Mint(),
|
||||
() => PointerUint64(),
|
||||
() => PointerUintPtr(),
|
||||
() => PointerFloat(),
|
||||
() => PointerDouble(),
|
||||
() => PointerPointer(),
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
}
|
||||
}
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:benchmark_harness/benchmark_harness.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
//
|
||||
// Struct field store (plus Pointer elementAt and load).
|
||||
|
@ -68,12 +68,17 @@ class FieldLoadStore extends BenchmarkBase {
|
|||
// Main driver.
|
||||
//
|
||||
|
||||
void main() {
|
||||
void main(List<String> args) {
|
||||
final benchmarks = [
|
||||
() => FieldLoadStore(),
|
||||
FieldLoadStore.new,
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
benchmark().report();
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
import 'dart:ffi';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:args/args.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import 'benchmark_generated.dart';
|
||||
|
@ -84,26 +83,18 @@ abstract class StructCopyBenchmark {
|
|||
}
|
||||
|
||||
void main(List<String> args) {
|
||||
final argParser = ArgParser();
|
||||
argParser.addFlag('verbose', abbr: 'v');
|
||||
final argsParsed = argParser.parse(args);
|
||||
final verbose = argsParsed['verbose'] as bool;
|
||||
final rest = argsParsed.rest;
|
||||
String? filter;
|
||||
if (rest.isNotEmpty) {
|
||||
filter = rest.first;
|
||||
}
|
||||
|
||||
final benchmarks = [
|
||||
Copy1Bytes.new,
|
||||
Copy32Bytes.new,
|
||||
Copy1024Bytes.new,
|
||||
Copy32768Bytes.new,
|
||||
];
|
||||
for (final benchmark in benchmarks) {
|
||||
final b = benchmark();
|
||||
if (filter == null || b.name.contains(filter)) {
|
||||
b.report(verbose: verbose);
|
||||
|
||||
final filter = args.firstOrNull;
|
||||
for (var constructor in benchmarks) {
|
||||
final benchmark = constructor();
|
||||
if (filter == null || benchmark.name.contains(filter)) {
|
||||
benchmark.report();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user