Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
7437a23b
Commit
7437a23b
authored
Mar 14, 2019
by
knowledge4igor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix issue #473
parent
43bdf4eb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
15 deletions
+56
-15
tests/streaming_test.cc
tests/streaming_test.cc
+56
-15
No files found.
tests/streaming_test.cc
View file @
7437a23b
...
...
@@ -8,37 +8,78 @@
#include <curl/curl.h>
#include <curl/easy.h>
#include <vector>
#include <queue>
#include <mutex>
#include <thread>
using
namespace
std
;
using
namespace
Pistache
;
static
const
size_t
N_LETTERS
=
26
;
static
const
size_t
LETTER_REPEATS
=
100000
;
static
const
size_t
SET_REPEATS
=
10
;
static
constexpr
size_t
N_LETTERS
=
26
;
static
constexpr
size_t
LETTER_REPEATS
=
100000
;
static
constexpr
size_t
SET_REPEATS
=
10
;
static
constexpr
size_t
N_WORKERS
=
10
;
void
dumpData
(
const
Rest
::
Request
&
req
,
Http
::
ResponseWriter
response
)
{
UNUSED
(
req
);
auto
stream
=
response
.
stream
(
Http
::
Code
::
Ok
);
char
letter
=
'A'
;
void
dumpData
(
const
Rest
::
Request
&
/*req*/
,
Http
::
ResponseWriter
response
)
{
using
Lock
=
std
::
mutex
;
using
Guard
=
std
::
lock_guard
<
Lock
>
;
std
::
mutex
responseGuard
;
Lock
responseLock
;
std
::
vector
<
std
::
thread
>
workers
;
std
::
condition_variable
cv
;
std
::
queue
<
std
::
function
<
void
()
>>
jobs
;
Lock
jobLock
;
std
::
atomic
<
size_t
>
jobCounter
(
0
);
constexpr
size_t
JOB_LIMIT
=
SET_REPEATS
*
N_LETTERS
;
for
(
size_t
j
=
0
;
j
<
N_WORKERS
;
++
j
)
{
workers
.
push_back
(
std
::
thread
([
&
jobCounter
,
&
cv
,
&
jobLock
,
&
jobs
]()
{
while
(
jobCounter
<
JOB_LIMIT
)
{
std
::
unique_lock
<
Lock
>
l
(
jobLock
);
cv
.
wait
(
l
,
[
&
jobCounter
,
&
jobs
]{
return
jobs
.
size
()
||
!
(
jobCounter
<
JOB_LIMIT
);});
if
(
!
jobs
.
empty
())
{
auto
f
=
std
::
move
(
jobs
.
front
());
jobs
.
pop
();
l
.
unlock
();
f
();
++
jobCounter
;
l
.
lock
();
}
}
cv
.
notify_all
();
}));
}
auto
stream
=
response
.
stream
(
Http
::
Code
::
Ok
);
const
char
letter
=
'A'
;
for
(
size_t
s
=
0
;
s
<
SET_REPEATS
;
++
s
)
{
for
(
size_t
i
=
0
;
i
<
N_LETTERS
;
++
i
)
{
std
::
thread
job
([
&
,
i
]()
->
void
{
const
size_t
nchunks
=
10
;
size_t
chunk_size
=
LETTER_REPEATS
/
nchunks
;
std
::
string
payload
(
chunk_size
,
letter
+
i
);
auto
job
=
[
&
stream
,
&
responseLock
,
i
]()
->
void
{
const
expr
size_t
nchunks
=
10
;
constexpr
size_t
chunk_size
=
LETTER_REPEATS
/
nchunks
;
const
std
::
string
payload
(
chunk_size
,
letter
+
i
);
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
responseGuard
);
Guard
guard
(
responseLock
);
for
(
size_t
chunk
=
0
;
chunk
<
nchunks
;
++
chunk
)
{
stream
.
write
(
payload
.
c_str
(),
chunk_size
);
stream
.
flush
();
}
}
});
workers
.
push_back
(
std
::
move
(
job
));
};
std
::
unique_lock
<
Lock
>
l
(
jobLock
);
jobs
.
push
(
std
::
move
(
job
));
l
.
unlock
();
cv
.
notify_all
();
}
}
...
...
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