Skip to content

Commit

Permalink
add socket profile
Browse files Browse the repository at this point in the history
  • Loading branch information
zizdlp committed Oct 8, 2023
1 parent 9b7d7cb commit 5cc8b64
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ build:
bazel build //profile/workflow:sync_client
bazel build //profile/workflow:async_server
bazel build //profile/workflow:async_client
bazel build //profile/socket:server
bazel build //profile/socket:client
benchmark:
python benchmark.py --loop=$(LOOP) --length=$(LENGTH)
.PHONY: workflow_sync_server workflow_sync_client benchmark
8 changes: 6 additions & 2 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse

def benchmark(server, client, length=None, loop=None,port=None):
time.sleep(3)
# 启动 sync_server 进程
server_args = [server]
if port is not None:
Expand Down Expand Up @@ -36,12 +37,15 @@ def benchmark(server, client, length=None, loop=None,port=None):

args = parser.parse_args()
server_lists=[
"./bazel-bin/profile/socket/server",
"./bazel-bin/profile/workflow/sync_server",
"./bazel-bin/profile/workflow/async_server"
"./bazel-bin/profile/workflow/async_server",

]
client_lists=[
"./bazel-bin/profile/socket/client",
"./bazel-bin/profile/workflow/sync_client",
"./bazel-bin/profile/workflow/async_client"
"./bazel-bin/profile/workflow/async_client",
]
for i in range(len(server_lists)):
server = server_lists[i]
Expand Down
43 changes: 43 additions & 0 deletions profile/socket/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2020 the gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

licenses(["notice"])

cc_binary(
name = "client",
srcs = ["client.cc"],
defines = ["BAZEL_BUILD"],
deps = [
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/strings:str_format",
],
linkshared = False,
linkstatic = True,

)


cc_binary(
name = "server",
srcs = ["server.cc"],
defines = ["BAZEL_BUILD"],
deps = [
"@com_google_absl//absl/flags:flag",
"@com_google_absl//absl/flags:parse",
"@com_google_absl//absl/strings:str_format",
],
linkshared = False,
linkstatic = True,
)
89 changes: 89 additions & 0 deletions profile/socket/client.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/str_format.h"

#include <arpa/inet.h>
#include <netdb.h>

ABSL_FLAG(std::string, ip, "127.0.0.1", "Server address");
ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
ABSL_FLAG(uint32_t, loop, 1000, "client call loop times");
ABSL_FLAG(uint32_t, length, 25000, "send data bytes each time");

std::string convert2IP(std::string ip){

const char* hostname = ip.c_str();
struct addrinfo hints, *res;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // 使用IPv4

int status = getaddrinfo(hostname, NULL, &hints, &res);
if (status != 0) {
std::cerr << "getaddrinfo error: " << gai_strerror(status) << std::endl;
return "";
}

struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr;
char ip_address[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(addr->sin_addr), ip_address, INET_ADDRSTRLEN);

std::cout << "IP Address of " << hostname << " is: " << ip_address << std::endl;

freeaddrinfo(res);
return ip_address;
}
int RunClient(uint16_t port,int loop,std::string ip,int length){
ip = convert2IP(ip);
int sock = 0, valread = 0;
struct sockaddr_in serv_addr;
std::string send_data(length, 'a');
const char *hello = send_data.c_str();
char buffer[102400] = {0};

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
std::cerr << "Socket creation error" << std::endl;
return -1;
}

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);

// 将 IP 地址从字符串转换为网络地址
if(inet_pton(AF_INET,ip.c_str(), &serv_addr.sin_addr)<=0) {
std::cerr << "Invalid address/ Address not supported" << std::endl;
return -1;
}

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
std::cerr << "Connection Failed" << std::endl;
return -1;
}
auto s= std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
for(int i=0;i<loop;++i){
// 发送消息给服务器
send(sock, hello, strlen(hello), 0);
// std::cout << "Hello message sent" << std::endl;

// 从服务器接收消息
valread = read(sock, buffer, 1024);
// std::cout << "Server: " << buffer << std::endl;
}
auto e= std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch())
.count();
std::cout << "=== socket;"<<"port:"<<port<<";loop:"<<loop<<";length:"<<length<<";time:" << e - s << "us ===" << std::endl;
return 0;
}
int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
RunClient(absl::GetFlag(FLAGS_port),absl::GetFlag(FLAGS_loop),absl::GetFlag(FLAGS_ip),absl::GetFlag(FLAGS_length));
return 0;
}
63 changes: 63 additions & 0 deletions profile/socket/server.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "absl/flags/flag.h"
#include "absl/flags/parse.h"
#include "absl/strings/str_format.h"

ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
ABSL_FLAG(uint32_t, length, 25000, "send data bytes each time");
int RunServer(uint16_t port,int length) {
std::cout<<"========== mydebug: start socket server port:"<<port<<std::endl;
int server_fd, new_socket;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[102400] = {0};
const char *hello = "Hello from server";
// 创建 Socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
return 1;
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);

// 绑定端口
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
return 1;
}

// 监听
if (listen(server_fd, 3) < 0) {
perror("listen failed");
return 1;
}

if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
return 1;
}

while(true){
// 从客户端接收消息
read(new_socket, buffer, length);
// std::cout << "Client: " << buffer << std::endl;

// 发送消息给客户端
send(new_socket, hello, strlen(hello), 0);
// std::cout << "Hello message sent" << std::endl;
}

return 0;
}
int main(int argc, char** argv) {
absl::ParseCommandLine(argc, argv);
RunServer(absl::GetFlag(FLAGS_port),absl::GetFlag(FLAGS_length));
return 0;
}

0 comments on commit 5cc8b64

Please sign in to comment.