Skip to content

Commit 03f9475

Browse files
committed
streaming app for stress testing ssh transport
1 parent d4938e3 commit 03f9475

File tree

4 files changed

+419
-0
lines changed

4 files changed

+419
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{FB6C3DD6-8AE3-4115-8178-95646161878C}</ProjectGuid>
23+
<Keyword>Win32Proj</Keyword>
24+
<RootNamespace>WinsockClient</RootNamespace>
25+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
26+
<ProjectName>sock-app</ProjectName>
27+
</PropertyGroup>
28+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
29+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
30+
<ConfigurationType>Application</ConfigurationType>
31+
<UseDebugLibraries>true</UseDebugLibraries>
32+
<PlatformToolset>v140</PlatformToolset>
33+
<CharacterSet>Unicode</CharacterSet>
34+
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
36+
<ConfigurationType>Application</ConfigurationType>
37+
<UseDebugLibraries>false</UseDebugLibraries>
38+
<PlatformToolset>v140</PlatformToolset>
39+
<WholeProgramOptimization>true</WholeProgramOptimization>
40+
<CharacterSet>Unicode</CharacterSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
43+
<ConfigurationType>Application</ConfigurationType>
44+
<UseDebugLibraries>true</UseDebugLibraries>
45+
<PlatformToolset>v140</PlatformToolset>
46+
<CharacterSet>Unicode</CharacterSet>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49+
<ConfigurationType>Application</ConfigurationType>
50+
<UseDebugLibraries>false</UseDebugLibraries>
51+
<PlatformToolset>v140</PlatformToolset>
52+
<WholeProgramOptimization>true</WholeProgramOptimization>
53+
<CharacterSet>Unicode</CharacterSet>
54+
</PropertyGroup>
55+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
56+
<ImportGroup Label="ExtensionSettings">
57+
</ImportGroup>
58+
<ImportGroup Label="Shared">
59+
</ImportGroup>
60+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
61+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
62+
</ImportGroup>
63+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
64+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
65+
</ImportGroup>
66+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
67+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
68+
</ImportGroup>
69+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
70+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
71+
</ImportGroup>
72+
<PropertyGroup Label="UserMacros" />
73+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<LinkIncremental>true</LinkIncremental>
75+
</PropertyGroup>
76+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
77+
<LinkIncremental>true</LinkIncremental>
78+
</PropertyGroup>
79+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
80+
<LinkIncremental>false</LinkIncremental>
81+
</PropertyGroup>
82+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
83+
<LinkIncremental>false</LinkIncremental>
84+
<TargetName>st</TargetName>
85+
</PropertyGroup>
86+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
87+
<ClCompile>
88+
<PrecompiledHeader>
89+
</PrecompiledHeader>
90+
<WarningLevel>Level3</WarningLevel>
91+
<Optimization>Disabled</Optimization>
92+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
93+
</ClCompile>
94+
<Link>
95+
<SubSystem>Console</SubSystem>
96+
<GenerateDebugInformation>true</GenerateDebugInformation>
97+
</Link>
98+
</ItemDefinitionGroup>
99+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
100+
<ClCompile>
101+
<PrecompiledHeader>
102+
</PrecompiledHeader>
103+
<WarningLevel>Level3</WarningLevel>
104+
<Optimization>Disabled</Optimization>
105+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
106+
</ClCompile>
107+
<Link>
108+
<SubSystem>Console</SubSystem>
109+
<GenerateDebugInformation>true</GenerateDebugInformation>
110+
</Link>
111+
</ItemDefinitionGroup>
112+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
113+
<ClCompile>
114+
<WarningLevel>Level3</WarningLevel>
115+
<PrecompiledHeader>
116+
</PrecompiledHeader>
117+
<Optimization>MaxSpeed</Optimization>
118+
<FunctionLevelLinking>true</FunctionLevelLinking>
119+
<IntrinsicFunctions>true</IntrinsicFunctions>
120+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
121+
</ClCompile>
122+
<Link>
123+
<SubSystem>Console</SubSystem>
124+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
125+
<OptimizeReferences>true</OptimizeReferences>
126+
<GenerateDebugInformation>true</GenerateDebugInformation>
127+
</Link>
128+
</ItemDefinitionGroup>
129+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
130+
<ClCompile>
131+
<WarningLevel>Level3</WarningLevel>
132+
<PrecompiledHeader>
133+
</PrecompiledHeader>
134+
<Optimization>MaxSpeed</Optimization>
135+
<FunctionLevelLinking>true</FunctionLevelLinking>
136+
<IntrinsicFunctions>true</IntrinsicFunctions>
137+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
138+
</ClCompile>
139+
<Link>
140+
<SubSystem>Console</SubSystem>
141+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
142+
<OptimizeReferences>true</OptimizeReferences>
143+
<GenerateDebugInformation>true</GenerateDebugInformation>
144+
</Link>
145+
</ItemDefinitionGroup>
146+
<ItemGroup>
147+
<ClCompile Include="sock-app.cpp" />
148+
</ItemGroup>
149+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
150+
<ImportGroup Label="ExtensionTargets">
151+
</ImportGroup>
152+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
</ItemGroup>
9+
<ItemGroup>
10+
<ClCompile Include="sock-app.cpp">
11+
<Filter>Source Files</Filter>
12+
</ClCompile>
13+
</ItemGroup>
14+
</Project>
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
2+
3+
#define WIN32_LEAN_AND_MEAN
4+
5+
#include <windows.h>
6+
#include <winsock2.h>
7+
#include <ws2tcpip.h>
8+
#include <stdlib.h>
9+
#include <stdio.h>
10+
11+
12+
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
13+
#pragma comment (lib, "Ws2_32.lib")
14+
#pragma comment (lib, "Mswsock.lib")
15+
#pragma comment (lib, "AdvApi32.lib")
16+
17+
18+
#define DEFAULT_BUFLEN 512*1024 //512 KB
19+
char recvbuf[DEFAULT_BUFLEN];
20+
char sendbuf[DEFAULT_BUFLEN];
21+
bool keep_going = true;
22+
__int64 rec_bytes = 0, sent_bytes = 0;
23+
bool server = true;
24+
25+
26+
void prep_send_buf()
27+
{
28+
int *buf = (int*)sendbuf;
29+
for (int i = 0; i < DEFAULT_BUFLEN; i += sizeof(int))
30+
*buf++ = rand();
31+
}
32+
33+
SOCKET ConnectSocket = INVALID_SOCKET;
34+
35+
DWORD WINAPI RecvThread(
36+
_In_ LPVOID lpParameter
37+
) {
38+
int rec = 1;
39+
while (keep_going && (rec>0)) {
40+
rec = recv(ConnectSocket, recvbuf, DEFAULT_BUFLEN, 0);
41+
rec_bytes += rec;
42+
}
43+
return 0;
44+
}
45+
46+
DWORD WINAPI SendThread(
47+
_In_ LPVOID lpParameter
48+
) {
49+
int rec = 1, rnd;
50+
while (keep_going && (rec>0)) {
51+
rnd = rand();
52+
rec = send(ConnectSocket, sendbuf + rnd, DEFAULT_BUFLEN - rnd, 0);
53+
sent_bytes += rec;
54+
}
55+
return 0;
56+
}
57+
58+
int __cdecl main(int argc, char **argv)
59+
{
60+
WSADATA wsaData;
61+
62+
struct addrinfo *result = NULL,
63+
*ptr = NULL,
64+
hints;
65+
66+
int iResult;
67+
68+
69+
// Validate the parameters
70+
if ((argc < 2) || (strlen(argv[1]) > 1)) {
71+
printf("usage: %s [c|s] IP port\n", argv[0]);
72+
return 1;
73+
}
74+
75+
if (argv[1][0] == 'c')
76+
server = false;
77+
78+
// Initialize Winsock
79+
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
80+
if (iResult != 0) {
81+
printf("WSAStartup failed with error: %d\n", iResult);
82+
return 1;
83+
}
84+
85+
ZeroMemory(&hints, sizeof(hints));
86+
hints.ai_family = AF_UNSPEC;
87+
hints.ai_socktype = SOCK_STREAM;
88+
hints.ai_protocol = IPPROTO_TCP;
89+
90+
// Resolve the server address and port
91+
iResult = getaddrinfo(argv[2], argv[3], &hints, &result);
92+
if (iResult != 0) {
93+
printf("getaddrinfo failed with error: %d\n", iResult);
94+
WSACleanup();
95+
return 1;
96+
}
97+
98+
if (!server) {
99+
// Attempt to connect to an address until one succeeds
100+
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
101+
102+
// Create a SOCKET for connecting to server
103+
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
104+
ptr->ai_protocol);
105+
if (ConnectSocket == INVALID_SOCKET) {
106+
printf("socket failed with error: %ld\n", WSAGetLastError());
107+
WSACleanup();
108+
return 1;
109+
}
110+
111+
// Connect to server.
112+
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
113+
if (iResult == SOCKET_ERROR) {
114+
closesocket(ConnectSocket);
115+
ConnectSocket = INVALID_SOCKET;
116+
continue;
117+
}
118+
break;
119+
}
120+
121+
if (ConnectSocket == INVALID_SOCKET) {
122+
printf("Unable to connect to server!\n");
123+
WSACleanup();
124+
return 1;
125+
}
126+
}
127+
else {
128+
SOCKET ListenSocket;
129+
// Create a SOCKET for connecting to server
130+
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
131+
if (ListenSocket == INVALID_SOCKET) {
132+
printf("socket failed with error: %ld\n", WSAGetLastError());
133+
freeaddrinfo(result);
134+
WSACleanup();
135+
return 1;
136+
}
137+
138+
// Setup the TCP listening socket
139+
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
140+
if (iResult == SOCKET_ERROR) {
141+
printf("bind failed with error: %d\n", WSAGetLastError());
142+
freeaddrinfo(result);
143+
closesocket(ListenSocket);
144+
WSACleanup();
145+
return 1;
146+
}
147+
148+
iResult = listen(ListenSocket, SOMAXCONN);
149+
if (iResult == SOCKET_ERROR) {
150+
printf("listen failed with error: %d\n", WSAGetLastError());
151+
closesocket(ListenSocket);
152+
WSACleanup();
153+
return 1;
154+
}
155+
156+
// Accept a client socket
157+
ConnectSocket = accept(ListenSocket, NULL, NULL);
158+
if (ConnectSocket == INVALID_SOCKET) {
159+
printf("accept failed with error: %d\n", WSAGetLastError());
160+
closesocket(ListenSocket);
161+
WSACleanup();
162+
return 1;
163+
}
164+
// No longer need server socket
165+
closesocket(ListenSocket);
166+
167+
168+
}
169+
170+
freeaddrinfo(result);
171+
172+
HANDLE rt = CreateThread(NULL, 0, RecvThread, NULL, 0, NULL);
173+
if (rt == NULL) {
174+
printf("Unable to create read thread!\n");
175+
closesocket(ConnectSocket);
176+
WSACleanup();
177+
return 1;
178+
}
179+
180+
HANDLE wt = CreateThread(NULL, 0, SendThread, NULL, 0, NULL);
181+
if (wt == NULL) {
182+
printf("Unable to create send thread!\n");
183+
closesocket(ConnectSocket);
184+
WSACleanup();
185+
return 1;
186+
}
187+
188+
HANDLE timer = CreateWaitableTimer(NULL, FALSE, NULL);
189+
if (timer == NULL){
190+
printf("Unable to create timer!\n");
191+
closesocket(ConnectSocket);
192+
WSACleanup();
193+
return 1;
194+
}
195+
LARGE_INTEGER liDueTime;
196+
liDueTime.QuadPart = 0;
197+
if(!SetWaitableTimer(timer, &liDueTime, 2000, NULL, NULL, false)) {
198+
printf("Unable to set timer!\n");
199+
closesocket(ConnectSocket);
200+
WSACleanup();
201+
return 1;
202+
}
203+
204+
printf("\t Recv(Kb/s) \t\t Sent(Kb/s)\n");
205+
__int64 last_recv = 0;
206+
__int64 last_send = 0;
207+
while (1) {
208+
if (WAIT_OBJECT_0 != WaitForSingleObject(timer, INFINITE)) {
209+
printf("wait failed %d\n", GetLastError());
210+
break;
211+
}
212+
__int64 now_recv = rec_bytes;
213+
__int64 now_send = sent_bytes;
214+
215+
printf("\r\t %lld \t\t %lld", (now_recv - last_recv) / 2048, (now_send - last_send) / 2048);
216+
last_recv = now_recv;
217+
last_send = now_send;
218+
219+
}
220+
221+
closesocket(ConnectSocket);
222+
WSACleanup();
223+
224+
return 0;
225+
}

0 commit comments

Comments
 (0)