tencent cloud

Protobuf-based Performance Testing
Last updated: 2025-03-10 22:14:19
Protobuf-based Performance Testing
Last updated: 2025-03-10 22:14:19
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.



Note:
For multi-protocol files, see Using Protocol 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';


// Load the student.proto file in the root directory of the protocol files. The duty.proto file will be loaded at the same time.
protobuf.load([], 'student.proto');


// Load the student.proto file in the dirName directory of the protocol files.
// protobuf.load(['dirName'], 'student.proto');


export default function () {
let bodyBuffer = protobuf.marshal('student.Student', {
'name': 'Alice',
'gender': 1, // Alternatively, set it to FEMALE. You can directly set the specific value for enum.
'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);
// {"name":"Alice","gender":"FEMALE","gradeInfo":{"grade":"THIRD"},"scores":{"Math":120,"Chinese":116,"English":106},"duties":[{"time":"time","work":"work"}]}
console.log(JSON.stringify(value));
}

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback