Skip to content

Commit f9bae1b

Browse files
committed
test(JS): add xlang serialization tests for strings, lists, and maps
1 parent e62df66 commit f9bae1b

67 files changed

Lines changed: 3588 additions & 2653 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
/*
2-
* Licensed to the Apache Software Foundation (ASF) under one
3-
* or more contributor license agreements. See the NOTICE file
4-
* distributed with this work for additional information
5-
* regarding copyright ownership. The ASF licenses this file
6-
* to you under the Apache License, Version 2.0 (the
7-
* "License"); you may not use this file except in compliance
8-
* with the License. You may obtain a copy of the License at
9-
*
10-
* http://www.apache.org/licenses/LICENSE-2.0
11-
*
12-
* Unless required by applicable law or agreed to in writing,
13-
* software distributed under the License is distributed on an
14-
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15-
* KIND, either express or implied. See the License for the
16-
* specific language governing permissions and limitations
17-
* under the License.
18-
*/
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
1917

2018
import 'package:analyzer/dart/constant/value.dart';
2119
import 'package:analyzer/dart/element/element.dart';
@@ -51,31 +49,18 @@ class KeyAnnotationAnalyzer {
5149
);
5250
}
5351
getMeta = true;
54-
// Currently only handles ForyKey
5552
}
5653
}
57-
// If there is no annotation, both includeFromFory and includeToFory default to true.
5854
bool includeFromFory = true;
5955
bool includeToFory = true;
6056
if (getMeta && anno != null){
6157
includeFromFory = anno.getField("includeFromFory")!.toBoolValue()!;
6258
includeToFory = anno.getField("includeToFory")!.toBoolValue()!;
63-
// serializeToVar = anno.getField("serializeTo")?.variable;
64-
// deserializeFromVar = anno.getField("deserializeFrom")?.variable;
65-
// if (serializeToVar != null){
66-
// serializeTo = ForyType.fromString(serializeToVar.name)!;
67-
// }
68-
// if (deserializeFromVar != null){
69-
// deserializeFrom = ForyType.fromString(deserializeFromVar.name)!;
70-
// }
71-
// targetType = (anno.getField("targetType")!.variable.enclosingElement3 as EnumElement).fields[1].;
7259
}
7360
ForyKey foryKey = ForyKey(
74-
// serializeTo: serializeTo,
75-
// deserializeFrom: deserializeFrom,
7661
includeFromFory: includeFromFory,
7762
includeToFory: includeToFory,
7863
);
7964
return foryKey;
8065
}
81-
}
66+
}

javascript/.eslintrc.cjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,13 @@ module.exports = {
3838
"@typescript-eslint/no-explicit-any": "off",
3939
"@typescript-eslint/no-var-requires": "off",
4040
},
41-
ignorePatterns: ["test/*", "dist/*", "*.js", "murmurHash3.ts", "packages/**/dist/*", "packages/**/build/*", "**/typeInfo.ts"],
41+
ignorePatterns: [
42+
"test/*",
43+
"dist/*",
44+
"*.js",
45+
"murmurHash3.ts",
46+
"packages/**/dist/*",
47+
"packages/**/build/*",
48+
"**/typeInfo.ts",
49+
],
4250
};

javascript/benchmark/index.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919

2020
const Fory = require("@apache-fory/fory");
2121
const utils = require("@apache-fory/fory/dist/lib/util");
22-
const hps = require('@apache-fory/hps').default;
23-
const fory = new Fory.default({ hps, refTracking: false, useSliceString: true });
22+
const hps = require("@apache-fory/hps").default;
23+
const fory = new Fory.default({
24+
hps,
25+
refTracking: false,
26+
useSliceString: true,
27+
});
2428
const Benchmark = require("benchmark");
2529
const protobuf = require("protobufjs");
26-
const path = require('path');
30+
const path = require("path");
2731
const Type = Fory.Type;
28-
const assert = require('assert');
32+
const assert = require("assert");
2933
const { spawn } = require("child_process");
3034

31-
32-
export const data2TypeInfo = (
33-
data,
34-
typeName,
35-
) => {
35+
export const data2TypeInfo = (data, typeName) => {
3636
if (data === null || data === undefined) {
3737
return null;
3838
}
@@ -75,7 +75,7 @@ export const data2TypeInfo = (
7575

7676
return Type.struct(
7777
{
78-
typeName
78+
typeName,
7979
},
8080
Object.fromEntries(
8181
Object.entries(data)
@@ -90,7 +90,6 @@ export const data2TypeInfo = (
9090
throw new Error(`unknown data type ${typeof data}`);
9191
};
9292

93-
9493
const sample = {
9594
id: 123456,
9695
name: "John Doe",
@@ -169,16 +168,16 @@ const sample = {
169168
`,
170169
};
171170

172-
173171
const typeinfo = utils.data2TypeInfo(sample, "fory.test.foo");
174-
const { serialize, deserialize, serializeVolatile } = fory.registerSerializer(typeinfo);
172+
const { serialize, deserialize, serializeVolatile } =
173+
fory.registerSerializer(typeinfo);
175174

176175
const foryAb = serialize(sample);
177176
const sampleJson = JSON.stringify(sample);
178177

179178
function loadProto() {
180179
return new Promise((resolve) => {
181-
protobuf.load(path.join(__dirname, 'sample.proto'), function (err, root) {
180+
protobuf.load(path.join(__dirname, "sample.proto"), function (err, root) {
182181
if (err) throw err;
183182
const AwesomeMessage = root.lookupType("SomeMessage");
184183
resolve({
@@ -205,7 +204,10 @@ async function start() {
205204
const protobufBf = protobufEncode(sample);
206205

207206
{
208-
console.log('sample json size: ', `${(sampleJson.length / 1000).toFixed()}k`);
207+
console.log(
208+
"sample json size: ",
209+
`${(sampleJson.length / 1000).toFixed()}k`,
210+
);
209211
assert(JSON.stringify(protobufDecode(protobufBf)) === sampleJson);
210212
assert.deepEqual(deserialize(foryAb), sample);
211213
}
@@ -221,8 +223,8 @@ async function start() {
221223
json: {
222224
serialize: 0,
223225
deserialize: 0,
224-
}
225-
}
226+
},
227+
};
226228

227229
{
228230
var suite = new Benchmark.Suite();
@@ -244,7 +246,6 @@ async function start() {
244246
.run({ async: false });
245247
}
246248

247-
248249
{
249250
var suite = new Benchmark.Suite();
250251
suite
@@ -268,10 +269,18 @@ async function start() {
268269

269270
spawn(
270271
`python3`,
271-
['draw.py', result.json.serialize, result.json.deserialize, result.protobuf.serialize, result.protobuf.deserialize, result.fory.serialize, result.fory.deserialize],
272+
[
273+
"draw.py",
274+
result.json.serialize,
275+
result.json.deserialize,
276+
result.protobuf.serialize,
277+
result.protobuf.deserialize,
278+
result.fory.serialize,
279+
result.fory.deserialize,
280+
],
272281
{
273282
cwd: __dirname,
274-
}
275-
)
283+
},
284+
);
276285
}
277286
start();

javascript/benchmark/map.js

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,88 @@
1919

2020
const Fory = require("@apache-fory/fory");
2121
const beautify = require("js-beautify");
22-
const hps = require('@apache-fory/hps');
22+
const hps = require("@apache-fory/hps");
2323
const fory = new Fory.default({
24-
hps, refTracking: false, useSliceString: true, hooks: {
24+
hps,
25+
refTracking: false,
26+
useSliceString: true,
27+
hooks: {
2528
afterCodeGenerated: (code) => {
26-
return beautify.js(code, { indent_size: 2, space_in_empty_paren: true, indent_empty_lines: true });
27-
}
28-
}
29+
return beautify.js(code, {
30+
indent_size: 2,
31+
space_in_empty_paren: true,
32+
indent_empty_lines: true,
33+
});
34+
},
35+
},
2936
});
3037
const Benchmark = require("benchmark");
3138
const Type = Fory.Type;
3239

40+
const {
41+
serialize: serialize1,
42+
deserialize: deserialize1,
43+
serializeVolatile: serializeVolatile1,
44+
} = fory.registerSerializer(
45+
Type.struct("any", {
46+
f1: Type.map(Type.any(), Type.any()),
47+
f2: Type.map(Type.any(), Type.any()),
48+
}),
49+
);
3350

34-
35-
const { serialize: serialize1, deserialize: deserialize1, serializeVolatile: serializeVolatile1 } = fory.registerSerializer(Type.struct("any", {
36-
f1: Type.map(Type.any(), Type.any()),
37-
f2: Type.map(Type.any(), Type.any())
38-
}));
39-
40-
const { serialize: serialize2, deserialize: deserialize2, serializeVolatile: serializeVolatile2 } = fory.registerSerializer(Type.struct("specific", {
41-
f1: Type.map(Type.string(), Type.string()),
42-
f2: Type.map(Type.int32(), Type.string())
43-
}));
51+
const {
52+
serialize: serialize2,
53+
deserialize: deserialize2,
54+
serializeVolatile: serializeVolatile2,
55+
} = fory.registerSerializer(
56+
Type.struct("specific", {
57+
f1: Type.map(Type.string(), Type.string()),
58+
f2: Type.map(Type.int32(), Type.string()),
59+
}),
60+
);
4461
const sample = {
45-
f1: new Map([["foo", "ba1"], ["foo1", "ba1"], ["foo2", "ba1"], ["foo3", "ba1"], ["foo4", "ba1"], ["foo5", "ba1"], ["foo5", "ba1"], ["foo5", "ba1"]]),
46-
f2: new Map([[123, "ba1"], [234, "ba1"], [345, "ba1"], [456, "ba1"], [567, "ba1"], [678, "ba1"], [789, "ba1"], [890, "ba1"]])
62+
f1: new Map([
63+
["foo", "ba1"],
64+
["foo1", "ba1"],
65+
["foo2", "ba1"],
66+
["foo3", "ba1"],
67+
["foo4", "ba1"],
68+
["foo5", "ba1"],
69+
["foo5", "ba1"],
70+
["foo5", "ba1"],
71+
]),
72+
f2: new Map([
73+
[123, "ba1"],
74+
[234, "ba1"],
75+
[345, "ba1"],
76+
[456, "ba1"],
77+
[567, "ba1"],
78+
[678, "ba1"],
79+
[789, "ba1"],
80+
[890, "ba1"],
81+
]),
4782
};
4883

4984
const foryAb1 = serialize1(sample);
5085
const foryAb2 = serialize2(sample);
5186

5287
async function start() {
53-
54-
let result = {
55-
}
88+
let result = {};
5689

5790
{
5891
var suite = new Benchmark.Suite();
5992
suite
6093
.add("any serialize", function () {
61-
serializeVolatile1(sample).dispose()
94+
serializeVolatile1(sample).dispose();
6295
})
6396
.add("any deserialize", function () {
64-
deserialize1(foryAb1)
97+
deserialize1(foryAb1);
6598
})
6699
.add("jit serialize", function () {
67-
serializeVolatile2(sample).dispose()
100+
serializeVolatile2(sample).dispose();
68101
})
69102
.add("jit deserialize", function () {
70-
deserialize2(foryAb2)
103+
deserialize2(foryAb2);
71104
})
72105
.on("complete", function (e) {
73106
e.currentTarget.forEach(({ name, hz }) => {
@@ -78,6 +111,5 @@ async function start() {
78111
}
79112

80113
console.table(result);
81-
82114
}
83115
start();

0 commit comments

Comments
 (0)