diff --git a/example/benchmark.dart b/example/benchmark.dart index 8859fb2..0ea9c47 100644 --- a/example/benchmark.dart +++ b/example/benchmark.dart @@ -1,7 +1,7 @@ import "dart:convert"; +import "dart:io"; import "package:msgpack/msgpack.dart"; -import "dart:io"; const int TIMES = 10000; @@ -18,8 +18,7 @@ class Fraction { return new Fraction(numerator / cd, denominator / cd); } - Fraction cross(Fraction other) => - new Fraction(other.denominator * numerator, other.numerator * denominator); + Fraction cross(Fraction other) => new Fraction(other.denominator * numerator, other.numerator * denominator); @override String toString() => "${numerator}/${denominator}"; @@ -30,16 +29,12 @@ class Fraction { main(List args) async { print("Warming Up..."); for (var i = 0; i < TIMES; i++) { - var packed = pack({ - "hello": "world" - }); + var packed = pack({"hello": "world"}); - var json = JSON.encode({ - "hello": "world" - }); + var jsonValue = json.encode({"hello": "world"}); unpack(packed); - JSON.decode(json); + json.decode(jsonValue); } if (args.contains("--savings")) { @@ -48,7 +43,7 @@ main(List args) async { numbers.add(i); } - var jsonBytes = UTF8.encode(JSON.encode(numbers)).length; + var jsonBytes = utf8.encode(json.encode(numbers)).length; var msgpackBytes = pack(numbers).length; var fract = new Fraction(jsonBytes, msgpackBytes); fract = fract.reduce(); @@ -61,168 +56,52 @@ main(List args) async { var objects = { "One": 1, "Five Hundred Thousand": 500000, - "List of Small Integers": [ - 1, - 2, - 3 - ], - "Simple Map": { - "hello": "world" - }, + "List of Small Integers": [1, 2, 3], + "Simple Map": {"hello": "world"}, "5.02817472928": 5.02817472928, "Multiple Type Map": { "String": "Hello World", "Integer": 1, "Double": 2.0154, - "Array": const [ - 1, - 2, - 3, - "Hello" - ] + "Array": const [1, 2, 3, "Hello"] }, "Medium Data": { "/downstream/wemo/CoffeeMaker-1_0-221421S0000731/Brew_Age": [ - [ - 1440366101049, - -123881 - ], - [ - 1440366102047, - -123882 - ], - [ - 1440366103049, - -123883 - ], - [ - 1440366104046, - -123884 - ], - [ - 1440366105062, - -123885 - ], - [ - 1440366106050, - -123886 - ], - [ - 1440366107046, - -123887 - ], - [ - 1440366108045, - -123888 - ], - [ - 1440366109036, - -123889 - ], - [ - 1440366110048, - -123890 - ], - [ - 1440366111047, - -123891 - ], - [ - 1440366112037, - -123892 - ], - [ - 1440366113048, - -123893 - ], - [ - 1440366114048, - -123894 - ], - [ - 1440366115046, - -123895 - ], - [ - 1440366116044, - -123896 - ], - [ - 1440366117045, - -123897 - ], - [ - 1440366118049, - -123898 - ], - [ - 1440366119046, - -123899 - ], - [ - 1440366120042, - -123900 - ], - [ - 1440366121047, - -123901 - ], - [ - 1440366122048, - -123902 - ], - [ - 1440366123046, - -123903 - ], - [ - 1440366124055, - -123904 - ], - [ - 1440366126059, - -123906 - ], - [ - 1440366127054, - -123907 - ], - [ - 1440366128047, - -123908 - ], - [ - 1440366129051, - -123909 - ], - [ - 1440366130051, - -123910 - ], - [ - 1440366131048, - -123911 - ], - [ - 1440366132050, - -123912 - ], - [ - 1440366133032, - -123913 - ], - [ - 1440366134045, - -123914 - ], - [ - 1440366135050, - -123915 - ], - [ - 1440366136049, - -123916 - ] + [1440366101049, -123881], + [1440366102047, -123882], + [1440366103049, -123883], + [1440366104046, -123884], + [1440366105062, -123885], + [1440366106050, -123886], + [1440366107046, -123887], + [1440366108045, -123888], + [1440366109036, -123889], + [1440366110048, -123890], + [1440366111047, -123891], + [1440366112037, -123892], + [1440366113048, -123893], + [1440366114048, -123894], + [1440366115046, -123895], + [1440366116044, -123896], + [1440366117045, -123897], + [1440366118049, -123898], + [1440366119046, -123899], + [1440366120042, -123900], + [1440366121047, -123901], + [1440366122048, -123902], + [1440366123046, -123903], + [1440366124055, -123904], + [1440366126059, -123906], + [1440366127054, -123907], + [1440366128047, -123908], + [1440366129051, -123909], + [1440366130051, -123910], + [1440366131048, -123911], + [1440366132050, -123912], + [1440366133032, -123913], + [1440366134045, -123914], + [1440366135050, -123915], + [1440366136049, -123916] ] } }; @@ -249,7 +128,7 @@ main(List args) async { testObjectDecode(String desc, input) { print("${desc}:"); - var packedJson = JSON.encode(input); + var packedJson = json.encode(input); var packed = pack(input); var watch = new Stopwatch(); var times = []; @@ -274,7 +153,7 @@ testObjectDecode(String desc, input) { for (var i = 1; i <= TIMES; i++) { watch.reset(); watch.start(); - JSON.decode(packedJson); + json.decode(packedJson); watch.stop(); times.add(watch.elapsedMicroseconds); } @@ -320,12 +199,12 @@ testObjectEncode(String desc, input) { print(" Shortest Time: ${times.first} microseconds (${times.first / 1000}ms)"); print(" Size: ${size} bytes"); watch.reset(); - size = UTF8.encode(JSON.encode(input)).length; + size = utf8.encode(json.encode(input)).length; times.clear(); for (var i = 1; i <= TIMES; i++) { watch.reset(); watch.start(); - JSON.encode(input); + json.encode(input); watch.stop(); times.add(watch.elapsedMicroseconds); } diff --git a/pubspec.yaml b/pubspec.yaml index 16897b9..869322b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,9 @@ name: msgpack description: MsgPack for Dart -version: 0.9.0 +version: 1.0.0 authors: - Kenneth Endfinger - Dan Ellis homepage: https://github.com/DirectMyFile/msgpack.dart dev_dependencies: - test: "^0.12.0" + test: '^1.0.0' diff --git a/test/msgpack_test.dart b/test/msgpack_test.dart index af65bc4..0e21a36 100644 --- a/test/msgpack_test.dart +++ b/test/msgpack_test.dart @@ -1,6 +1,7 @@ import 'dart:typed_data'; -import 'package:test/test.dart'; + import 'package:msgpack/msgpack.dart'; +import 'package:test/test.dart'; var isString = predicate((e) => e is String, 'is a String'); var isInt = predicate((e) => e is int, 'is an int'); @@ -14,7 +15,7 @@ class TestMessage extends Message { TestMessage(this.a, this.b, this.c); - static TestMessage fromList(List f) => new TestMessage(f[0], f[1], f[2]); + static TestMessage fromList(List f) => new TestMessage(f[0], f[1], Map.castFrom(f[2])); List toList() => [a, b, c]; } @@ -27,8 +28,7 @@ class OuterMessage extends Message { OuterMessage(this.a, this.b, this.list, this.inner); - static OuterMessage fromList(List f) => - new OuterMessage(f[0], f[1], f[2], TestMessage.fromList(f[3])); + static OuterMessage fromList(List f) => new OuterMessage(f[0], f[1], List.castFrom(f[2]), TestMessage.fromList(f[3])); List toList() => [a, b, list, inner]; } @@ -65,56 +65,58 @@ void packDSA() { // Use http://kawanet.github.io/msgpack-lite/ to test decode // 81 A3 6D 73 67 D1 00 EB List testObjData = [0x81, 0xA3, 0x6D, 0x73, 0x67, 0xD1, 0x00, 0xEB]; - Object obj=unpack(testObjData); + Object obj = unpack(testObjData); expect(unpack(testObjData)["msg"], 235); } void packNegative1() { List encoded = pack(-24577); - expect(encoded, orderedEquals([0xd1,0x9f,0xff])); + expect(encoded, orderedEquals([0xd1, 0x9f, 0xff])); Object decoded = unpack(encoded); expect(decoded, -24577); } void packNegative2() { List encoded = pack(-245778641); - expect(encoded, orderedEquals([0xd2,0xf1,0x59,0xb7,0x2f])); + expect(encoded, orderedEquals([0xd2, 0xf1, 0x59, 0xb7, 0x2f])); Object decoded = unpack(encoded); expect(decoded, -245778641); } void packString22() { List encoded = pack("hello there, everyone!"); - expect(encoded, orderedEquals([ - 182, - 104, - 101, - 108, - 108, - 111, - 32, - 116, - 104, - 101, - 114, - 101, - 44, - 32, - 101, - 118, - 101, - 114, - 121, - 111, - 110, - 101, - 33 - ])); + expect( + encoded, + orderedEquals([ + 182, + 104, + 101, + 108, + 108, + 111, + 32, + 116, + 104, + 101, + 114, + 101, + 44, + 32, + 101, + 118, + 101, + 114, + 121, + 111, + 110, + 101, + 33 + ])); } void packString256() { List encoded = pack( - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); expect(encoded, hasLength(259)); expect(encoded.sublist(0, 3), orderedEquals([218, 1, 0])); expect(encoded.sublist(3, 259), everyElement(65)); @@ -122,54 +124,52 @@ void packString256() { void packStringArray() { List encoded = pack(["one", "two", "three"]); - expect(encoded, orderedEquals( - [147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101 - ])); + expect(encoded, orderedEquals([147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101])); } void packIntToStringMap() { List encoded = pack({1: "one", 2: "two"}); - expect(encoded, - orderedEquals([130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111])); + expect(encoded, orderedEquals([130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111])); } void packMessage() { Message message = new TestMessage(1, "one", {2: "two"}); List encoded = pack(message); - expect(encoded, - orderedEquals([147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111])); + expect(encoded, orderedEquals([147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111])); } void packNestedMessage() { Message inner = new TestMessage(1, "one", {2: "two"}); Message outer = new OuterMessage("three", true, [4, 5, 6], inner); List encoded = pack(outer); - expect(encoded, orderedEquals([ - 148, - 165, - 116, - 104, - 114, - 101, - 101, - 195, - 147, - 4, - 5, - 6, - 147, - 1, - 163, - 111, - 110, - 101, - 129, - 2, - 163, - 116, - 119, - 111 - ])); + expect( + encoded, + orderedEquals([ + 148, + 165, + 116, + 104, + 114, + 101, + 101, + 195, + 147, + 4, + 5, + 6, + 147, + 1, + 163, + 111, + 110, + 101, + 129, + 2, + 163, + 116, + 119, + 111 + ])); } // Test unpacking @@ -183,31 +183,8 @@ void unpackString5() { } void unpackString22() { - Uint8List data = new Uint8List.fromList([ - 182, - 104, - 101, - 108, - 108, - 111, - 32, - 116, - 104, - 101, - 114, - 101, - 44, - 32, - 101, - 118, - 101, - 114, - 121, - 111, - 110, - 101, - 33 - ]); + Uint8List data = new Uint8List.fromList( + [182, 104, 101, 108, 108, 111, 32, 116, 104, 101, 114, 101, 44, 32, 101, 118, 101, 114, 121, 111, 110, 101, 33]); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpack(); expect(value, isString); @@ -215,19 +192,18 @@ void unpackString22() { } void unpackString256() { - Uint8List data = new Uint8List.fromList( - [218, 1, 0]..addAll(new List.filled(256, 65))); + Uint8List data = new Uint8List.fromList([218, 1, 0]..addAll(new List.filled(256, 65))); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpack(); expect(value, isString); - expect(value, equals( - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); + expect( + value, + equals( + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")); } void unpackStringArray() { - Uint8List data = new Uint8List.fromList( - [147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101 - ]); + Uint8List data = new Uint8List.fromList([147, 163, 111, 110, 101, 163, 116, 119, 111, 165, 116, 104, 114, 101, 101]); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpack(); expect(value, isList); @@ -235,8 +211,7 @@ void unpackStringArray() { } void unpackIntToStringMap() { - Uint8List data = new Uint8List.fromList( - [130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111]); + Uint8List data = new Uint8List.fromList([130, 1, 163, 111, 110, 101, 2, 163, 116, 119, 111]); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpack(); expect(value, isMap); @@ -245,8 +220,7 @@ void unpackIntToStringMap() { } void unpackMessage() { - Uint8List data = new Uint8List.fromList( - [147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111]); + Uint8List data = new Uint8List.fromList([147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111]); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpackMessage(TestMessage.fromList); expect(value, predicate((x) => x is TestMessage)); @@ -256,32 +230,8 @@ void unpackMessage() { } void unpackNestedMessage() { - Uint8List data = new Uint8List.fromList([ - 148, - 165, - 116, - 104, - 114, - 101, - 101, - 195, - 147, - 4, - 5, - 6, - 147, - 1, - 163, - 111, - 110, - 101, - 129, - 2, - 163, - 116, - 119, - 111 - ]); + Uint8List data = new Uint8List.fromList( + [148, 165, 116, 104, 114, 101, 101, 195, 147, 4, 5, 6, 147, 1, 163, 111, 110, 101, 129, 2, 163, 116, 119, 111]); Unpacker unpacker = new Unpacker(data.buffer); var value = unpacker.unpackMessage(OuterMessage.fromList); expect(value, predicate((x) => x is OuterMessage));