Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-02-ExecutorService
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohamadbashar.disoki
PP-02-ExecutorService
Commits
addea042
Commit
addea042
authored
Nov 01, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
LoadTester Example
parent
75718739
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
0 deletions
+57
-0
RestApiLoadTester.java
src/main/java/RestApiLoadTester.java
+57
-0
No files found.
src/main/java/RestApiLoadTester.java
0 → 100644
View file @
addea042
import
java.io.IOException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
okhttp3.*
;
public
class
RestApiLoadTester
{
public
static
void
main
(
String
[]
args
){
String
url
=
"http://172.29.3.203:30152/hello/fib/30"
;
String
headers
=
"content-type: application/json\n"
+
"user-agent: Mozilla/5.0"
;
int
threadCount
=
5
;
int
callCountPerThread
=
2
;
RestApiLoadTester
.
runLoadTest
(
url
,
headers
,
threadCount
,
callCountPerThread
);
}
public
static
void
runLoadTest
(
String
url
,
String
headers
,
int
threadCount
,
int
callCountPerThread
)
{
OkHttpClient
httpClient
=
new
OkHttpClient
();
// MediaType mediaType = MediaType.parse("application/json");
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
for
(
int
i
=
0
;
i
<
threadCount
;
i
++)
{
executor
.
submit
(()
->
{
for
(
int
j
=
0
;
j
<
callCountPerThread
;
j
++)
{
Request
.
Builder
requestBuilder
=
new
Request
.
Builder
()
.
url
(
url
);
// add headers
for
(
String
header
:
headers
.
split
(
"\n"
))
{
String
[]
headerParts
=
header
.
split
(
": "
);
requestBuilder
.
addHeader
(
headerParts
[
0
],
headerParts
[
1
]);
}
// add request body
// RequestBody requestBodyObj = RequestBody.create(requestBody, mediaType);
Request
request
=
requestBuilder
// .post(requestBodyObj)
.
build
();
try
{
Response
response
=
httpClient
.
newCall
(
request
).
execute
();
System
.
out
.
println
(
response
.
body
().
string
());
response
.
body
().
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
});
}
executor
.
shutdown
();
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment