File declaration throwing error in JMeter, JSR223 sampler with groovy script - jmeter

I am using the following script to handle a file upload request for my application. I am using this script inside a JSR223 Sampler and the code block
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
def imageFileName = "C:\\Users\\IMAGES\\IMG00001.dcm"
def urlRequest='https://<app-server>/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=${SIDVALUE}&study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110'
def postRequest = new HttpPost(urlRequest);
def file = new File(imageFileName);
def builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, imageFileName);
def entity = builder.build();
post.setEntity(entity);
def response = HttpClientBuilder.create().build().execute(postRequestt);
ERROR I am getting: When I am removing the file declaration code execute with error.
2022-09-01 13:44:03,501 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script44.groovy: 8: Unexpected input: '"' # line 8, column 21.
def imageFileName = "C:\Users\IMAGES\IMG00001.dcm"
^
1 error
javax.script.ScriptException: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script44.groovy: 8: Unexpected input: '"' # line 8, column 21.
def imageFileName = "C:\Users\IMAGES\IMG00001.dcm"
^
1 error
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile(GroovyScriptEngineImpl.java:183) ~[groovy-jsr223-3.0.3.jar:3.0.3]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:216) ~[ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) [ApacheJMeter_java.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:630) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script44.groovy: 8: Unexpected input: '"' # line 8, column 21.
def imageFileName = "C:\Users\IMAGES\IMG00001.dcm"
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:287) ~[groovy-3.0.3.jar:3.0.3]
at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:143) ~[groovy-3.0.3.jar:3.0.3]
at org.apache.groovy.parser.antlr4.AstBuilder.collectSyntaxError(AstBuilder.java:4544) ~[groovy-3.0.3.jar:3.0.3]
at org.apache.groovy.parser.antlr4.AstBuilder.access$000(AstBuilder.java:341) ~[groovy-3.0.3.jar:3.0.3]
at org.apache.groovy.parser.antlr4.AstBuilder$1.syntaxError(AstBuilder.java:4559) ~[groovy-3.0.3.jar:3.0.3]
I tried with C:\Users\IMAGES\IMG00001.dcm instead of C:\\Users\\IMAGES\\IMG00001.dcm, but no luck

Your post is not declared anywhere
Your postRequestt is not declared anywhere
You shouldn't refer JMeter Functions or Variables as ${SIDVALUE}, use vars shorthand for JMeterVariables instance instead
Fixed code would be something like:
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.HttpClientBuilder
def imageFileName = "C:\\Users\\IMAGES\\IMG00001.dcm"
def urlRequest = 'https://<app-server>/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=' + vars.get('SIDVALUE') + '&study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110'
def postRequest = new HttpPost(urlRequest);
def file = new File(imageFileName);
def builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, imageFileName);
def entity = builder.build();
postRequest.setEntity(entity);
def response = HttpClientBuilder.create().build().execute(postRequest);

Related

ModuleNotFoundError: No module named 'pycaret.internal'

I am trying to deploy a ML model using Streamlit and Pycaret on Heroku.
When I try deploying the app, I get the following error:
ModuleNotFoundError: No module named 'pycaret.internal'
Traceback:
File "/app/.heroku/python/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 322, in _run_script
exec(code, module.__dict__)
File "/app/Final.py", line 11, in <module>
tuned_cat=joblib.load('Cat.pkl')
File "/app/.heroku/python/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 585, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "/app/.heroku/python/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
obj = unpickler.load()
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1050, in load
dispatch[key[0]](self)
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1338, in load_global
klass = self.find_class(module, name)
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1388, in find_class
__import__(module, level=0)
I have the following dependencies in the requirements.txt file:
pycaret==1.0.0
streamlit==0.58.0
I had to go with the pycaret version 1.0 because when I chose the latest version the slug size on Heroku would go beyond 500M and it would not deploy. Compiled slug size: 552M is too large (max is 500M).
Link to Github
Final.py:
import streamlit as st
import joblib
from pycaret.classification import *
tuned_cat=joblib.load('Cat.pkl')
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('CPU')
def run():
add_selectbox = st.sidebar.selectbox(
"What would you like to do?",
("Online Prediction","Batch Prediction"))
st.sidebar.info('This app is created to predict if the applicant should be granted a loan or not.')
st.title("Loan Prediction App")
if add_selectbox == 'Online Prediction':
gender = st.selectbox('Gender',['Female','Male'])
married = st.selectbox('Married',['No','Yes'])
depend = st.selectbox('Dependents',['0','1','2','3+'])
edu = st.selectbox('Education',['Graduate','Not Graduate'])
self = st.selectbox('Self Employed',['No','Yes'])
app_inc = st.number_input ('Applicant Income')
co_inc = st.number_input ('Coapplicant Income')
amt = st.number_input ('Loan Amount')
term = st.number_input ('Loan Amount Term')
credit = st.selectbox('Credit History',['0','1'])
prop_are = st.selectbox('Property Area',['Rural','Semiurban','Urban'])
output=""
test_df = pd.DataFrame()
test_df['Gender']= [gender]
test_df['Married']=[married]
test_df['Dependents']=[depend]
test_df['Education']=[edu]
test_df['Self_Employed']=[self]
test_df['ApplicantIncome']=[app_inc]
test_df['CoapplicantIncome']=[co_inc]
test_df['LoanAmount']=[amt]
test_df['Loan_Amount_Term']=[term]
test_df['Credit_History']=[credit]
test_df['Property_Area']=[prop_are]
if st.button("Predict"):
Cat_pred=predict_model(tuned_cat,data=test_df)['Label']
output = Cat_pred.values
if(output==0):
text="Rejected"
st.error(text)
elif(output==1):
text="Approved"
st.success(text)
if add_selectbox == 'Batch Prediction':
file_upload = st.file_uploader("Upload excel file for predictions", type=["xlsx"])
if file_upload is not None:
data = pd.read_excel(file_upload)
st.success('File uploaded successfully!')
Cat_pred=predict_model(tuned_cat,data=data)['Label']
data['Prediction']=Cat_pred
st.write(data)
st.markdown(get_table_download_link(data), unsafe_allow_html=True)
import base64
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(
csv.encode()
).decode() # some strings <-> bytes conversions necessary here
return f'Download csv file'
if __name__ == '__main__':
run()

Groovy: unable to resolve class when running from command line

I'm trying to run a groovy script from command line. It runs fine in a gradle project in intellij, but when I try to run it via command line I get several errors like this one
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed:
/home/tpulayan/jenkins-
watcher/src/main/groovy/attempt_authentification.groovy: 1: unable to resolve
class in.ashwanthkumar.slack.webhook.Slack
# line 1, column 1.
import in.ashwanthkumar.slack.webhook.Slack
^
1 error
My code:
#Grapes(
#Grab(group='in.ashwanthkumar', module='slack-java-webhook',
version='0.0.7')
)
import in.ashwanthkumar.slack.webhook.Slack
import in.ashwanthkumar.slack.webhook.SlackMessage
import org.apache.http.HttpException
import org.apache.http.HttpHost
import org.apache.http.HttpRequest
import org.apache.http.HttpRequestInterceptor
import org.apache.http.auth.AuthScheme
import org.apache.http.auth.AuthScope
import org.apache.http.auth.AuthState
import org.apache.http.auth.UsernamePasswordCredentials
import org.apache.http.client.CredentialsProvider
import org.apache.http.client.methods.HttpGet
import org.apache.http.client.protocol.ClientContext
import org.apache.http.impl.auth.BasicScheme
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.protocol.BasicHttpContext
import org.apache.http.protocol.ExecutionContext
import org.apache.http.protocol.HttpContext
import org.apache.http.util.EntityUtils
use(TimerMethods) {
def timer = new Timer()
def periodSeconds = 600
new Slack()
.icon(':exclamation:')
.sendToChannel('jenkins-monitoring')
.displayName('Jenkins Watcher')
.push(new SlackMessage("Jenkins Watcher started at ${new Date()}. Started to monitor the status of deb-jenkins-prd..."))
def task = timer.runEvery(1000, periodSeconds * 1000) {
def response = getAuthentication().statusCode
if (response != 200) {
new Slack()
.icon(':exclamation:')
.sendToChannel('jenkins-monitoring')
.displayName('Jenkins Watcher')
.push(new SlackMessage("Login attempt by Jenkins Watcher failed at ${new Date()}. This could indicate that Jenkins is stuck! Request response code is: ${response}"))
}
}
}
I suppose it's something related to the classpath but I was unable to solve it.
Any help is much appreciated!
EDIT AFTER RESPONSE
I've updated my code with the #Grab but unfortunately I get this error when running it from command line:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
General error during conversion: Error grabbing Grapes -- [unresolved dependency: in.ashwanthkumar#slack-java-webhook;0.0.7: not found]
java.lang.RuntimeException: Error grabbing Grapes -- [unresolved dependency: in.ashwanthkumar#slack-java-webhook;0.0.7: not found]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:238)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:250)
at groovy.grape.GrapeIvy.getDependencies(GrapeIvy.groovy:464)
in build.gradle seems you have defined dependencies.
you can put all dependencies (*.jar) into ./lib dir and run your groovy script like this:
groovy -cp ./lib/* MyScript.groovy
or you can specify all the dependencies using Grape/Grab directly in your groovy script then your command line will be simple and groovy will download all dependencies on script start
groovy MyScript.groovy

Python error: one of the arguments is required

I'm trying to run a code from github that uses Python to classify images but I'm getting an error.
here is the code:
import argparse as ap
import cv2
import imutils
import numpy as np
import os
from sklearn.svm import LinearSVC
from sklearn.externals import joblib
from scipy.cluster.vq import *
# Get the path of the testing set
parser = ap.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-t", "--testingSet", help="Path to testing Set")
group.add_argument("-i", "--image", help="Path to image")
parser.add_argument('-v',"--visualize", action='store_true')
args = vars(parser.parse_args())
# Get the path of the testing image(s) and store them in a list
image_paths = []
if args["testingSet"]:
test_path = args["testingSet"]
try:
testing_names = os.listdir(test_path)
except OSError:
print "No such directory {}\nCheck if the file exists".format(test_path)
exit()
for testing_name in testing_names:
dir = os.path.join(test_path, testing_name)
class_path = imutils.imlist(dir)
image_paths+=class_path
else:
image_paths = [args["image"]]
and this is the error message I'm getting
usage: getClass.py [-h]
(- C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test TESTINGSET | - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/test_1.jpg IMAGE)
[- C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset]
getClass.py: error: one of the arguments - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/--testingSet - C:/Users/Lenovo/Downloads/iris/bag-of-words-master/dataset/test/test_1.jpg/--image is required
can you please help me with this? where and how should I write the file path?
This is an error your own program is issuing. The message is not about the file path but about the number of arguments. This line
group = parser.add_mutually_exclusive_group(required=True)
says that only one of your command-line arguments (-t, -i) is permitted. But it appears from the error message that you are supplying both --testingSet and --image on your command line.
Since you only have 3 arguments, I have to wonder if you really need argument groups at all.
To get your command line to work, drop the mutually-exclusive group and add the arguments to the parser directly.
parser.add_argument("-t", "--testingSet", help="Path to testing Set")
parser.add_argument("-i", "--image", help="Path to image")
parser.add_argument('-v',"--visualize", action='store_true')

Replace pickle with dill and pymongo call

I finally understood example how to replace pickle with dill from the following discussion: pickle-dill.
For example, the following code worked for me
import os
import dill
import multiprocessing
def run_dill_encoded(what):
fun, args = dill.loads(what)
return fun(*args)
def apply_async(pool, fun, args):
return pool.apply_async(run_dill_encoded, (dill.dumps((fun, args)),))
if __name__ == '__main__':
pool = multiprocessing.Pool(5)
results = [apply_async(pool, lambda x: x*x, args=(x,)) for x in range(1,7)]
output = [p.get() for p in results]
print(output)
I tried to apply the same philosophy to pymongo. The following code
import os
import dill
import multiprocessing
import pymongo
def run_dill_encoded(what):
fun, args = dill.loads(what)
return fun(*args)
def apply_async(pool, fun, args):
return pool.apply_async(run_dill_encoded, (dill.dumps((fun, args)),))
def write_to_db(value_to_insert):
client = pymongo.MongoClient('localhost', 27017)
db = client['somedb']
collection = db['somecollection']
result = collection.insert_one({"filed1": value_to_insert})
client.close()
if __name__ == '__main__':
pool = multiprocessing.Pool(5)
results = [apply_async(pool, write_to_db, args=(x,)) for x in ['one', 'two', 'three']]
output = [p.get() for p in results]
print(output)
produces error:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Python34\lib\multiprocessing\pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "C:\...\temp2.py", line 10, in run_dill_encoded
return fun(*args)
File "C:\...\temp2.py", line 21, in write_to_db
client = pymongo.MongoClient('localhost', 27017)
NameError: name 'pymongo' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/.../temp2.py", line 32, in <module>
output = [p.get() for p in results]
File "C:/.../temp2.py", line 32, in <listcomp>
output = [p.get() for p in results]
File "C:\Python34\lib\multiprocessing\pool.py", line 599, in get
raise self._value
NameError: name 'pymongo' is not defined
Process finished with exit code 1
What is wrong?
As I mentioned in the comments, you need to put an import pymongo inside the function write_to_db. This is because when the function is serialized, it does not take along any of the global references with it when it is shipped to the other process space.

GAE + Python2.7 + webapp2 + AJAX

Are there any tutorials or code examples related to AJAX implementation for GAE + Python2.7 + webapp2.
I have tried to follow instructions below:
http://code.google.com/appengine/articles/rpc.html
but I receive the following error:
Traceback (most recent call last):
File "E:\dev\workspace\test\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "E:\dev\workspace\test\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "E:\dev\workspace\test\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "E:\dev\workspace\test\webapp2.py", line 1101, in __call__
handler = self.handler(request, response)
TypeError: __init__() takes exactly 1 argument (3 given)
There is another similar discussion here:
Google App Engine Python Protorpc Error: __call__() takes exactly 1 argument (3 given)
heres is my code from Specialscope's example:
main.py
from BaseHandler import BaseHandler
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
import logging
from google.appengine.api import files
from google.appengine.api import images
import json
import webapp2
class FileuploadHandler(BaseHandler):
def get(self):
blobstore.create_upload_url('/static')
context={}
self.render_response("uploader.html",**context)
class FileDownloadHandler(blobstore_handlers.BlobstoreUploadHandler,BaseHandler):
def post(self):
upload_files=self.request.POST
#image=upload_files['file']
logging.error(upload_files)
keys=upload_files.keys()
imageurls=[]
for key in keys:
if key.find("uploadimage")!=-1:
image=upload_files[key]
file_name=files.blobstore.create(mime_type='image/jpg')
with files.open(file_name,'a') as f:
f.write(image.value)
files.finalize(file_name)
blob_key=files.blobstore.get_blob_key(file_name)
imageurls.append(images.get_serving_url(blob_key))
context={}
context['imagelinks']=imageurls
self.response.write(json.dumps(context))
app = webapp2.WSGIApplication([
('/upload', FileuploadHandler),
('/download', FileDownloadHandler),
], debug = True)
BaseHandler.py
import webapp2
import os
from webapp2_extras import jinja2
from google.appengine.ext import db
class BaseHandler(webapp2.RequestHandler):
#webapp2.cached_property
def jinja2(self):
# Returns a Jinja2 renderer cached in the app registry.
return jinja2.get_jinja2(app=self.app)
def render_response(self, _template, **context):
# Renders a template and writes the result to the response.
rv = self.jinja2.render_template(_template, **context)
self.response.write(rv)
The stack trace suggests that you have a url mapping in your WSGIApplication that has a group in it, but there's no handler with the corresponding arguments.
If you have
(r'/foo/(\s+)/(\s+)', FooHandler),
then you need
class FooHandler(webapp2.RequestHandler):
def get(self, arg1, arg2):
...
The doc you're using pre-dates Python 2.7 support by several years. Were I in your position, I'd be tempted to get the app working first on Python 2.5, then port to 2.7.
The problem is here:
import webapp2
app = webapp2.WSGIApplication([
('/upload', FileuploadHandler),
('/download', FileDownloadHandler),
], debug = True)
You can't use webapp2.WSGIApplication to construct your application, it doesn't understand protorpc. Instead, do this:
from protorpc.wsgi import service
app = service.service_mappings([
('/upload', FileuploadHandler),
('/download', FileDownloadHandler),
])

Resources