Locust - Shape Class ignored when running Web UI with --class-picker - python

I saw that locust supports defining different user classes and shape classes and that it is possible to choose which one to run through the web-ui when locust is started with --class-picker
However, when I select a user class and a shape class, locust doesn't use the shape and uses only what I type in the "Number of Users" and "Spawn rate" fields on the interface.
How can I make locust use the selected shape file?
This is how my user classes look: (the other user classes are very similar to this one)
from locust import HttpUser, TaskSet, task
from locust import LoadTestShape
import csv
import random
class Tasks1(TaskSet):
DATA = {}
def on_start(self):
if not self.DATA:
self._get_pics()
def _get_pics(self):
with open('targets.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',', quotechar="'")
for row in csv_reader:
self.DATA['images/' + row[0]] = '{"x":' + row[1] + ',"y":' + \
row[2] + ',"width":' + row[3] + ',"height":' + row[4] + '}'
#task
def service(self):
key = random.choice(list(self.DATA.keys()))
payload = {self.DATA[key]}
with open(key, 'rb') as image:
self.client.post('/process', headers={'Authorization': 'Bearer ' + token, 'Accept-Encoding': 'gzip'}, files={'image': image}, data=payload)
#task
def stop(self):
self.interrupt()
class FirstUser(HttpUser):
tasks = [Tasks1]
And these are the shape classes:
ShapeClass1.py:
from locust import LoadTestShape
class ramp_up_20min_40users_hold_5min_ramp_down_5min_1user(LoadTestShape):
stages = [
{"duration": 1200, "users": 40, "spawn_rate": 0.034},
{"duration": 300, "users": 40, "spawn_rate": 1},
{"duration": 300, "users": 1, "spawn_rate": 0.0325}
]
def tick(self):
run_time = self.get_run_time()
for stage in self.stages:
if run_time < stage["duration"]:
try:
tick_data = (
stage["users"], stage["spawn_rate"], stage["user_classes"])
except:
tick_data = (stage["users"], stage["spawn_rate"])
return tick_data
return None
ShapeClass2.py:
from locust import LoadTestShape
class ramp_up_1min_40users_hold_20min(LoadTestShape):
stages = [
{"duration": 60, "users": 40, "spawn_rate": 0.67},
{"duration": 1200, "users": 40, "spawn_rate": 1},
]
def tick(self):
run_time = self.get_run_time()
for stage in self.stages:
if run_time < stage["duration"]:
try:
tick_data = (
stage["users"], stage["spawn_rate"], stage["user_classes"])
except:
tick_data = (stage["users"], stage["spawn_rate"])
return tick_data
return None
I can see the choices in the UI.
However, the Number of users and spawn rate field are not disabled and locust executes what I type in there and not what is defined in the load shape files.
Am I missing something here?

Related

How to use mocking to compare real results

I have a sample class which reads a saved Tensorflow model and runs predictions
class Sample():
## all it does is creates a new column with predictions
def __init__(self, tf_model):
self.tf_model = tf_model
def tf_process(self, x):
##some other preprocessing
x["tf_predictions"] = self.tf_model.predict(x)
return x
def predict(self, x):
predictions = self.tf_process(x)
return predictions
Code for Unittesting without having to load the model:
import unittest
import pandas as pd
from unittest import TestCase, mock
from my_package.sample_model import Sample
class TestSample(unittest.TestCase):
def test_predict(self):
with mock.patch("Sample.tf_process") as process:
process.return_value = pd.DataFrame("hardcoded_value")
#to check: process.return_value = Output (Sample.predict())
Goal:
To compare process.return_value with the Output of predict method in Sample, but to do this I still have to load the model, I dont understand what is the use of mock here since i will have to anyway call the predict method to compare it with process.return_value. Any suggestions will be helpful
I think in your case it's better to use Mock(). You can create really good and simple tests without patch(). Just prepare all necessary mocked instances for initialization.
from unittest.mock import Mock
class TestSample(TestCase):
def test_predict(self):
# let's say predict() will return something... just an example
tf = Mock(predict=Mock(return_value=(10, 20, 30)))
df = pd.DataFrame({'test_col': (1, 2, 3)})
df = Sample(tf).predict(df)
# check column
self.assertTrue('tf_predictions' in df.columns)
# or check records
self.assertEqual(
df.to_dict('records'),
[
{'test_col': 1, 'tf_predictions': 10},
{'test_col': 2, 'tf_predictions': 20},
{'test_col': 3, 'tf_predictions': 30}
]
)
Also it's really helps when you need tests for complex services. Just an example:
class ClusterService:
def __init__(self, service_a, service_b, service_c) -> None:
self._service_a = service_a
self._service_b = service_b
self._service_c = service_c
# service_d, ... etc
def get_cluster_info(self, name: str):
self._service_a.send_something_to_somewhere(name)
data = {
'name': name,
'free_resources': self._service_b.get_free_resources(),
'current_price': self._service_c.get_price(name),
}
return ' ,'.join([
': '.join(['Cluster name', name]),
': '.join(['CPU', str(data['free_resources']['cpu'])]),
': '.join(['RAM', str(data['free_resources']['ram'])]),
': '.join(['Price', '{} $'.format(round(data['current_price']['usd'], 2))]),
])
class TestClusterService(TestCase):
def test_get_cluster_info(self):
cluster = ClusterService(
service_a=Mock(),
service_b=Mock(get_free_resources=Mock(return_value={'cpu': 100, 'ram': 200})),
service_c=Mock(get_price=Mock(return_value={'usd': 101.4999})),
)
self.assertEqual(
cluster.get_cluster_info('best name'),
'Cluster name: best name ,CPU: 100 ,RAM: 200 ,Price: 101.5 $'
)

Cannot Export NDVI Layer To Google Drive

I wish to export NDVI layer as image to my Google Drive. I can run the code seamlessly but nothing is exported into my drive after the code is executed.
Here is my code:
import ee
import ee.mapclient
import datetime
import ee.batch as batch
ee.Initialize()
roi_lat = 14.82762
roi_lon = 101.079659
ullat = roi_lat + 0.01
ullon = roi_lon - 0.01
lrlat = roi_lat - 0.01
lrlon = roi_lon + 0.01
geometry = ([ullon,ullat], [lrlon, ullat],[lrlon, lrlat],[ullon, lrlat])
l8 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
image = ee.Image(
(l8.filterBounds(point)
.filterDate(datetime.datetime(2018, 1, 1), datetime.datetime(2018, 12, 31))
.sort('CLOUD_COVER')
.first()
)
)
def NDVI(image):
return image.expression('float(b("B5") - b("B4")) / (b("B5") + b("B4"))')
ndvi = l8.map(NDVI).mean()
visualization = ndvi.visualize({
'min': 0,
'max': 1,
'palette': ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163',
'99B718', '74A901', '66A000', '529400', '3E8601',
'207401', '056201', '004C00', '023B01', '012E01',
'011D01', '011301']
})
task_config = {
'description': 'NDVI',
'scale': 30,
'region':geometry
}
print("Starting to create a image")
out = ee.batch.Export.image(visualization,'l8_ndvi', task_config)
process = batch.Task.start(out)
print("Process sent to cloud")
If anyone has any knowledge. Please enlighten me where did I go wrong?
Thanks.
What you want is out = ee.batch.Export.image.toDrive(...). You can start that with out.start(). You can monitor tasks with ee.batch.Task.list() or use the Tasks tab of the Code Editor.

Why is Django shell running a function using not the inputted parameters?

I have a function that is supposed to update Django objects, which each have two date fields, named "production" and "development". The function reads from a python object, containing a list of Django objects along with their new dates, as well as a single label indicating for the entire list whether the fields to be updated are "production" or "development".
For testing, I have two python objects, containing two different lists of django objects and dates, one for "production" and one for "development". When I call the function on one of these objects, no matter what, it's the list and dates in the "production" object that get updated, though whether the info goes to the "development" or "production" fields in those django objects still depends on the label of the initial python object being called on. I have no idea why this is happening.
Python class and initialization of two python objects:
import pytz
import datetime
class timess():
#Reflects update statuses for the two servers
lists = {
"none" : [],
"uploaded" : [],
}
servername = ""
def __init__(self, nonay, uplay, servertype):
self.lists["none"] = nonay
self.lists["uploaded"] = uplay
self.servername = servertype
timezone = pytz.timezone("America/Chicago")
d1 = datetime.datetime(1999, 8, 12, 12, 32, 41)
d2 = datetime.datetime(1996, 8, 12, 12, 32, 41)
d3 = datetime.datetime(2004, 8, 12, 12, 32, 41)
d1a = timezone.localize(d1)
d2a = timezone.localize(d2)
d3a = timezone.localize(d3)
p1 = datetime.datetime(1997, 8, 12, 12, 32, 41)
p2 = datetime.datetime(2002, 8, 12, 12, 32, 41)
p3 = datetime.datetime(2006, 8, 12, 12, 32, 41)
p4 = datetime.datetime(1992, 8, 12, 12, 32, 41)
p1a = timezone.localize(p1)
p2a = timezone.localize(p2)
p3a = timezone.localize(p3)
p4a = timezone.localize(p4)
dnonay = [
("mvol/0004/1905/0404", None),
("mvol/0004/1920/1111", None),
("mvol/0004/1930/0812", None),
("mvol/0004/1905/0214", None),
("mvol/0004/1930/1001", None),
("mvol/0004/1905/0917", None),
("mvol/0004/1930/0712", None),
("mvol/0004/1920/1202", None),
]
duplay = [
("mvol/0004/1905/0130", d1a),
("mvol/0004/1920/0624", d2a),
("mvol/0004/1930/0311", d3a),
]
dtimess = timess(dnonay, duplay, "development")
pnonay = [
("mvol/0004/1905/0130", None),
("mvol/0004/1920/1111", None),
("mvol/0004/1905/0214", None),
("mvol/0004/1930/0311", None),
("mvol/0004/1905/0917", None),
("mvol/0004/1930/0712", None),
("mvol/0004/1920/0624", None)
]
puplay = [
("mvol/0004/1905/0404", p1a),
("mvol/0004/1920/1202", p2a),
("mvol/0004/1930/0812", p3a),
("mvol/0004/1930/1001", p4a),
]
ptimess = timess(pnonay, puplay, "production")
Function definitions:
from listpage.timess import *
from listpage.models import *
def integratetimess(timess):
'''
Takes information in server update list and applies it
to mvolFolder objects in site. Does not work properly.
'''
if timess.servername == "development":
for line in timess.lists["uploaded"]:
target = mvolFolder.objects.get(name = line[0])
target.dev = line[1]
target.save()
if timess.servername == "production":
for line in timess.lists["uploaded"]:
target = mvolFolder.objects.get(name = line[0])
target.pro = line[1]
target.save()
def integrateot(namey, date, servertype):
'''
This function works when called on
a django mvolFolder object outside of the
timess object
'''
target = mvolFolder.objects.get(name = namey)
if(servertype == "development"):
print(namey)
print("development, initially:")
print(target.dev)
print(target.pro)
target.dev = date
print("development, after changing")
print(target.dev)
print(target.pro)
target.save()
if(servertype == "production"):
print(namey)
print("production, initially:")
print(target.dev)
print(target.pro)
target.pro = date
print("production, after changing")
print(target.dev)
print(target.pro)
target.save()
def integratetimesx(timess):
'''
However, when integrateot is called to loop on the
list in the python object, there's the same issue
as the first function
'''
for line in timess.lists["uploaded"]:
integrateot(line[0], line[1], timess.servername)
Django object model:
from django.db import models
# Create your models here.
class Folder(models.Model):
name = models.CharField(max_length = 19)
def __str__(self):
return self.name
class mvolFolder(Folder):
date = models.DateTimeField(null = True)
valid = models.NullBooleanField(null = True)
dev = models.DateTimeField(null = True)
pro = models.DateTimeField(null = True)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null = True, related_name = 'children')
Django shell input:
from listpage.models import mvolFolder
from listpage.timess import *
from listpage.convtimess import *
integratetimesx(ptimess)
integratetimesx(dtimess)
Python 3.7.0
Django 2.0.8
Windows 10 Enterprise Version 1803
I could be wrong, the question is a little vague and you never give an exact description of the problem, but it looks to me to be a python problem not a django one. The timess class has a class attribute called lists which you are treating as an instance attribute. Here's a concise description of the difference in a similar case. For a timess instance, self.list is a local reference to the class dictionary. Calling timess(...) changes the value in the class dictionary, so it's changed for all instances. I won't comment on the rest of the code, but to make this class work as (I think) you expect, try this instead:
class timess():
def __init__(self, nonay, uplay, servertype):
self.lists = {
"none" : [nonay],
"uploaded" : [uplay],
}
self.servername = servertype

Building python dict using multithreading

I'm currently building a Tool that generates a PDF report using Python pdfkit and jinja.
This report builds on a lot of static html and around 30 functions that produces data and images (charts) for the report. These functions all access external data through pyodbc or pandas from_sql.
I am now running into performance issues, and the report takes around 5 minutes to build.
I'm hoping to utilize multithreading in order to build a dictionary of data, but have not been able to figure out how to approach the issue.
My current code looks something like this.
def buildReport():
if checkKvaegCVR(SQL = checkKvaegCVRSQL(cvrNummer = cvrNummer), cursor = OEDBCursor):
env = Environment(loader=FileSystemLoader('.'))
template = env.get_template("templates/kvaeg/kvaegBase.html")
pdfOptions = {
'page-size': 'A4',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'quiet': '',
'encoding': "UTF-8",
'footer-right': '[page]'
}
css = 'static/css/style.css'
template_vars = {'kvaegForsideBillede': imageBuilder()['kvaegForsideBillede'],
'bagsideBillede': imageBuilder()['bagsideBillede'],
'navn' : bedriftAdresse(cvrNummer = cvrNummer,
cursor = KundeAnalyseDBCursor)[0],
'adresse' : bedriftAdresse(cvrNummer = cvrNummer,
cursor = KundeAnalyseDBCursor)[1],
'postnrBy' : str(int(bedriftAdresse(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor)[2])) + ' ' +
bedriftAdresse(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor)[3],
'fremstillingsprisKorr': imageBuilder()['fremstillingsprisKorr'],
'fremstillingsprisForbedring':imageBuilder()['fremstillingsprisForbedring'],
'graesoptagelse':kgGraesPrKo(),
'indreSaedskifteKort':indreSaedskifteKortPNG(CVRPunkt = CVRPunkt(cvrNummer, KundeAnalyseDBCursor),
CVRBuffer = CVRBuffer(cvrNummer, KundeAnalyseDBCursor),
indreSaedskifteKort = indreSaedskifteKort(indreSaedskifteKortSQL = indreSaedskifteKortSQL(cvrNummer = cvrNummer), cursor = KundeAnalyseDBCursor)),
'naboKort':naboKortPNG(CVRPunkt = CVRPunkt(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor),
CVRBuffer = CVRBuffer(cvrNummer = cvrNummer, cursor = KundeAnalyseDBCursor),
naboKort = naboKort(naboMarkerSQL = naboMarkerSQL(cvrNummer = cvrNummer),
egneMarkerSQL = egneMarkerSQL(cvrNummer = cvrNummer),
cursor = KundeAnalyseDBCursor))
...
...
30 more functions here
...
...}
pdfkit.from_string(template.render(template_vars), 'KvaegRapport - {}.pdf'.format(cvrNummer), options=pdfOptions, css=css)
print('Rapporten er klar')
else:
print('Kan ikke bygge rapport på dette CVR nummer')
I would like to build the dictionary "Template_vars" using multithreading (probably outside my main function)
Any suggestions?
I could suggest following but with multiprocessing (the following code has not been tested):
from multiprocessing import Process, Queue
def make_smth(func, queue, name, *args, **kwargs):
queue.put((name, func(*args, **kwargs)))
result_queue = Queue()
processes = list()
processes.append(
Process(target=make_smth,
args=(bedriftAdresse, result_queue, "navn"),
kwargs={cvrNummer: cvrNummer, cursor: KundeAnalyseDBCursor[0]}
)
)
processes.append(
Process(target=make_smth,
args=(kgGraesPrKo, result_queue, "graesoptagelse"),
kwargs={}
)
)
#...... You should do it for each of your functions
for p in processes:
p.start()
template_vars = {}
result = result_queue.get()
while result:
template_vars[result[0]] = result[1]
result = result_queue.get()

Blender 3D Add-on Script loading failed

Heading
I made add-on script.
However loading fail in blender UI.
Error message is '_RestrictContext' object has no attribute 'scene' .
But this script is very well run in blender of Text Editor.
Why don't load this add-on?
bl_info = {
"name": "Add Cube",
"author": "jsh",
"version": (1, 0),
"blender": (2, 68, 0),
"location": "View3D > Tool Shelf > Text make",
"description": "Adds a new Mesh Object",
"warning": "",
"wiki_url": "",
"tracker_url": "http://togetherall.infomaster.co.kr",
"category": "Object"}
import bpy
from bpy.props import *
#
# Store properties in the active scene
#
def initSceneProperties(scn):
bpy.types.Scene.MyInt = IntProperty(
name = "Integer",
description = "Enter an integer")
scn['MyInt'] = 17
bpy.types.Scene.MyFloat = FloatProperty(
name = "Float",
description = "Enter a float",
default = 33.33,
min = -100,
max = 100)
bpy.types.Scene.MyBool = BoolProperty(
name = "Boolean",
description = "True or False?")
scn['MyBool'] = True
bpy.types.Scene.MyEnum = EnumProperty(
items = [('Eine', 'Un', 'One'),
('Zwei', 'Deux', 'Two'),
('Drei', 'Trois', 'Three')],
name = "Ziffer")
scn['MyEnum'] = 2
bpy.types.Scene.MyString = StringProperty(
name = "String2")
scn['MyString'] = "Lorem ipsum dolor sit amet"
return
initSceneProperties(bpy.context.scene)
#
# Menu in UI region
#
class UIPanel(bpy.types.Panel):
bl_label = "Make Text"
bl_space_type = "VIEW_3D"
#bl_region_type = "UI"
bl_region_type = "TOOL_PROPS"
def draw(self, context):
layout = self.layout
scn = context.scene
layout.prop(scn, 'MyInt', icon='BLENDER', toggle=True)
layout.prop(scn, 'MyFloat')
layout.prop(scn, 'MyBool')
layout.prop(scn, 'MyEnum')
layout.prop(scn, 'MyString')
layout.operator("idname_must.be_all_lowercase_and_contain_one_dot")
#
# The button prints the values of the properites in the console.
#
class OBJECT_OT_PrintPropsButton(bpy.types.Operator):
bl_idname = "idname_must.be_all_lowercase_and_contain_one_dot"
bl_label = "make"
def execute(self, context):
bpy.ops.mesh.primitive_cube_add()
return{'FINISHED'}
def printProp(label, key, scn):
try:
val = scn[key]
except:
val = 'Undefined'
#print("%s %s" % (key, val))
return val
def register():
bpy.utils.register_class(UIPanel)
bpy.utils.register_class(OBJECT_OT_PrintPropsButton)
def unregister():
bpy.utils.unregister_class(UIPanel)
bpy.utils.unregister_class(OBJECT_OT_PrintPropsButton)
if __name__ == "__main__":
register()
Blender uses a so called RestrictContext during the register / unregsiter phase of a plugin. This means, you're not allowed to do certain operations on the context because the content might not be ready yet.
In your case, you're doing initSceneProperties(bpy.context.scene) in the global module scope, which means it will be executed immediately after that module is being loaded. Move that initialization code for example in your operator's execute() method and do it when the operator is first run, or any other place where it makes sense (as late as possible, as early as necessary).
See the docs on RestrictContext for an example on how to do this.

Categories

Resources