Skip to content

Commit a910222

Browse files
committed
add Azure hellopy-read-random raw-code
1 parent d29d917 commit a910222

File tree

1 file changed

+52
-0
lines changed
  • src/setup/deployment/raw-code/serverless/azure/hellopy-read-random

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
import os
3+
import time
4+
import random
5+
6+
import azure.functions as func
7+
8+
9+
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
10+
incr_limit = int(req.params.get('IncrementLimit')) if req.params.get('IncrementLimit') else None
11+
if not incr_limit:
12+
try:
13+
req_body = req.get_json()
14+
except ValueError:
15+
incr_limit = 0
16+
pass
17+
else:
18+
incr_limit = int(req_body.get('IncrementLimit')) if req_body.get('IncrementLimit') else 0
19+
else:
20+
incr_limit = 0
21+
22+
simulate_work(incr_limit)
23+
read_filler_file(f"{context.function_directory}/../filler.file")
24+
25+
return func.HttpResponse(
26+
body=json.dumps({
27+
"RequestID": context.invocation_id,
28+
"TimestampChain": [str(time.time_ns())]
29+
}, indent=4),
30+
status_code=200,
31+
headers={
32+
"Content-Type": "application/json"
33+
}
34+
)
35+
36+
37+
def simulate_work(increment):
38+
# MAXNUM = 6103705
39+
num = 0
40+
while num < increment:
41+
num += 1
42+
43+
44+
def read_filler_file(path: str) -> None:
45+
file_size = os.stat(path).st_size
46+
number_of_pages = file_size // 4096
47+
with open(path, 'rb') as f:
48+
for _ in range(100):
49+
page_number = random.randrange(0, number_of_pages)
50+
page_offset = page_number * 4096
51+
f.seek(page_offset)
52+
f.read(1)

0 commit comments

Comments
 (0)