-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataStream.nim
67 lines (58 loc) · 1.6 KB
/
dataStream.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
## This sends 1GB to the local server
## run: nim c -r -d:release examples/localServer.nim
## to start the server and then run this one
{.define: ssl.}
import std/strutils
import std/asyncdispatch
import ../src/hyperx/client
from ./localServer import localHost, localPort
const dataSizeMb = 1000
# do not change:
const dataSize = dataSizeMb * 1024 * 1024
const frmSize = 16 * 1024
doAssert dataSize mod frmSize == 0
const chunks = dataSize div frmSize
when isMainModule:
var dataSentSize = 0
var dataRecvSize = 0
proc send(
strm: ClientStream, path: string
) {.async.} =
await strm.sendHeaders(
hmPost, path,
contentLen = dataSize
)
var data = new string
for _ in 0 .. frmSize-1:
data[].add 'a'
for i in 0 .. chunks-1:
await strm.sendBody(data, finish = i == chunks-1)
dataSentSize += data[].len
proc recv(
strm: ClientStream
) {.async.} =
var data = new string
await strm.recvHeaders(data)
doAssert ":status:" in data[]
while not strm.recvEnded:
data[].setLen 0
await strm.recvBody(data)
dataRecvSize += data[].len
proc streamChunks(
client: ClientContext, path: string
) {.async.} =
let strm = client.newClientStream()
with strm:
let recvFut = strm.recv()
let sendFut = strm.send(path)
await recvFut
await sendFut
proc main() {.async.} =
var client = newClient(localHost, localPort)
with client:
await streamChunks(client, "/foo")
waitFor main()
doAssert not hasPendingOperations()
doAssert dataSentSize == dataSize
doAssert dataRecvSize == dataSize
echo "ok"