How to use variable from JSR223 in MQTT request - jmeter

I have following script in JSR223:
setStrictJava (true);
int a = 0; // Creating a Variable
int b = 1; // Creating a Variable
int S = a - b; //
log.info(S + " - Subtraction Operation");
vars.put("T1", S)
Added, I have MQTT request, where i need to use the T1 output (-1) but it doesn't return anything.
Sure i am missing few basic. pls any help.

I don't think you can save an Integer using vars.put() function as it takes 2 Strings as the parameters, so my expectation is that you need to explicitly convert your S variable to String like:
vars.put("T1", S as String)
If you need the variable as the Integer in your MQTT logic you can use vars.putObject() function instead like:
vars.putObject("T1", S)
More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Related

How to create a global variable in ATS?

Basically, I am looking for something more or less equivalent to the following C code:
int theGlobalCount = 0;
int
theGlobalCount_get() { return theGlobalCount; }
void
theGlobalCount_set(int n) { theGlobalCount = n; return; }
You could use a neat trick: declare a mutable global variable, and make a ref (aka mutable reference) point to it (no GC is required to make this work!). Then, implement functions to provide access to the mutable reference.
local
var theGlobalCount_var : int = 0
val theGlobalCount = ref_make_viewptr (view# theGlobalCount_var | addr# theGlobalCount_var)
in // in of [local]
fun
theGlobalCount_get () : int = ref_get_elt (theGlobalCount)
fun
theGlobalCount_set (n: int): void = ref_set_elt (theGlobalCount, n)
end // end of [local]
Note that declarations inside local-in are visible only to code inside in-end. Therefore, neither theGlobalCount_var nor theGlobalCount are visible outside the scope of the local.
Full code: glot.io
You can also use the extvar feature to update an external global variable (declared in the target language). This is very useful if you compile ATS to a language that does not support explicit pointers (e.g., JavaScript). Here is a running example that makes use of this feature:
http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw/MsXhVE0A

Jmeter BeanShell - compare between int to counter

I'm trying to compare int (parse from string) to counter in BeanShell assertion.
my code:
int i = Integer.parseInt(vars.get("count_2"));
counter = vars.get("counter");
if (i != counter)
{
Failure = true;
FailureMessage = "failed";
} else {
Failure = false;
}
On debug sampler I can see that both "count_2" and "counter" have the same values in all loop runs, but the assertion fails.
What went wrong?
Option 1: use integers everywhere
Change this line:
counter = vars.get("counter");
To:
int counter = Integer.parseInt(vars.get("counter"));
Option 2: use strings everywhere
String i = vars.get("count_2");
String counter = vars.get("counter");
if (!i.equals(counter))
...
JMeterVariables can be either Strings or Objects, so you need to cast them to the types you need to work with.
See How to use BeanShell: JMeter's favorite built-in component guide for essential information on scripting in JMeter and some form of cookbook.

protobuf.serializer.serialize equivalnce in C++

I am writing adapter class (library function) which will take different kind of PB messages as the input in the form of std::Map and serialize this map write in to the file then vice versa.
Example:
message user_defined_type
{
optional int Val1 = 1;
optional string Val2 = 2;
}
message Store
{
optional int Key = 1;
optional user_defined_type Value = 2;
}
The client will create std::Map and stores the above message (i.e., std::map XYZ). The library takes the std::Map as input and does serializing the message and store it in to the file. But the library don't have/know the Proto message definitions.
To achieve the above came up with an approach, the library will have intermediate proto message which has both the fields are byte type
message MAP
{
optional byte KeyField = 1;
optional byte ValueField = 2;
}
Such that the KeyField takes has value of Store::Key and ValueField has the value of Store::user_defined_type so the serialization and de-serialization will be generic for all type of messages.
In C# using the protobuf.serializer.serialize I can serialize/de-serialize to the designated type but in C++ don't know how to make it, any help/pointer much appreciated.
If I understand correctly, the challenge is that your library needs to know how to parse ValueField (and perhaps KeyField) but the library itself does not know their types; only the caller of the library does.
The way to solve this is to have the caller pass in a "prototype" instance of their message type, from which you can spawn additional instances:
map<string, Message*> parse(string data, const Message& prototype) {
map<string, Message*> result;
MapProto proto;
proto.ParseFromString(data);
for (int i = 0; i < proto.entry_size(); i++) {
Message* value = prototype->New();
value->ParseFromString(proto.entry(i).value());
result[proto.entry(i).key()] = value;
}
return result;
}
The caller would call this function like:
map<string, Message*> items = parse(data, MyValueType::default_instance());
Then, each message in the returned map will be an instance of MyValueType (the caller can use static_cast to cast the pointer to that type). The trick is that we had the caller pass in the default instance of their type, and we called its New() method to construct additional instances of the same type.

How to make my program(exe) to stop(and not exit) once an event(generation of a text file) has occured?

I am creating an executable in visual studio.
My code goes like this:
if(condition)
goto Step 1
else
goto Step 2
Step 1:
code
Step 2:
code
I want to make this in a way that if Step 1 has run then Step 2 must be skipped.
Should it be done using functions?
Within your class It can be placed in two functions and called from the if - else logic or you can place the code in between the if and else. If the code is large then it would be better to create two functions.
If (condition)
call function step1
else
call function step2
or
if (condition)
code...
else
code...
C# Example of defining a method and calling it:
public void Caller()
{
int numA = 4;
// Call with an int variable.
int productA = Square(numA);
int numB = 32;
// Call with another int variable.
int productB = Square(numB);
// Call with an integer literal.
int productC = Square(12);
// Call with an expression that evaulates to int.
productC = Square(productA * 3);
}
int Square(int i)
{
// Store input argument in a local variable.
int input = i;
return input * input;
}

how to rb_protect everything in ruby

I want to call ruby code from my own C code. In case an exception gets raised, I have to rb_protect the ruby code I call. rb_protect looks like this:
VALUE rb_protect(VALUE (* proc) (VALUE), VALUE data, int * state)
So proc has to be a function which takes VALUE arguments and returns VALUE. I have to call a lot of functions which do not work that way. How can I rb_protect them from raising exceptions?
I have thought of using Data_Make_Struct to wrap everything into one ruby object and call methods on it. Data_Make_Struct could itself raise an exception. How do I rb_protect Data_Make_Struct?
To use rb_protect in a flexible way (e.g., to call a Ruby function with an arbitrary numbers of arguments), pass a small dispatch function to rb_protect. Ruby requires that sizeof(VALUE) == sizeof(void*), and rb_protect blindly passes the VALUE-typed data to the dispatch function without inspecting it or modifying it. This means that you can pass whatever data you want to the dispatch function, let it unpack the data and call the appropriate Ruby method(s).
For example, to rb_protect a call to a Ruby method, you might use something like this:
#define MAX_ARGS 16
struct my_callback_stuff {
VALUE obj;
ID method_id;
int nargs;
VALUE args[MAX_ARGS];
};
VALUE my_callback_dispatch(VALUE rdata)
{
struct my_callback_stuff* data = (struct my_callback_stuff*) rdata;
return rb_funcall2(data->obj, data->method_id, data->nargs, data->args);
}
... in some other function ...
{
/* need to call Ruby */
struct my_callback_stuff stuff;
stuff.obj = the_object_to_call;
stuff.method_id = rb_intern("the_method_id");
stuff.nargs = 3;
stuff.args[0] = INT2FIX(1);
stuff.args[1] = INT2FIX(2);
stuff.args[2] = INT2FIX(3);
int state = 0;
VALUE ret = rb_protect(my_callback_dispatch, (VALUE)(&stuff), &state);
if (state) {
/* ... error processing happens here ... */
}
}
Also, keep in mind that rb_rescue or rb_ensure may be a better approach for some problems.

Resources