Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dfaast
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
Santhosh Kumar
dfaast
Commits
89f6ea52
Commit
89f6ea52
authored
May 03, 2024
by
Santhosh Kumar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
my updates
parent
bd451927
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
1 deletion
+64
-1
faas/example2/.function.py.swp
faas/example2/.function.py.swp
+0
-0
faas/example2/function.py
faas/example2/function.py
+64
-1
No files found.
faas/example2/.function.py.swp
0 → 100644
View file @
89f6ea52
File added
faas/example2/function.py
View file @
89f6ea52
from
flask
import
Flask
from
flask
import
Flask
,
request
,
jsonify
import
ctypes
from
prometheus_client
import
Counter
,
Gauge
,
start_http_server
from
prometheus_client
import
generate_latest
,
CONTENT_TYPE_LATEST
,
CollectorRegistry
import
psutil
import
time
import
multiprocessing
app
=
Flask
(
__name__
)
...
...
@@ -28,6 +31,66 @@ def clear_metrics():
registry
=
CollectorRegistry
()
return
generate_latest
(
registry
),
200
,
{
"Content-Type"
:
CONTENT_TYPE_LATEST
}
cpu_process
=
None
def
occupy_cpu
(
percentage
):
while
True
:
# Start time
start_time
=
time
.
time
()
# Perform CPU-bound task
while
(
time
.
time
()
-
start_time
)
<
(
percentage
/
100
):
pass
# Sleep to balance CPU usage
time
.
sleep
(
1
-
(
percentage
/
100
))
@
app
.
route
(
'/occupy_cpu/<int:percentage>'
,
methods
=
[
'GET'
])
def
start_cpu_occupier
(
percentage
):
global
cpu_process
if
0
<=
percentage
<=
100
:
# Kill previous process if exists
if
cpu_process
and
cpu_process
.
is_alive
():
cpu_process
.
terminate
()
# Create a new process for occupying CPU
cpu_process
=
multiprocessing
.
Process
(
target
=
occupy_cpu
,
args
=
(
percentage
,))
cpu_process
.
start
()
return
jsonify
({
'message'
:
f
'CPU is being occupied at {percentage}
%
'
}),
200
else
:
return
jsonify
({
'error'
:
'Invalid percentage, must be between 0 and 100'
}),
400
memory_process
=
None
def
allocate_memory
(
memory_size
):
try
:
memory_size
=
int
(
memory_size
)
*
1024
*
1024
except
ValueError
:
return
jsonify
({
'error'
:
'Invalid memory size'
}),
400
if
memory_size
<=
0
:
return
jsonify
({
'error'
:
'Memory size must be greater than 0'
}),
400
# Allocate memory of specified size
ptr
=
ctypes
.
c_char
*
memory_size
memory_block
=
ptr
()
return
jsonify
({
'message'
:
'Memory allocated successfully'
}),
200
@
app
.
route
(
'/allocate_memory/<int:memory_size>'
,
methods
=
[
'GET'
])
def
start_memory_allocator
(
memory_size
):
global
memory_process
# Kill previous process if exists
if
memory_process
and
memory_process
.
is_alive
():
memory_process
.
terminate
()
# Create a new process for memory allocation
memory_process
=
multiprocessing
.
Process
(
target
=
allocate_memory
,
args
=
(
memory_size
,))
memory_process
.
start
()
return
jsonify
({
'message'
:
f
'Memory allocated with size {memory_size}'
}),
200
@
app
.
route
(
"/"
)
def
hello
():
...
...
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