Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Kaffeekasse 2000XT
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Lehrstuhl für Informatik 4 (Systemsoftware)
PASST
Projekte
Kaffeekasse 2000XT
Commits
1032af62
Commit
1032af62
authored
5 years ago
by
Fabian Krüger
Browse files
Options
Downloads
Patches
Plain Diff
add multithreaded stresstest
parent
83067fe5
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
store/stresstest.py
+91
-0
91 additions, 0 deletions
store/stresstest.py
store/urls.py
+1
-1
1 addition, 1 deletion
store/urls.py
store/views.py
+16
-3
16 additions, 3 deletions
store/views.py
with
108 additions
and
4 deletions
store/stresstest.py
0 → 100644
+
91
−
0
View file @
1032af62
from
threading
import
Thread
,
current_thread
,
Barrier
,
Lock
import
time
from
random
import
randint
from
store.models
import
*
from
store.backend
import
*
from
store.store_exceptions
import
*
from
math
import
ceil
,
floor
from
decimal
import
Decimal
class
StressTester
(
Thread
):
def
__init__
(
self
,
user
,
min_sleep
,
max_sleep
,
barrier
,
lock
,
iterations
):
super
().
__init__
()
self
.
min_sleep
=
min_sleep
self
.
max_sleep
=
max_sleep
self
.
barrier
=
barrier
self
.
lock
=
lock
self
.
iterations
=
iterations
self
.
user
=
user
self
.
sequence
=
[]
self
.
products
=
[]
self
.
users
=
[]
def
run
(
self
):
self
.
buildLists
()
self
.
generateSequence
()
user
=
list
(
User
.
objects
.
filter
(
id
=
self
.
user
))[
0
]
self
.
barrier
.
wait
()
actions
=
[]
for
action
in
self
.
sequence
:
if
action
==
'
buy
'
:
product
=
self
.
products
[
randint
(
0
,
len
(
self
.
products
)
-
1
)]
product
=
list
(
Product
.
objects
.
filter
(
id
=
product
))[
0
]
try
:
self
.
buy
(
product
.
id
)
except
UserNotEnoughMoney
:
charge_amount
=
Decimal
(
str
(
randint
(
ceil
(
product
.
price
),
50
))
+
"
.
"
+
str
(
randint
(
0
,
99
)))
self
.
charge
(
charge_amount
)
self
.
buy
(
product
.
id
)
actions
.
append
(
'
charged {}
'
.
format
(
charge_amount
))
actions
.
append
(
'
bought {} for {}
'
.
format
(
product
.
name
,
product
.
price
))
else
:
receiver
=
self
.
users
[
randint
(
0
,
len
(
self
.
users
)
-
1
)]
transfer_amount
=
randint
(
1
,
floor
(
user
.
money
))
try
:
self
.
transfer
(
receiver
,
transfer_amount
)
except
UserNotEnoughMoney
:
charge_amount
=
Decimal
(
str
(
randint
(
ceil
(
transfer_amount
),
50
))
+
"
.
"
+
str
(
randint
(
0
,
99
)))
self
.
charge
(
charge_amount
)
self
.
transfer
(
receiver
,
randint
(
1
,
floor
(
user
.
money
)))
actions
.
append
(
'
charged {}
'
.
format
(
charge_amount
))
actions
.
append
(
'
transfered {} to {}
'
.
format
(
transfer_amount
,
receiver
))
self
.
lock
.
acquire
()
print
(
"
Thread {} running for user
'
{}
'
(id: {}):
"
.
format
(
current_thread
().
ident
,
user
.
nickname
,
user
.
id
),
actions
)
self
.
lock
.
release
()
def
buildLists
(
self
):
users
=
User
.
objects
.
exclude
(
id
=
self
.
user
).
values
(
'
id
'
)
self
.
users
=
[
u
[
'
id
'
]
for
u
in
users
]
products
=
Product
.
objects
.
all
().
values
(
'
id
'
)
self
.
products
=
[
p
[
'
id
'
]
for
p
in
products
]
def
generateSequence
(
self
):
for
i
in
range
(
0
,
self
.
iterations
):
if
randint
(
0
,
4
)
<=
2
:
self
.
sequence
.
append
(
'
buy
'
)
else
:
self
.
sequence
.
append
(
'
transfer
'
)
def
buy
(
self
,
product
):
token
=
TokenLogic
.
get_token
()
self
.
wait
()
PurchaseLogic
.
purchase
(
self
.
user
,
product
,
token
)
self
.
wait
()
def
transfer
(
self
,
receiver
,
amount
):
token
=
TokenLogic
.
get_token
()
self
.
wait
()
TransferLogic
.
transfer
(
self
.
user
,
receiver
,
amount
,
token
)
self
.
wait
()
def
charge
(
self
,
amount
):
token
=
TokenLogic
.
get_token
()
self
.
wait
()
ChargeLogic
.
charge
(
self
.
user
,
amount
,
token
)
self
.
wait
()
def
wait
(
self
):
time
.
sleep
(
randint
(
self
.
min_sleep
,
self
.
max_sleep
+
1
))
This diff is collapsed.
Click to expand it.
store/urls.py
+
1
−
1
View file @
1032af62
...
@@ -23,6 +23,6 @@ urlpatterns = [
...
@@ -23,6 +23,6 @@ urlpatterns = [
path
(
'
revert_transfer
'
,
views
.
revert_transfer
,
name
=
"
revert_transfer
"
),
path
(
'
revert_transfer
'
,
views
.
revert_transfer
,
name
=
"
revert_transfer
"
),
# Test
# Test
path
(
'
test
'
,
views
.
t
est
,
name
=
"
test
"
),
path
(
'
stress
test
'
,
views
.
stressT
est
,
name
=
"
stress
test
"
),
path
(
'
test2
'
,
views
.
test2
,
name
=
"
test2
"
),
path
(
'
test2
'
,
views
.
test2
,
name
=
"
test2
"
),
]
]
This diff is collapsed.
Click to expand it.
store/views.py
+
16
−
3
View file @
1032af62
...
@@ -7,6 +7,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
...
@@ -7,6 +7,7 @@ from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from
django.contrib.auth
import
logout
as
auth_logout
from
django.contrib.auth
import
logout
as
auth_logout
from
.backend
import
*
from
.backend
import
*
from
.store_exceptions
import
*
from
.store_exceptions
import
*
from
.stresstest
import
*
# Rendered pages
# Rendered pages
...
@@ -183,13 +184,25 @@ def revert_transfer(request):
...
@@ -183,13 +184,25 @@ def revert_transfer(request):
# Test
# Test
def
test
(
request
):
def
stressTest
(
request
):
return
render
(
request
,
"
test.html
"
,
{})
nthreads
=
10
barrier
=
Barrier
(
nthreads
+
1
)
lock
=
Lock
()
for
i
in
range
(
0
,
nthreads
):
st
=
StressTester
(
i
+
1
,
0
,
4
,
barrier
,
lock
,
60
)
st
.
start
()
def
test2
(
request
):
tmp
=
input
(
"
press any key to start
"
)
barrier
.
wait
()
def
test
(
request
):
transfers
=
TransferLogic
.
getFreuquentTransferTargeds
(
1
)
transfers
=
TransferLogic
.
getFreuquentTransferTargeds
(
1
)
for
transfer
in
transfers
:
for
transfer
in
transfers
:
print
(
transfer
)
print
(
transfer
)
print
(
"
n_transfers:
"
,
len
(
transfers
))
print
(
"
n_transfers:
"
,
len
(
transfers
))
def
test2
(
request
):
pass
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment