Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Grpc
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
tci-gateway-module
Grpc
Commits
d10bbb63
Commit
d10bbb63
authored
9 years ago
by
Vijay Pai
Browse files
Options
Downloads
Patches
Plain Diff
Refactor to favor composition over inheritance
Also make num_threads and num_rpcs as command-line flags
parent
ce7929c8
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test/cpp/end2end/thread_stress_test.cc
+49
-32
49 additions, 32 deletions
test/cpp/end2end/thread_stress_test.cc
with
49 additions
and
32 deletions
test/cpp/end2end/thread_stress_test.cc
+
49
−
32
View file @
d10bbb63
...
@@ -34,6 +34,7 @@
...
@@ -34,6 +34,7 @@
#include
<mutex>
#include
<mutex>
#include
<thread>
#include
<thread>
#include
<gflags/gflags.h>
#include
<grpc++/channel.h>
#include
<grpc++/channel.h>
#include
<grpc++/client_context.h>
#include
<grpc++/client_context.h>
#include
<grpc++/create_channel.h>
#include
<grpc++/create_channel.h>
...
@@ -54,6 +55,9 @@ using grpc::testing::EchoRequest;
...
@@ -54,6 +55,9 @@ using grpc::testing::EchoRequest;
using
grpc
::
testing
::
EchoResponse
;
using
grpc
::
testing
::
EchoResponse
;
using
std
::
chrono
::
system_clock
;
using
std
::
chrono
::
system_clock
;
DEFINE_int32
(
num_threads
,
100
,
"Number of threads"
);
DEFINE_int32
(
num_rpcs
,
1000
,
"Number of RPCs per thread"
);
namespace
grpc
{
namespace
grpc
{
namespace
testing
{
namespace
testing
{
...
@@ -168,11 +172,10 @@ class TestServiceImplDupPkg
...
@@ -168,11 +172,10 @@ class TestServiceImplDupPkg
}
}
};
};
class
End2endTest
:
public
::
testing
::
Test
{
class
CommonStressTest
{
protected:
public:
End2endTest
()
:
kMaxMessageSize_
(
8192
)
{}
CommonStressTest
()
:
kMaxMessageSize_
(
8192
)
{}
void
SetUp
()
{
void
SetUp
()
GRPC_OVERRIDE
{
int
port
=
grpc_pick_unused_port_or_die
();
int
port
=
grpc_pick_unused_port_or_die
();
server_address_
<<
"localhost:"
<<
port
;
server_address_
<<
"localhost:"
<<
port
;
// Setup server
// Setup server
...
@@ -185,15 +188,15 @@ class End2endTest : public ::testing::Test {
...
@@ -185,15 +188,15 @@ class End2endTest : public ::testing::Test {
builder
.
RegisterService
(
&
dup_pkg_service_
);
builder
.
RegisterService
(
&
dup_pkg_service_
);
server_
=
builder
.
BuildAndStart
();
server_
=
builder
.
BuildAndStart
();
}
}
void
TearDown
()
{
server_
->
Shutdown
();
}
void
TearDown
()
GRPC_OVERRIDE
{
server_
->
Shutdown
();
}
void
ResetStub
()
{
void
ResetStub
()
{
std
::
shared_ptr
<
Channel
>
channel
=
std
::
shared_ptr
<
Channel
>
channel
=
CreateChannel
(
server_address_
.
str
(),
InsecureChannelCredentials
());
CreateChannel
(
server_address_
.
str
(),
InsecureChannelCredentials
());
stub_
=
grpc
::
testing
::
EchoTestService
::
NewStub
(
channel
);
stub_
=
grpc
::
testing
::
EchoTestService
::
NewStub
(
channel
);
}
}
grpc
::
testing
::
EchoTestService
::
Stub
*
GetStub
()
{
return
stub_
.
get
();
}
private
:
std
::
unique_ptr
<
grpc
::
testing
::
EchoTestService
::
Stub
>
stub_
;
std
::
unique_ptr
<
grpc
::
testing
::
EchoTestService
::
Stub
>
stub_
;
std
::
unique_ptr
<
Server
>
server_
;
std
::
unique_ptr
<
Server
>
server_
;
std
::
ostringstream
server_address_
;
std
::
ostringstream
server_address_
;
...
@@ -202,6 +205,16 @@ class End2endTest : public ::testing::Test {
...
@@ -202,6 +205,16 @@ class End2endTest : public ::testing::Test {
TestServiceImplDupPkg
dup_pkg_service_
;
TestServiceImplDupPkg
dup_pkg_service_
;
};
};
class
End2endTest
:
public
::
testing
::
Test
{
protected:
End2endTest
()
{}
void
SetUp
()
GRPC_OVERRIDE
{
common_
.
SetUp
();
}
void
TearDown
()
GRPC_OVERRIDE
{
common_
.
TearDown
();
}
void
ResetStub
()
{
common_
.
ResetStub
();
}
CommonStressTest
common_
;
};
static
void
SendRpc
(
grpc
::
testing
::
EchoTestService
::
Stub
*
stub
,
int
num_rpcs
)
{
static
void
SendRpc
(
grpc
::
testing
::
EchoTestService
::
Stub
*
stub
,
int
num_rpcs
)
{
EchoRequest
request
;
EchoRequest
request
;
EchoResponse
response
;
EchoResponse
response
;
...
@@ -216,27 +229,29 @@ static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs) {
...
@@ -216,27 +229,29 @@ static void SendRpc(grpc::testing::EchoTestService::Stub* stub, int num_rpcs) {
}
}
TEST_F
(
End2endTest
,
ThreadStress
)
{
TEST_F
(
End2endTest
,
ThreadStress
)
{
ResetStub
();
common_
.
ResetStub
();
std
::
vector
<
std
::
thread
*>
threads
;
std
::
vector
<
std
::
thread
*>
threads
;
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
.
push_back
(
new
std
::
thread
(
SendRpc
,
stub_
.
get
(),
1000
));
threads
.
push_back
(
new
std
::
thread
(
SendRpc
,
common_
.
GetStub
(),
FLAGS_num_rpcs
));
}
}
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
[
i
]
->
join
();
threads
[
i
]
->
join
();
delete
threads
[
i
];
delete
threads
[
i
];
}
}
}
}
class
AsyncClientEnd2endTest
:
public
End2end
Test
{
class
AsyncClientEnd2endTest
:
public
::
testing
::
Test
{
protected:
protected:
AsyncClientEnd2endTest
()
:
rpcs_outstanding_
(
0
)
{}
AsyncClientEnd2endTest
()
:
rpcs_outstanding_
(
0
)
{}
void
SetUp
()
GRPC_OVERRIDE
{
common_
.
SetUp
();
}
void
TearDown
()
GRPC_OVERRIDE
{
void
TearDown
()
GRPC_OVERRIDE
{
void
*
ignored_tag
;
void
*
ignored_tag
;
bool
ignored_ok
;
bool
ignored_ok
;
while
(
cq_
.
Next
(
&
ignored_tag
,
&
ignored_ok
))
while
(
cq_
.
Next
(
&
ignored_tag
,
&
ignored_ok
))
;
;
End2endTest
::
TearDown
();
common_
.
TearDown
();
}
}
void
Wait
()
{
void
Wait
()
{
...
@@ -260,7 +275,8 @@ class AsyncClientEnd2endTest : public End2endTest {
...
@@ -260,7 +275,8 @@ class AsyncClientEnd2endTest : public End2endTest {
AsyncClientCall
*
call
=
new
AsyncClientCall
;
AsyncClientCall
*
call
=
new
AsyncClientCall
;
EchoRequest
request
;
EchoRequest
request
;
request
.
set_message
(
"Hello"
);
request
.
set_message
(
"Hello"
);
call
->
response_reader
=
stub_
->
AsyncEcho
(
&
call
->
context
,
request
,
&
cq_
);
call
->
response_reader
=
common_
.
GetStub
()
->
AsyncEcho
(
&
call
->
context
,
request
,
&
cq_
);
call
->
response_reader
->
Finish
(
&
call
->
response
,
&
call
->
status
,
call
->
response_reader
->
Finish
(
&
call
->
response
,
&
call
->
status
,
(
void
*
)
call
);
(
void
*
)
call
);
...
@@ -290,6 +306,7 @@ class AsyncClientEnd2endTest : public End2endTest {
...
@@ -290,6 +306,7 @@ class AsyncClientEnd2endTest : public End2endTest {
}
}
}
}
CommonStressTest
common_
;
CompletionQueue
cq_
;
CompletionQueue
cq_
;
std
::
mutex
mu_
;
std
::
mutex
mu_
;
std
::
condition_variable
cv_
;
std
::
condition_variable
cv_
;
...
@@ -297,27 +314,26 @@ class AsyncClientEnd2endTest : public End2endTest {
...
@@ -297,27 +314,26 @@ class AsyncClientEnd2endTest : public End2endTest {
};
};
TEST_F
(
AsyncClientEnd2endTest
,
ThreadStress
)
{
TEST_F
(
AsyncClientEnd2endTest
,
ThreadStress
)
{
ResetStub
();
common_
.
ResetStub
();
std
::
vector
<
std
::
thread
*>
threads
;
std
::
vector
<
std
::
thread
*>
send_threads
,
completion_
threads
;
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
.
push_back
(
new
std
::
thread
(
completion_
threads
.
push_back
(
new
std
::
thread
(
&
AsyncClientEnd2endTest_ThreadStress_Test
::
Async
Send
Rpc
,
this
,
1000
));
&
AsyncClientEnd2endTest_ThreadStress_Test
::
Async
Complete
Rpc
,
this
));
}
}
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
[
i
]
->
join
();
send_threads
.
push_back
(
delete
threads
[
i
];
new
std
::
thread
(
&
AsyncClientEnd2endTest_ThreadStress_Test
::
AsyncSendRpc
,
this
,
FLAGS_num_rpcs
));
}
}
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
.
clear
();
send_threads
[
i
]
->
join
();
delete
send_threads
[
i
];
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
threads
.
push_back
(
new
std
::
thread
(
&
AsyncClientEnd2endTest_ThreadStress_Test
::
AsyncCompleteRpc
,
this
));
}
}
Wait
();
Wait
();
for
(
int
i
=
0
;
i
<
100
;
++
i
)
{
for
(
int
i
=
0
;
i
<
FLAGS_num_threads
;
++
i
)
{
threads
[
i
]
->
join
();
completion_
threads
[
i
]
->
join
();
delete
threads
[
i
];
delete
completion_
threads
[
i
];
}
}
}
}
...
@@ -325,6 +341,7 @@ TEST_F(AsyncClientEnd2endTest, ThreadStress) {
...
@@ -325,6 +341,7 @@ TEST_F(AsyncClientEnd2endTest, ThreadStress) {
}
// namespace grpc
}
// namespace grpc
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
::
google
::
ParseCommandLineFlags
(
&
argc
,
&
argv
,
true
);
grpc_test_init
(
argc
,
argv
);
grpc_test_init
(
argc
,
argv
);
::
testing
::
InitGoogleTest
(
&
argc
,
argv
);
::
testing
::
InitGoogleTest
(
&
argc
,
argv
);
return
RUN_ALL_TESTS
();
return
RUN_ALL_TESTS
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment