Basic Concept of Socket
Socket is a communication mechanism provided by the operating system for inter-process communication. The operating system provides a set of socket APIs that encapsulate the TCP/IP protocol, allowing processes to use sockets and send and receive network data through these APIs.
When you use socket APIs, the IP address and port of a process form a socket address. The socket addresses of both client and server processes, along with the transport protocol (TCP or UDP), form a socket 5-tuple, which identifies a network communication.
Script Writing
The socket module of the PTS API provides APIs supporting sockets. For details, see pts/socket. By using these APIs, you can create a socket instance and send or receive TCP/UDP data through this instance.
Basic usage:
1. Create a socket instance.
Create a socket instance by using the new socket.Conn
method. The parameters of this method include the protocol name (tcp
or udp
), service address, and service port.
2. Use the socket instance.
Sending data: Send data by using the send
method of the instance. The parameter is binary data, and the return value is the number of bytes sent.
Receiving data: Receive data by using the recv
method of the instance. The parameter is the number of bytes received, and the return value is the binary data received.
Closing the connection: Close the connection by using the close
method of the instance.
Script example with the TCP protocol used:
import socket from "pts/socket";
import util from 'pts/util';
import {sleep} from 'pts';
export default function () {
const tcp_socket = new socket.Conn('tcp', '127.0.0.1', 80);
const send_data = `GET /get HTTP/1.1
Host: 127.0.0.1
User-Agent: pts-engine
\r\n`;
tcp_socket.send(util.toArrayBuffer(send_data));
const bytes_read = tcp_socket.recv(512);
tcp_socket.close();
console.log(bytes_read);
sleep(1);
}
Script example with the UDP protocol used:
import socket from "pts/socket";
import util from 'pts/util';
export default function main() {
const udp_socket = new socket.Conn('udp', '127.0.0.1', 20001);
const send_data = `test data`;
udp_socket.send(util.toArrayBuffer(send_data));
const bytes_read = udp_socket.recv(1024);
udp_socket.close();
console.log(bytes_read);
}
Script Verification
To verify the script execution results, you can use the PTS debugging feature to quickly verify whether the results meet expectations before the formal performance testing. For more details, see Debug Scenarios. File Dependency
In the performance testing scenario, you can upload the following types of files to provide status data during the execution of the performance testing task:
Parameter file: It dynamically provides test data in CSV format. That is, when the scenario is executed by each concurrent user (VU), each line of data will be obtained from the parameter file as the test data values for reference by variables in the script. For specific usage, see Using Parameter Files. Request file: It is required for constructing your request, for example, the file that needs to be uploaded. For specific usage, see Using Request Files. Protocol file: It is required for request serialization. For specific usage, see Using Protocol Files.