interplane.Get Omega
Read in your language

cortex

aien-dev/cortex

Brought by drakestapleton

branch main Branches Tags History

cortex / tests / conc / slow.om

209 lines · 3741 bytes Raw Blame History
1note slow.om: slow-client (slowloris) driver. usage: slow PORT N SECS OUT
2note Opens N connections to 127.0.0.1:PORT, sends an incomplete request head ("GET /health
3note HTTP/1.1" and one header line) and then one more header byte per connection every second,
4note never finishing the head. Stops each connection when the server answers or closes it, or
5note when SECS seconds have passed. OUT gets one line per connection: "<index> <ns alive> <how>"
6note with how = closed (server closed without answering), answered:<first 12 answer bytes>, or
7note open (still open at SECS). Exit 0 when every connection was opened.
8global pa uchar 4096
9global sa uchar 16
10global ob uchar 128
11global rb uchar 4096
12note per connection: fd, start ns, end ns, state (0 open, 1 closed, 2 answered, 3 never opened)
13global cfd ulong 1024
14global cst ulong 1024
15global cen ulong 1024
16global cmo ulong 1024
17global ans uchar 12288
18global sv ulong 4
19text u_use "usage: slow PORT N SECS OUT\n"
20text h_req "GET /health HTTP/1.1\nhost: 127.0.0.1\nx-slow: "
21text w_cl " closed\n"
22text w_op " open\n"
23text w_an " answered:"
24fn say t view uchar * to void
25w = write 2 t 0 len t
26if lt w 0
27ret
28end
29end
30fn argn i ulong to ulong
31n = argread i pa 0 len pa
32if le n 0
33ret 0
34end
35v = ulong 0
36for k ulong ulong n
37c = get pa k
38if or lt c 48 gt c 57
39ret 0
40end
41v = add mul v 10 ulong sub c 48
42end
43ret v
44end
45fn obdec o ulong v ulong to ulong
46k = o
47if ge v 10
48k = obdec o div v 10
49end
50put ob k uchar add rem v 10 48
51ret add k 1
52end
53fn opn port ulong to int
54fd = socknew 2 524289 0
55if lt fd 0
56ret -1
57end
58put sa 0 2
59put sa 1 0
60put sa 2 uchar shr port 8
61put sa 3 uchar and port 255
62put sa 4 127
63put sa 5 0
64put sa 6 0
65put sa 7 1
66r = connect int fd sa 0 16
67if ne r 0
68x = fclose int fd
69ret -1
70end
71s = send int fd h_req 0 len h_req 0
72if ne s long len h_req
73x = fclose int fd
74ret -1
75end
76ret int fd
77end
78note look at connection i once: answer, close, or still waiting; true while still open
79fn poll1 i ulong to bool
80fd = int get cfd i
81w = waitio fd 1 0
82if le w 0
83ret true
84end
85r = recv fd rb 0 12 0
86put cen i ulong monons
87x = fclose fd
88if le r 0
89put cmo i 1
90ret false
91end
92put cmo i 2
93for k ulong ulong r
94put ans add mul i 12 k get rb k
95end
96ret false
97end
98fn drip i ulong to void
99put rb 0 120
100s = send int get cfd i rb 0 1 0
101if le s 0
102put cen i ulong monons
103put cmo i 1
104x = fclose int get cfd i
105end
106end
107fn emitln ofd int i ulong to void
108o = obdec 0 i
109put ob o 32
110o = add o 1
111o = obdec o sub get cen i get cst i
112m = get cmo i
113if eq m 1
114for k ulong len w_cl
115put ob add o k get w_cl k
116end
117o = add o len w_cl
118end
119if eq m 0
120for k ulong len w_op
121put ob add o k get w_op k
122end
123o = add o len w_op
124end
125if eq m 2
126for k ulong len w_an
127put ob add o k get w_an k
128end
129o = add o len w_an
130for k ulong 12
131c = get ans add mul i 12 k
132if or lt c 32 gt c 126
133c = 46
134end
135put ob add o k c
136end
137o = add o 12
138put ob o 10
139o = add o 1
140end
141w = write ofd ob 0 o
142end
143fn main to int
144port = argn 1
145n = argn 2
146secs = argn 3
147if or or eq port 0 eq n 0 gt n 1024
148say u_use
149ret 2
150end
151q = argread 4 pa 0 len pa
152if le q 0
153say u_use
154ret 2
155end
156ofd = fopen pa 0 ulong q 524865 420
157if lt ofd 0
158say u_use
159ret 2
160end
161bad = ulong 0
162for i ulong n
163put cst i ulong monons
164f = opn port
165if lt f 0
166put cmo i 3
167put cen i ulong monons
168bad = add bad 1
169else
170put cfd i ulong f
171put cmo i 0
172end
173end
174dl = add monons mul long secs 1000000000
175nx = add monons 1000000000
176while lt monons dl bound 999999
177live = ulong 0
178for i ulong n
179if eq get cmo i 0
180if poll1 i
181live = add live 1
182end
183end
184end
185if eq live 0
186break
187end
188if ge monons nx
189for i ulong n
190if eq get cmo i 0
191drip i
192end
193end
194nx = add nx 1000000000
195end
196x = sleepns 20000000
197end
198for i ulong n
199if eq get cmo i 0
200put cen i ulong monons
201x = fclose int get cfd i
202end
203emitln int ofd i
204end
205if gt bad 0
206ret 1
207end
208ret 0
209end