This document introduces how to use the serialization protocol Protobuf.
Protocol File Upload
Before using the Protobuf protocol, you need to upload .proto files. The performance testing engine relies on protocol files to complete the serialization of requests. You can upload files or directories. File names should be unique, and existing files with the same name will be overwritten by newly uploaded files. If you upload a ZIP file, PTS will decompress the file and display the structure of the decompressed file. If the directory or ZIP package contains files other than .proto files, PTS will ignore these files.
With the .proto files you upload, you can serialize/deserialize objects in the script. If demo.proto depends on other .proto files, they also need to be uploaded together (the standard .proto files provided by Google, namely, google/protobuf/*.proto, do not need to be uploaded separately because PTS will load them automatically). You only need to load the main .proto file, and other .proto files that the main .proto file depends on will be loaded recursively.
Example
Protocol File
duty.proto
syntax = "proto3";
message Duty {
string time = 1;
string work = 2;
}
student.proto
syntax = "proto3";
import "duty.proto";
package student;
message Student {
string name = 1;
Gender gender = 2;
message GradeInfo {
enum Grade {
DEFAULT = 0;
FIRST = 1;
SECOND = 2;
THIRD = 3;
}
Grade grade = 1;
}
GradeInfo gradeInfo = 3;
map<string, int32> scores = 4;
repeated Duty duties = 5;
}
enum Gender {
DEFAULT = 0;
FEMALE = 1;
MALE = 2;
}
message SearchRequest {
string id = 1;
}
message SearchResponse {
message Result {
Student student = 1;
}
}
service SearchService {
rpc SearchScores (SearchRequest) returns (SearchResponse);
}
Script
import protobuf from 'pts/protobuf';
protobuf.load([], 'student.proto');
export default function () {
let bodyBuffer = protobuf.marshal('student.Student', {
'name': 'Alice',
'gender': 1,
'gradeInfo': {
'grade': 'THIRD'
},
'scores': {
'Chinese': 116,
'Math': 120,
'English': 106
},
'duties': [
{
'time': 'time1',
'work': 'work1'
},
{
'time': 'time2',
'work': 'work2'
}
]
});
const value = protobuf.unmarshal('student.Student', bodyBuffer);
console.log(JSON.stringify(value));
}