-
Notifications
You must be signed in to change notification settings - Fork 588
Fix performance tests #6665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix performance tests #6665
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,53 +16,59 @@ | |
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
import Realm, { ObjectSchema, PropertySchema } from "realm"; | ||
import { expect } from "chai"; | ||
import Realm, { BSON, ObjectSchema, PropertySchema, PropertySchemaShorthand } from "realm"; | ||
|
||
import { describePerformance } from "../utils/benchmark"; | ||
|
||
type Value = ((realm: Realm) => unknown) | unknown; | ||
|
||
function getTypeName(type: Realm.PropertySchemaShorthand | Realm.PropertySchema) { | ||
if (typeof type === "object") { | ||
const prefix = type.optional ? "optional " : ""; | ||
if (type.objectType) { | ||
return prefix + `${type.type}<${type.objectType}>`; | ||
} else { | ||
return prefix + type.type; | ||
} | ||
} else { | ||
return type; | ||
const COLLECTION_MARKERS: Readonly<Record<string, string>> = { | ||
list: "[]", | ||
dictionary: "{}", | ||
set: "<>", | ||
}; | ||
|
||
/** | ||
* Get a representative consistent name of the type depending on the schema. | ||
* | ||
* @example | ||
* "int?[]" -> "int?[]" | ||
* { type: "list", objectType: "int", optional: true } -> "int?[]" | ||
*/ | ||
function getTypeDisplayName(schema: PropertySchemaShorthand | PropertySchema) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming from |
||
const isShorthand = typeof schema === "string"; | ||
if (isShorthand) { | ||
return schema; | ||
} | ||
|
||
const optionalMarker = schema.optional ? "?" : ""; | ||
const collectionMarker = COLLECTION_MARKERS[schema.type] ?? ""; | ||
|
||
return (schema.objectType || schema.type) + optionalMarker + collectionMarker; | ||
} | ||
|
||
type TestParameters = { | ||
name?: string; | ||
type: Realm.PropertySchemaShorthand | Realm.PropertySchema; | ||
propertySchema: PropertySchemaShorthand | PropertySchema; | ||
value: Value; | ||
schema?: Realm.ObjectSchema[]; | ||
extraObjectSchemas?: ObjectSchema[]; | ||
}; | ||
|
||
function describeTypeRead({ type, value, schema = [] }: TestParameters) { | ||
const typeName = getTypeName(type); | ||
const objectSchemaName = type + "Class"; | ||
const propertyName = type + "Prop"; | ||
function describeTypeRead({ propertySchema, value, extraObjectSchemas = [] }: TestParameters) { | ||
const typeDisplayName = getTypeDisplayName(propertySchema); | ||
const objectSchemaName = typeDisplayName + "Class"; | ||
const propertyName = typeDisplayName + "Prop"; | ||
|
||
const defaultSchema: ObjectSchema = { | ||
name: objectSchemaName, | ||
properties: { | ||
[propertyName]: | ||
typeof type === "object" | ||
? type | ||
: ({ | ||
type, | ||
optional: true, | ||
} as PropertySchema), | ||
[propertyName]: propertySchema, | ||
}, | ||
}; | ||
|
||
describePerformance(`reading property of type '${typeName}'`, { | ||
schema: [defaultSchema, ...schema], | ||
benchmarkTitle: `reads ${typeName}`, | ||
describePerformance(`reading property of type '${typeDisplayName}'`, { | ||
schema: [defaultSchema, ...extraObjectSchemas], | ||
benchmarkTitle: `reads ${typeDisplayName}`, | ||
before(this: Partial<RealmObjectContext> & RealmContext & Mocha.Context) { | ||
this.realm.write(() => { | ||
this.object = this.realm.create(objectSchemaName, { | ||
|
@@ -76,15 +82,17 @@ function describeTypeRead({ type, value, schema = [] }: TestParameters) { | |
}, | ||
test(this: RealmObjectContext) { | ||
const value = this.object[propertyName]; | ||
// Performing a check to avoid the get of the property to be optimized away. | ||
if (typeof value === "undefined") { | ||
// Performing a check to avoid the get of the property to be optimized away | ||
throw new Error("Expected a value"); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.PropertySchema, Value]> = [ | ||
type SchemaValuePair = [PropertySchemaShorthand | PropertySchema, Value]; | ||
|
||
const cases: (TestParameters | SchemaValuePair)[] = [ | ||
["bool?", true], | ||
["bool", true], | ||
["int?", 123], | ||
|
@@ -93,15 +101,15 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope | |
["double?", 123.456], | ||
["double", 123.456], | ||
["string?", "Hello!"], | ||
["decimal128?", new Realm.BSON.Decimal128("123")], | ||
["objectId?", new Realm.BSON.ObjectId("0000002a9a7969d24bea4cf4")], | ||
["uuid?", new Realm.BSON.UUID()], | ||
["decimal128?", new BSON.Decimal128("123")], | ||
["objectId?", new BSON.ObjectId("0000002a9a7969d24bea4cf4")], | ||
["uuid?", new BSON.UUID()], | ||
["date?", new Date("2000-01-01")], | ||
["data?", new Uint8Array([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09])], | ||
{ | ||
type: "Car", | ||
schema: [{ name: "Car", properties: { model: "string" } }], | ||
propertySchema: "Car", | ||
value: (realm: Realm) => realm.create("Car", { model: "VW Touran" }), | ||
extraObjectSchemas: [{ name: "Car", properties: { model: "string" } }], | ||
}, | ||
["bool?[]", []], | ||
["bool?<>", []], | ||
|
@@ -111,10 +119,26 @@ const cases: Array<TestParameters | [Realm.PropertySchemaShorthand | Realm.Prope | |
describe.skipIf(environment.performance !== true, "Property read performance", () => { | ||
for (const c of cases) { | ||
if (Array.isArray(c)) { | ||
const [type, value] = c; | ||
describeTypeRead({ type, value }); | ||
const [propertySchema, value] = c; | ||
describeTypeRead({ propertySchema, value }); | ||
} else { | ||
describeTypeRead(c); | ||
} | ||
} | ||
|
||
describe("Helpers", () => { | ||
it("getTypeDisplayName()", function () { | ||
expect(getTypeDisplayName("int")).equals("int"); | ||
expect(getTypeDisplayName("int?")).equals("int?"); | ||
expect(getTypeDisplayName("int?[]")).equals("int?[]"); | ||
expect(getTypeDisplayName("Car")).equals("Car"); | ||
expect(getTypeDisplayName({ type: "int" })).equals("int"); | ||
expect(getTypeDisplayName({ type: "list", objectType: "int" })).equals("int[]"); | ||
expect(getTypeDisplayName({ type: "list", objectType: "int", optional: true })).equals("int?[]"); | ||
expect(getTypeDisplayName({ type: "dictionary", objectType: "int" })).equals("int{}"); | ||
expect(getTypeDisplayName({ type: "set", objectType: "int" })).equals("int<>"); | ||
expect(getTypeDisplayName({ type: "object", objectType: "Car" })).equals("Car"); | ||
expect(getTypeDisplayName({ type: "object", objectType: "Car", optional: true })).equals("Car?"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Widening the type here for better ergonomics when bracket accessing it by an unknown string (and that's the only way it's used in this file).