-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add message and field name to binary serialization errors #984
Open
bhollis
wants to merge
4
commits into
bufbuild:main
Choose a base branch
from
StatelyCloud:to-binary-error-details
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+264
−225
Open
Changes from all commits
Commits
Show all changes
4 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 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -13,7 +13,14 @@ | |
// limitations under the License. | ||
|
||
import Benchmark from "benchmark"; | ||
import { create, fromBinary, toBinary, protoInt64 } from "@bufbuild/protobuf"; | ||
import { | ||
create, | ||
fromBinary, | ||
toBinary, | ||
protoInt64, | ||
toJsonString, | ||
fromJsonString, | ||
} from "@bufbuild/protobuf"; | ||
import { readFileSync } from "fs"; | ||
import { UserSchema } from "./gen/ts/extra/example_pb.js"; | ||
import { | ||
|
@@ -95,173 +102,163 @@ interface Test { | |
} | ||
|
||
function setupTests(): Test[] { | ||
const tests: Test[] = []; | ||
{ | ||
const bytes = readFileSync( | ||
new URL("perf-payload.bin", import.meta.url).pathname, | ||
); | ||
tests.push({ | ||
name: `fromBinary perf-payload.bin`, | ||
fn: () => { | ||
fromBinary(PerfMessageSchema, bytes); | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = UserSchema; | ||
const tinyUser = create(desc, { | ||
active: false, | ||
manager: { active: true }, | ||
}); | ||
const data = toBinary(desc, tinyUser); | ||
tests.push({ | ||
name: `fromBinary tiny example.User (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = UserSchema; | ||
const normalUser = create(desc, { | ||
firstName: "Jane", | ||
lastName: "Doe", | ||
active: true, | ||
manager: { firstName: "Jane", lastName: "Doe", active: false }, | ||
locations: ["Seattle", "New York", "Tokyo"], | ||
projects: { foo: "project foo", bar: "project bar" }, | ||
}); | ||
const data = toBinary(desc, normalUser); | ||
tests.push({ | ||
name: `fromBinary normal example.User (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = ScalarValuesMessageSchema; | ||
const message = create(ScalarValuesMessageSchema, { | ||
doubleField: 0.75, | ||
floatField: -0.75, | ||
int64Field: protoInt64.parse(-1), | ||
uint64Field: protoInt64.uParse(1), | ||
int32Field: -123, | ||
fixed64Field: protoInt64.uParse(1), | ||
fixed32Field: 123, | ||
boolField: true, | ||
stringField: "hello world", | ||
bytesField: new Uint8Array([ | ||
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, | ||
]), | ||
uint32Field: 123, | ||
sfixed32Field: -123, | ||
sfixed64Field: protoInt64.parse(-1), | ||
sint32Field: -1, | ||
sint64Field: protoInt64.parse(-1), | ||
}); | ||
const data = toBinary(desc, message); | ||
tests.push({ | ||
name: `fromBinary scalar values (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = RepeatedScalarValuesMessageSchema; | ||
const message = create(desc, { | ||
doubleField: [0.75, 0, 1], | ||
floatField: [0.75, -0.75], | ||
int64Field: [protoInt64.parse(-1), protoInt64.parse(-2)], | ||
uint64Field: [protoInt64.uParse(1), protoInt64.uParse(2)], | ||
int32Field: [-123, 500], | ||
fixed64Field: [protoInt64.uParse(1), protoInt64.uParse(99)], | ||
fixed32Field: [123, 999], | ||
boolField: [true, false, true], | ||
stringField: ["hello", "world"], | ||
bytesField: [ | ||
new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100]), | ||
], | ||
uint32Field: [123, 123], | ||
sfixed32Field: [-123, -123, -123], | ||
sfixed64Field: [ | ||
protoInt64.parse(-1), | ||
protoInt64.parse(-2), | ||
protoInt64.parse(100), | ||
], | ||
sint32Field: [-1, -2, 999], | ||
sint64Field: [ | ||
protoInt64.parse(-1), | ||
protoInt64.parse(-99), | ||
protoInt64.parse(99), | ||
], | ||
}); | ||
const data = toBinary(desc, message); | ||
tests.push({ | ||
name: `fromBinary repeated scalar fields (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = MapsMessageSchema; | ||
const message = create(desc, { | ||
strStrField: { a: "str", b: "xx" }, | ||
strInt32Field: { a: 123, b: 455 }, | ||
strInt64Field: { a: protoInt64.parse(123) }, | ||
strBoolField: { a: true, b: false }, | ||
strBytesField: { | ||
a: new Uint8Array([ | ||
const tests: Test[] = [ | ||
{ | ||
name: "perf-payload.bin", | ||
desc: PerfMessageSchema, | ||
msg: fromBinary( | ||
PerfMessageSchema, | ||
readFileSync(new URL("perf-payload.bin", import.meta.url).pathname), | ||
), | ||
}, | ||
{ | ||
name: "tiny example.User", | ||
desc: UserSchema, | ||
msg: create(UserSchema, { active: false, manager: { active: true } }), | ||
}, | ||
{ | ||
name: "normal example.User", | ||
desc: UserSchema, | ||
msg: create(UserSchema, { | ||
firstName: "Jane", | ||
lastName: "Doe", | ||
active: true, | ||
manager: { firstName: "Jane", lastName: "Doe", active: false }, | ||
locations: ["Seattle", "New York", "Tokyo"], | ||
projects: { foo: "project foo", bar: "project bar" }, | ||
}), | ||
}, | ||
{ | ||
name: "scalar values", | ||
desc: ScalarValuesMessageSchema, | ||
msg: create(ScalarValuesMessageSchema, { | ||
doubleField: 0.75, | ||
floatField: -0.75, | ||
int64Field: protoInt64.parse(-1), | ||
uint64Field: protoInt64.uParse(1), | ||
int32Field: -123, | ||
fixed64Field: protoInt64.uParse(1), | ||
fixed32Field: 123, | ||
boolField: true, | ||
stringField: "hello world", | ||
bytesField: new Uint8Array([ | ||
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, | ||
]), | ||
uint32Field: 123, | ||
sfixed32Field: -123, | ||
sfixed64Field: protoInt64.parse(-1), | ||
sint32Field: -1, | ||
sint64Field: protoInt64.parse(-1), | ||
}), | ||
}, | ||
{ | ||
name: "repeated scalar values", | ||
desc: RepeatedScalarValuesMessageSchema, | ||
msg: create(RepeatedScalarValuesMessageSchema, { | ||
doubleField: [0.75, 0, 1], | ||
floatField: [0.75, -0.75], | ||
int64Field: [protoInt64.parse(-1), protoInt64.parse(-2)], | ||
uint64Field: [protoInt64.uParse(1), protoInt64.uParse(2)], | ||
int32Field: [-123, 500], | ||
fixed64Field: [protoInt64.uParse(1), protoInt64.uParse(99)], | ||
fixed32Field: [123, 999], | ||
boolField: [true, false, true], | ||
stringField: ["hello", "world"], | ||
bytesField: [ | ||
new Uint8Array([ | ||
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, | ||
]), | ||
], | ||
uint32Field: [123, 123], | ||
sfixed32Field: [-123, -123, -123], | ||
sfixed64Field: [ | ||
protoInt64.parse(-1), | ||
protoInt64.parse(-2), | ||
protoInt64.parse(100), | ||
], | ||
sint32Field: [-1, -2, 999], | ||
sint64Field: [ | ||
protoInt64.parse(-1), | ||
protoInt64.parse(-99), | ||
protoInt64.parse(99), | ||
], | ||
}), | ||
}, | ||
{ | ||
name: "map with scalar keys and values", | ||
desc: MapsMessageSchema, | ||
msg: create(MapsMessageSchema, { | ||
strStrField: { a: "str", b: "xx" }, | ||
strInt32Field: { a: 123, b: 455 }, | ||
strInt64Field: { a: protoInt64.parse(123) }, | ||
strBoolField: { a: true, b: false }, | ||
strBytesField: { | ||
a: new Uint8Array([ | ||
104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, | ||
]), | ||
}, | ||
int32StrField: { 123: "hello" }, | ||
int64StrField: { "9223372036854775807": "hello" }, | ||
boolStrField: { true: "yes", false: "no" }, | ||
strEnuField: { a: 0, b: 1, c: 2 }, | ||
int32EnuField: { 1: 0, 2: 1, 0: 2 }, | ||
int64EnuField: { "-1": 0, "2": 1, "0": 2 }, | ||
}), | ||
}, | ||
{ | ||
name: "repeated field with 1000 messages", | ||
desc: MessageFieldMessageSchema, | ||
msg: (() => { | ||
const message = create(MessageFieldMessageSchema); | ||
for (let i = 0; i < 1000; i++) { | ||
message.repeatedMessageField.push( | ||
create(MessageFieldMessage_TestMessageSchema), | ||
); | ||
} | ||
return message; | ||
})(), | ||
}, | ||
{ | ||
name: "map field with 1000 messages", | ||
desc: MapsMessageSchema, | ||
msg: (() => { | ||
const message = create(MapsMessageSchema); | ||
for (let i = 0; i < 1000; i++) { | ||
message.strMsgField[i.toString()] = create(MapsMessageSchema); | ||
} | ||
return message; | ||
})(), | ||
}, | ||
].flatMap(({ name, msg, desc }) => { | ||
const bytes = toBinary(desc, msg); | ||
const jsonString = toJsonString(desc, msg); | ||
return [ | ||
{ | ||
name: `fromBinary ${name}`, | ||
Comment on lines
+235
to
+237
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. This is the actual change - we now benchmark |
||
fn: () => { | ||
fromBinary(desc, bytes); | ||
}, | ||
}, | ||
int32StrField: { 123: "hello" }, | ||
int64StrField: { "9223372036854775807": "hello" }, | ||
boolStrField: { true: "yes", false: "no" }, | ||
strEnuField: { a: 0, b: 1, c: 2 }, | ||
int32EnuField: { 1: 0, 2: 1, 0: 2 }, | ||
int64EnuField: { "-1": 0, "2": 1, "0": 2 }, | ||
}); | ||
const data = toBinary(desc, message); | ||
tests.push({ | ||
name: `fromBinary map with scalar keys and values (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
{ | ||
name: `fromJson ${name}`, | ||
fn: () => { | ||
fromJsonString(desc, jsonString); | ||
}, | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = MessageFieldMessageSchema; | ||
const message = create(desc); | ||
for (let i = 0; i < 1000; i++) { | ||
message.repeatedMessageField.push( | ||
create(MessageFieldMessage_TestMessageSchema), | ||
); | ||
} | ||
const data = toBinary(desc, message); | ||
tests.push({ | ||
name: `fromBinary repeated field with 1000 messages (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
{ | ||
name: `toBinary ${name}`, | ||
fn: () => { | ||
toBinary(desc, msg); | ||
}, | ||
}, | ||
}); | ||
} | ||
{ | ||
const desc = MapsMessageSchema; | ||
const message = create(desc); | ||
for (let i = 0; i < 1000; i++) { | ||
message.strMsgField[i.toString()] = create(desc); | ||
} | ||
const data = toBinary(desc, message); | ||
tests.push({ | ||
name: `fromBinary map field with 1000 messages (${data.byteLength} bytes)`, | ||
fn: () => { | ||
fromBinary(desc, data); | ||
{ | ||
name: `toJson ${name}`, | ||
fn: () => { | ||
toJsonString(desc, msg); | ||
}, | ||
}, | ||
}); | ||
} | ||
]; | ||
}); | ||
return tests; | ||
} | ||
|
||
|
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.
This looks like a huge change but I'm just switching to generating the benchmark cases instead of having to repeat code.