Write JDBC query results to CSV file? - jmeter

I would like to setup a test plan to execute a query and write the results to a csv file.
Following the advice from this question:
https://sqa.stackexchange.com/questions/26456/write-jdbc-request-results-to-csv-file
I have setup my test plan. It runs without issue, but a foo.csv file is not created.
this is the code in the JSR223 preprocessor:
resultSet = vars.getObject("resultSet");
StringBuilder result = new StringBuilder();
for (Object row : resultSet ) {
iter = row.entrySet().iterator();
while (iter.hasNext()) {
pair = iter.next();
result.append(pair.getValue());
result.append(",");
}
result.append(System.getProperty("line.separator"));
}
org.apache.commons.io.FileUtils.writeStringToFile(new File("foo.csv"), result.toString(), "UTF-8");

The file is being written in current working directory, you can locate where JMeter written it by running the following command in the Terminal application:
find / -type f -name 'foo.csv'
You can also amend the last line of code in order to explicitly specify full path to the CSV file like:
org.apache.commons.io.FileUtils.writeStringToFile(new File("/Users/aoppenheim/Desktop/foo.csv"), result.toString(), "UTF-8");
Also I would suggest switching "Language" to groovy as java assumes using Beanshell interpreter and it might become a performance bottleneck in case of high loads. See Apache Groovy - Why and How You Should Use It guide for more details.

This was actually working, the csv file was just not being written where I expected. I added this to find the file:
log.info(System.getProperty("user.dir"));

Related

JMeter Beanshell - save file as pdf

Currently I am saving variable values in ".txt" file using beanshell post-processor, I want to save variable value into a pdf file , is there any way I can achieve it?
To save variable value in a text file, i am using below code:
var1= vars.get("myVariableValue");
f = new FileOutputStream("D:/myTextFile.txt",true);
p = new PrintStream(f);
this.interpreter.setOut(p);
p.println(var1);
f.close();
You need an external library like PdfBox for this
You will need to have PDFBox in JMeter Classpath
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting
Assuming all above:
Download pdfbox-2.0.24.jar and fontbox-2.0.24.jar files and put them to "lib" folder of your JMeter installation
Restart JMeter to pick up the "jar"
Add an appropriate JSR223 Test Element to your Test Plan
Put the following code into "Script" area:
import org.apache.jmeter.threads.JMeterVariables
JMeterVariables vars = new JMeterVariables()
vars.put('myVariableValue','hello')
def document = new org.apache.pdfbox.pdmodel.PDDocument()
def page = new org.apache.pdfbox.pdmodel.PDPage()
document.addPage(page)
def contentStream = new org.apache.pdfbox.pdmodel.PDPageContentStream(document, page)
contentStream.setFont(org.apache.pdfbox.pdmodel.font.PDType1Font.COURIER, 12)
contentStream.beginText()
contentStream.showText(vars.get('myVariableValue'))
contentStream.endText()
contentStream.close()
document.save('myPDFFile.pdf')
document.close()
That's it, you should see myPDFFile.pdf file in JMeter's working directory

How can I add my own code to JAVA generated classes from proto file?

I'm using protobuf and I'm generating JAVA classes from the following proto file.
syntax = "proto3";
enum Greeting {
NONE = 0;
MR = 1;
MRS = 2;
MISS = 3;
}
message Hello {
Greeting greeting = 1;
string name = 2;
}
message Bye {
string name = 1;
}
option java_multiple_files = true;
Now I need to add some code to the generated files and I found that is possible using a custom plugin (https://developers.google.com/protocol-buffers/docs/reference/java-generated#plugins). I'm trying to generate that plugin in Java, something like this.
public class Test {
PluginProtos.CodeGeneratorResponse.getDefaultInstance();
/* Code to get generated files from java_out and use the insertion points */
codeGeneratorResponse.writeTo(System.out);
}
And then I run
protoc --java_out=./classes --plugin=protoc-gen-demo=my-plugin --demo_out=. example.proto
The problem is that on my Test.java main method I don't know how to get access to the files created by the option --java_out so that I can use their insertion points. Currently the CodeGeneratorResponse for the default instance is empty (no files).
Does anybody know how can I get the CodeGeneratorResponse from the --java_out so that I can add more code to the generated classes?
Thanks in advance.
I recently struggled with this as well and wasn't able to find a good answer. I finally figured it out after staring at the comments within the CodeGeneratorResponse message for a while.
What threw me off at first was that I was thinking of plugins as a pipeline, where the output from one feeds into the next. However, each plugin gets the exact same input (the parsed .proto files expressed via CodeGeneratorRequest messages), and all the generated code from the plugins (including the built-in ones) gets combined into the output file. However, plugins may modify the output from the previous plugins, which is what insertion points are designed for.
Specifically to your question, you would add a file to the response with the name field getting set to the name of the generated Java file, the insertion_point field getting set to the name of the insertion point at which you want to add code, and the content field getting set to the code you want inserted at that point.
I found this article helpful in creating a simple plugin (in this case in python). As a simple test, I modified the generate_code function from that article to look like this:
def generate_code(request, response):
for proto_file in request.proto_file:
f = response.file.add()
f.name = "Test.java"
f.insertion_point = "outer_class_scope"
f.content = "// Inserting a comment as a test"
Then I ran protoc with the plugin:
$ cat test.proto
syntax = "proto3";
message MyMsg {
int32 num = 1;
}
$ protoc --plugin=protoc-gen-sample=sample_proto_gen.py --java_out=. --sample_out=. test.proto
$ tail -n3 Test.java
// Inserting a comment as a test
// ##protoc_insertion_point(outer_class_scope)
}
Your plugin just needs to be some executable which reads a CodeGeneratorRequest message from stdin and writes a CodeGeneratorResponse message to stdout, so could certainly be written in Java instead. I just chose python as I'm generally more comfortable with it and found this simple example.
As a reference, here's a plugin I wrote for generating code based on custom protobuf options.
I have made a custom python plugin.
To run my plugin i use the command below:
protoc --plugin=protoc-gen-custom=my_plugin_executable_file --custom_out=./build test.proto
So i think that, you have to generate an executable file from your .java file and use it in your command.

Getting/using output of CMD window of Jmeter

I,m running a Java file from BeanShell Sampler in jmeter, I'm getting the output successful in cmd windows of jmeter. The output comprises of series of logger files,I need to extract only a specified string from the cmd window and use it for another sample
Given you run your program using i.e. ProcessBuilder you should be able to access its output via Process.getInputStream() method
Process process = new ProcessBuilder('c:\\apps\\jmeter\\bin\\jmeter.bat', '-v').start()
String output = org.apache.commons.io.IOUtils.toString(process.getInputStream(),'UTF-8')
log.info('My program output is:')
log.info(output)
Also I would recommend considering switching to JSR223 Sampler and Groovy language as this way it will be much faster and easier:
def output = "jmeter.bat -v".execute().text
log.info('My program output is:')
log.info(output)
Demo:
This java bean shell Command made the console out by j meter that is std out to be written in a file
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("D:\\dir1\\dir2\\abc.out")),true));
Make sure your path to file should have double backward slash

run sh script in jmeter

For load testing I want to randomize my testvalues before I run the test in jmeter. To do so, I want to use this bash script:
#! /bin/bash
cat data.dsv | shuf > randomdata.dsv
This should be executed in jmeter. I tried using a BeanShell Sampler with this command (I am using this command to always find the correct paht to the file no matter on which machine I want to execute it):
execute(${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}random.sh)
but I always get this error message:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``execute(/home/user/git/path/'' Encountered "( /" at line 1, column 8.
Any Ideas what to do or is there some best practice I just di not found yet?
I would suggest going for OS Process Sampler instead, it should be easier to use, something like:
In regards to Beanshell approach, there is no need to us __Beanshell function in the Beanshell sampler, besides an instance of Beanshell interpreter is created each time you call the function causing performance overhead. You can just put the code into sampler's "Script" area as
import org.apache.jmeter.services.FileServer;
StringBuilder command = new StringBuilder();
FileServer fileServer = FileServer.getFileServer();
command.append(fileServer.getBaseDir());
command.append(System.getProperty("file.separator"));
command.append("random.sh");
Process process = Runtime.getRuntime().exec(command.toString());
int returnValue = process.waitFor();
return String.valueOf(returnValue);
See How to use BeanShell: JMeter's favorite built-in component guide for information on Beanshell scripting in JMeter.

Book rating predication using lenskit

I read this website : http://lenskit.org/documentation/evaluator/quickstart/ I first tried to run it using the script " $ lenskit eval " and I just created a new groovy file in my hello-lenskit example and run it using the command line but nothing happened. Then I tried to use it in Java program(hello-lenskit.java).
I run into some errors.
File dataFile = new File("ml-100k/u.data");
PreferenceDomain domain = new PreferenceDomain(1.0,5.0,1.0);
DataSource data = new CSVDataSource("ml-100k",dataFile,"\t",domain);//give me an error CSVDataSource is not public and can not be accessed from the outside package.
CrossfoldTask cross = new CrossfoldTask();
LenskitConfiguration config1 = new LenskitConfiguration();
config1.bind(ItemScorer.class)
.to(UserMeanItemScorer.class);
AlgorithmInstance alg1 = new AlgorithmInstance("PersMean",config1);
evl.addAlgorithm(alg1);
LenskitConfiguration config2 = new LenskitConfiguration();
config2.bind(ItemScorer.class)
.to(ItemItemScorer.class);
config2.bind(UserVectorNormalizer.class)
.to(BaselineSubtractingUserVectorNormalizer.class);
config2.within(UserVectorNormalizer.class)
.bind(BaselineScorer.class,ItemScorer.class)
.to(ItemMeanRatingItemScorer.class);
AlgorithmInstance alg2 = new AlgorithmInstance("ItemItem",config2);
evl.addAlgorithm(alg2);
evl.addMetric(RMSEPredictMetric.class);
File file = new File("eval-results.csv");
evl.setOutput(file);
What should I do next? How could I generate the overall rating error?
Using the LensKit evaluation commands manually is difficult, undocumented, and not recommended.
The SimpleEvaluator is the best way to get overall accuracy from a LensKit recommender in a Java application.
For further assistance in debugging LensKit runs, I recommend e-mailing the mailing list with exactly the commands you are running and the output or errors you are getting.

Resources