Skip to content

Commit

Permalink
Fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed May 18, 2024
1 parent bf000ac commit 22dafa2
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 69 deletions.
58 changes: 50 additions & 8 deletions packages/tests/src/benchmark/Benchmark.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,26 @@ function makeAdvancedObjectSchema() {
});
}

function makeAdvancedStrictObjectSchema() {
return S$RescriptSchema.$$Object.strict(S$RescriptSchema.object(function (s) {
return {
number: s.f("number", S$RescriptSchema.$$float),
negNumber: s.f("negNumber", S$RescriptSchema.$$float),
maxNumber: s.f("maxNumber", S$RescriptSchema.$$float),
string: s.f("string", S$RescriptSchema.string),
longString: s.f("longString", S$RescriptSchema.string),
boolean: s.f("boolean", S$RescriptSchema.bool),
deeplyNested: s.f("deeplyNested", S$RescriptSchema.$$Object.strict(S$RescriptSchema.object(function (s) {
return {
foo: s.f("foo", S$RescriptSchema.string),
num: s.f("num", S$RescriptSchema.$$float),
bool: s.f("bool", S$RescriptSchema.bool)
};
})))
};
}));
}

var data = makeTestObject();

console.time("init");
Expand Down Expand Up @@ -94,15 +114,37 @@ S$RescriptSchema.serializeWith(data, schema);

console.timeEnd("s: 3");

run(addWithPrepare(new (Benchmark.default.Suite)(), "Parse array", (function () {
var schema = S$RescriptSchema.array(S$RescriptSchema.string);
var data = [
"Hello world!",
"Bar",
"foo"
];
run(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(new (Benchmark.default.Suite)(), "Parse string", (function () {
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})), "Serialize string", (function () {
return function () {
return S$RescriptSchema.serializeOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})).add("Advanced object schema factory", makeAdvancedObjectSchema), "Parse advanced object", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Create and parse advanced object", (function () {
var data = makeTestObject();
return function () {
var schema = makeAdvancedObjectSchema();
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Parse advanced strict object", (function () {
var schema = makeAdvancedStrictObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Serialize advanced object", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
return S$RescriptSchema.serializeOrRaiseWith(data, schema);
};
})));

Expand Down
122 changes: 61 additions & 61 deletions packages/tests/src/benchmark/Benchmark.res
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,28 @@ let makeAdvancedObjectSchema = () => {
)
}

// let makeAdvancedStrictObjectSchema = () => {
// S.object(s =>
// {
// "number": s.field("number", S.float),
// "negNumber": s.field("negNumber", S.float),
// "maxNumber": s.field("maxNumber", S.float),
// "string": s.field("string", S.string),
// "longString": s.field("longString", S.string),
// "boolean": s.field("boolean", S.bool),
// "deeplyNested": s.field(
// "deeplyNested",
// S.object(s =>
// {
// "foo": s.field("foo", S.string),
// "num": s.field("num", S.float),
// "bool": s.field("bool", S.bool),
// }
// )->S.Object.strict,
// ),
// }
// )->S.Object.strict
// }
let makeAdvancedStrictObjectSchema = () => {
S.object(s =>
{
"number": s.field("number", S.float),
"negNumber": s.field("negNumber", S.float),
"maxNumber": s.field("maxNumber", S.float),
"string": s.field("string", S.string),
"longString": s.field("longString", S.string),
"boolean": s.field("boolean", S.bool),
"deeplyNested": s.field(
"deeplyNested",
S.object(s =>
{
"foo": s.field("foo", S.string),
"num": s.field("num", S.float),
"bool": s.field("bool", S.bool),
}
)->S.Object.strict,
),
}
)->S.Object.strict
}

let data = makeTestObject()
Console.time("init")
Expand All @@ -123,47 +123,47 @@ data->S.serializeWith(schema)->ignore
Console.timeEnd("s: 3")

Suite.make()
->Suite.addWithPrepare("Parse array", () => {
let schema = S.array(S.string)
let data = ["Hello world!", "Bar", "foo"]
->Suite.addWithPrepare("Parse string", () => {
let schema = S.string
let data = "Hello world!"
() => {
data->S.parseAnyOrRaiseWith(schema)
}
})
// ->Suite.addWithPrepare("Serialize string", () => {
// let schema = S.string
// let data = "Hello world!"
// () => {
// data->S.serializeOrRaiseWith(schema)
// }
// })
// ->Suite.add("Advanced object schema factory", makeAdvancedObjectSchema)
// ->Suite.addWithPrepare("Parse advanced object", () => {
// let schema = makeAdvancedObjectSchema()
// let data = makeTestObject()
// () => {
// data->S.parseAnyOrRaiseWith(schema)
// }
// })
// ->Suite.addWithPrepare("Create and parse advanced object", () => {
// let data = makeTestObject()
// () => {
// let schema = makeAdvancedObjectSchema()
// data->S.parseAnyOrRaiseWith(schema)
// }
// })
// ->Suite.addWithPrepare("Parse advanced strict object", () => {
// let schema = makeAdvancedStrictObjectSchema()
// let data = makeTestObject()
// () => {
// data->S.parseAnyOrRaiseWith(schema)
// }
// })
// ->Suite.addWithPrepare("Serialize advanced object", () => {
// let schema = makeAdvancedObjectSchema()
// let data = makeTestObject()
// () => {
// data->S.serializeOrRaiseWith(schema)
// }
// })
->Suite.addWithPrepare("Serialize string", () => {
let schema = S.string
let data = "Hello world!"
() => {
data->S.serializeOrRaiseWith(schema)
}
})
->Suite.add("Advanced object schema factory", makeAdvancedObjectSchema)
->Suite.addWithPrepare("Parse advanced object", () => {
let schema = makeAdvancedObjectSchema()
let data = makeTestObject()
() => {
data->S.parseAnyOrRaiseWith(schema)
}
})
->Suite.addWithPrepare("Create and parse advanced object", () => {
let data = makeTestObject()
() => {
let schema = makeAdvancedObjectSchema()
data->S.parseAnyOrRaiseWith(schema)
}
})
->Suite.addWithPrepare("Parse advanced strict object", () => {
let schema = makeAdvancedStrictObjectSchema()
let data = makeTestObject()
() => {
data->S.parseAnyOrRaiseWith(schema)
}
})
->Suite.addWithPrepare("Serialize advanced object", () => {
let schema = makeAdvancedObjectSchema()
let data = makeTestObject()
() => {
data->S.serializeOrRaiseWith(schema)
}
})
->Suite.run

1 comment on commit 22dafa2

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 22dafa2 Previous: 9fb2e69 Ratio
Parse string 818568454 ops/sec (±0.11%) 819064824 ops/sec (±0.11%) 1.00
Serialize string 819244886 ops/sec (±0.06%) 819779932 ops/sec (±0.08%) 1.00
Advanced object schema factory 425245 ops/sec (±0.60%) 431262 ops/sec (±0.44%) 1.01
Parse advanced object 46108206 ops/sec (±1.24%) 46671133 ops/sec (±0.42%) 1.01
Create and parse advanced object 35778 ops/sec (±0.56%) 34129 ops/sec (±0.58%) 0.95
Parse advanced strict object 21009528 ops/sec (±0.14%) 22262984 ops/sec (±0.24%) 1.06
Serialize advanced object 807582787 ops/sec (±0.27%) 807737293 ops/sec (±0.20%) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.