BenchmarkResult.fromBytes constructor
BenchmarkResult.fromBytes(
- Uint8List bytes
Implementation
factory BenchmarkResult.fromBytes(Uint8List bytes) {
var offset = 0;
final preambleBytes = bytes.buffer.asUint8List(0, _preambleBytes.length);
final preamble = ascii.decode(preambleBytes);
if (preamble != _preamble) {
throw LoadException(
"The file doesn't start with the correct preamble: $_preamble");
}
offset += preambleBytes.lengthInBytes;
final version = bytes.buffer.asUint16List(offset, 1).single;
if (version != currentFormatVersion) {
// TODO: Support loading old versions when we have such a thing.
throw LoadException(
'The file is saved in an unsupported version format: $version. '
"This program's supported version: $currentFormatVersion");
}
offset += Uint16List.bytesPerElement;
final type =
ascii.decode(bytes.buffer.asUint8List(offset, _typeStringLength));
offset += _typeStringLength;
// The data isn't 16-bit aligned, so we need to first create a copy
// of the list that is just bytes, and then look at its buffer
// as a 16-bit list.
final labelLength = Uint8List.fromList(
bytes.buffer.asUint8List(offset, Uint16List.bytesPerElement))
.buffer
.asUint16List()
.single;
offset += Uint16List.bytesPerElement;
final labelBytes =
Uint8List.fromList(bytes.buffer.asUint8List(offset, labelLength))
.buffer
.asUint16List();
offset += labelBytes.lengthInBytes;
final label = String.fromCharCodes(labelBytes);
final timestampMilliseconds = Uint8List.fromList(
bytes.buffer.asUint8List(offset, Uint64List.bytesPerElement))
.buffer
.asUint64List()
.single;
offset += Uint64List.bytesPerElement;
final timestamp =
DateTime.fromMillisecondsSinceEpoch(timestampMilliseconds, isUtc: true);
final seriesCount = Uint8List.fromList(
bytes.buffer.asUint8List(offset, Uint32List.bytesPerElement))
.buffer
.asUint32List()
.single;
offset += Uint32List.bytesPerElement;
final seriesLengthsBytes = Uint8List.fromList(bytes.buffer
.asUint8List(offset, seriesCount * Uint64List.bytesPerElement))
.buffer
.asUint64List();
offset += seriesLengthsBytes.lengthInBytes;
final series = List<MeasurementSeries>.generate(seriesCount, (index) {
final seriesBytes = Uint8List.fromList(
bytes.buffer.asUint8List(offset, seriesLengthsBytes[index]))
.buffer
.asUint8List();
offset += seriesBytes.lengthInBytes;
return MeasurementSeries.fromBytes(seriesBytes);
}, growable: false);
return BenchmarkResult(
label: label,
timestamp: timestamp,
series: series,
type: type,
);
}