Skip to content

Commit fd50d2e

Browse files
authored
Fix performance tests (#6665)
* Fix broken performance tests and display consistent output. * Add doc comment.
1 parent 71b3cb7 commit fd50d2e

File tree

1 file changed

+61
-37
lines changed

1 file changed

+61
-37
lines changed

integration-tests/tests/src/performance-tests/property-reads.ts

+61-37
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,59 @@
1616
//
1717
////////////////////////////////////////////////////////////////////////////
1818

19-
import Realm, { ObjectSchema, PropertySchema } from "realm";
19+
import { expect } from "chai";
20+
import Realm, { BSON, ObjectSchema, PropertySchema, PropertySchemaShorthand } from "realm";
2021

2122
import { describePerformance } from "../utils/benchmark";
2223

2324
type Value = ((realm: Realm) => unknown) | unknown;
2425

25-
function getTypeName(type: Realm.PropertySchemaShorthand | Realm.PropertySchema) {
26-
if (typeof type === "object") {
27-
const prefix = type.optional ? "optional " : "";
28-
if (type.objectType) {
29-
return prefix + `${type.type}<${type.objectType}>`;
30-
} else {
31-
return prefix + type.type;
32-
}
33-
} else {
34-
return type;
26+
const COLLECTION_MARKERS: Readonly<Record<string, string>> = {
27+
list: "[]",
28+
dictionary: "{}",
29+
set: "<>",
30+
};
31+
32+
/**
33+
* Get a representative consistent name of the type depending on the schema.
34+
*
35+
* @example
36+
* "int?[]" -> "int?[]"
37+
* { type: "list", objectType: "int", optional: true } -> "int?[]"
38+
*/
39+
function getTypeDisplayName(schema: PropertySchemaShorthand | PropertySchema) {
40+
const isShorthand = typeof schema === "string";
41+
if (isShorthand) {
42+
return schema;
3543
}
44+
45+
const optionalMarker = schema.optional ? "?" : "";
46+
const collectionMarker = COLLECTION_MARKERS[schema.type] ?? "";
47+
48+
return (schema.objectType || schema.type) + optionalMarker + collectionMarker;
3649
}
3750

3851
type TestParameters = {
39-
name?: string;
40-
type: Realm.PropertySchemaShorthand | Realm.PropertySchema;
52+
propertySchema: PropertySchemaShorthand | PropertySchema;
4153
value: Value;
42-
schema?: Realm.ObjectSchema[];
54+
extraObjectSchemas?: ObjectSchema[];
4355
};
4456

45-
function describeTypeRead({ type, value, schema = [] }: TestParameters) {
46-
const typeName = getTypeName(type);
47-
const objectSchemaName = type + "Class";
48-
const propertyName = type + "Prop";
57+
function describeTypeRead({ propertySchema, value, extraObjectSchemas = [] }: TestParameters) {
58+
const typeDisplayName = getTypeDisplayName(propertySchema);
59+
const objectSchemaName = typeDisplayName + "Class";
60+
const propertyName = typeDisplayName + "Prop";
4961

5062
const defaultSchema: ObjectSchema = {
5163
name: objectSchemaName,
5264
properties: {
53-
[propertyName]:
54-
typeof type === "object"
55-
? type
56-
: ({
57-
type,
58-
optional: true,
59-
} as PropertySchema),
65+
[propertyName]: propertySchema,
6066
},
6167
};
6268

63-
describePerformance(`reading property of type '${typeName}'`, {
64-
schema: [defaultSchema, ...schema],
65-
benchmarkTitle: `reads ${typeName}`,
69+
describePerformance(`reading property of type '${typeDisplayName}'`, {
70+
schema: [defaultSchema, ...extraObjectSchemas],
71+
benchmarkTitle: `reads ${typeDisplayName}`,
6672
before(this: Partial<RealmObjectContext> & RealmContext & Mocha.Context) {
6773
this.realm.write(() => {
6874
this.object = this.realm.create(objectSchemaName, {
@@ -76,15 +82,17 @@ function describeTypeRead({ type, value, schema = [] }: TestParameters) {
7682
},
7783
test(this: RealmObjectContext) {
7884
const value = this.object[propertyName];
85+
// Performing a check to avoid the get of the property to be optimized away.
7986
if (typeof value === "undefined") {
80-
// Performing a check to avoid the get of the property to be optimized away
8187
throw new Error("Expected a value");
8288
}
8389
},
8490
});
8591
}
8692

87-
const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.PropertySchema, Value]> = [
93+
type SchemaValuePair = [PropertySchemaShorthand | PropertySchema, Value];
94+
95+
const cases: (TestParameters | SchemaValuePair)[] = [
8896
["bool?", true],
8997
["bool", true],
9098
["int?", 123],
@@ -93,15 +101,15 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope
93101
["double?", 123.456],
94102
["double", 123.456],
95103
["string?", "Hello!"],
96-
["decimal128?", new Realm.BSON.Decimal128("123")],
97-
["objectId?", new Realm.BSON.ObjectId("0000002a9a7969d24bea4cf4")],
98-
["uuid?", new Realm.BSON.UUID()],
104+
["decimal128?", new BSON.Decimal128("123")],
105+
["objectId?", new BSON.ObjectId("0000002a9a7969d24bea4cf4")],
106+
["uuid?", new BSON.UUID()],
99107
["date?", new Date("2000-01-01")],
100108
["data?", new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09])],
101109
{
102-
type: "Car",
103-
schema: [{ name: "Car", properties: { model: "string" } }],
110+
propertySchema: "Car",
104111
value: (realm: Realm) => realm.create("Car", { model: "VW Touran" }),
112+
extraObjectSchemas: [{ name: "Car", properties: { model: "string" } }],
105113
},
106114
["bool?[]", []],
107115
["bool?<>", []],
@@ -111,10 +119,26 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope
111119
describe.skipIf(environment.performance !== true, "Property read performance", () => {
112120
for (const c of cases) {
113121
if (Array.isArray(c)) {
114-
const [type, value] = c;
115-
describeTypeRead({ type, value });
122+
const [propertySchema, value] = c;
123+
describeTypeRead({ propertySchema, value });
116124
} else {
117125
describeTypeRead(c);
118126
}
119127
}
128+
129+
describe("Helpers", () => {
130+
it("getTypeDisplayName()", function () {
131+
expect(getTypeDisplayName("int")).equals("int");
132+
expect(getTypeDisplayName("int?")).equals("int?");
133+
expect(getTypeDisplayName("int?[]")).equals("int?[]");
134+
expect(getTypeDisplayName("Car")).equals("Car");
135+
expect(getTypeDisplayName({ type: "int" })).equals("int");
136+
expect(getTypeDisplayName({ type: "list", objectType: "int" })).equals("int[]");
137+
expect(getTypeDisplayName({ type: "list", objectType: "int", optional: true })).equals("int?[]");
138+
expect(getTypeDisplayName({ type: "dictionary", objectType: "int" })).equals("int{}");
139+
expect(getTypeDisplayName({ type: "set", objectType: "int" })).equals("int<>");
140+
expect(getTypeDisplayName({ type: "object", objectType: "Car" })).equals("Car");
141+
expect(getTypeDisplayName({ type: "object", objectType: "Car", optional: true })).equals("Car?");
142+
});
143+
});
120144
});

0 commit comments

Comments
 (0)