Character x,y in password - LoadRunner - c

I'm doing a load test for a sign in page where the user needs to input 2 characters of their password.
I've created an array of characters to say 'password1'.
Using correlation parameters I'm able to get the character number required. What I'm now trying to do is get the character number and match the array i.e. -
Character 1 is required, it will scan the array and bring back char[0].
Character 2 is required, it will scan the array and bring back char[1] etc.
I was thinking of doing a for loop to go through the array and determine where in the array a character is stored. I can't think how to initiate this for loop:
char1 = (char1-1);
char2 = (char2-1);
for(i=0;i<10;i++){
lr_output_message("%s",p[i]);
if (p[i] == p[char1]){
char1 = p[i];
}
}
The for loop works but it equals 115 (s in ASCII), I need a way of converting the value to a character, but I keep getting memory violations.
Sorry if I've over-complicated this issue, but my head has been lost trying to think about how to solve a seemingly easy problem. No doubt some of you will look at it a different way and tell me I've over-complicated it a significant amount!

Closed - worked it out.
Using these buffers instead of for loop.
buf[0] = char1a;
buf[1] = ‘\0’;
buf2[0] = char2a;
buf2[1] = ‘\0’;

Related

Correlation value not capturing for custom string as Left Boundary

I am capturing a string as my Left Boundary (LB) and then dividing that LB into 3 parts with strcpy and putting the value in char MyString. When I play my script, the correlation is not getting captured.
char MyString is capturing the value correctly, as when I'm printing it with lr_output_message it is showing me the correct LB as is from the server response values.
This is exactly what I'm doing...
char MyString[9999];
// original LB value is LB=DesktopQueuedActivitiesLV:0:CreateDate\" label=\"Create Date\" value=\"",
for (i = 0 ; i < 1 ; i++) {
lr_save_int(i,"MyRow");
strcpy(MyString, "DesktopQueuedActivitiesLV:");
strcat(MyString, lr_eval_string("{MyRow}"));
strcat(MyString, ":CreateDate\\\" label=\\\"Create Date\\\" value=\\\"");
lr_output_message("MyString = %s",MyString);
web_reg_save_param("DateReceived",
lr_eval_string("LB={MyString}"),
"RB=\">",
"Ord=1",
LAST);
}
Upon replay can't find the value for DateReceived
If I replace the line lr_eval_string("LB={MyString}") with the actual LB value, then it is working. Also, the lr_output_message("MyString = %s",MyString); is printing the exact same original LB value.
Can't figure it out why MyString is capturing the correct value but can't replace during actual line when playing the web_reg_save_param("DateReceived",. Please help.
You are using a loadrunner parameter designation for a C parameter inside of a loop. Both the loadrunner parameter reference is odd here, as is a re-running of the same correlation statement multiple times, as only the last one would have an affect when executed.
lr_output_message("MyString = %s",MyString);
web_reg_save_param("DateReceived",
lr_eval_string("LB={MyString}"),
Notice, the lr_output_message() is treating MyString as a C variable, but the second parameter of the web_reg_save_param() is treating the same element as a LoadRunner parameter. You either need to convert the C string to a LoadRunner parameter, recommend a different name, such as LR_MyString to differentiate the C string from the LR Parameter or create a C parameter which is in the form of "LB=myleftboundary"
lr_output_message("MyString = %s",MyString);
lr_save_string(MyString, "LR_MyString");
web_reg_save_param("DateReceived",
lr_eval_string("LB={LR_MyString}"),
OR
strcpy(MyString, "LB=DesktopQueuedActivitiesLV:");
strcat(MyString, lr_eval_string("{MyRow}"));
strcat(MyString, ":CreateDate\\\" label=\\\"Create Date\\\" value=\\\"");
lr_output_message("MyString = %s",MyString);
web_reg_save_param("DateReceived",
MyString,
You appear to be on the path of creating a psuedo array with DateReceived due to the loop which is executing once in this test, but you are probably wanting to increase the number of loop entries. In this case you are likely to fail for any number of array elements greater than 1 as you will always have the last time executed as the fulfilled correlation.

Having a character appear repeated times in an array and score counts. [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
**This is for Homework
I have a project coming up (A game of scrabble) and I have a couple questions. In scrabble certain letters can only appear a certain number of times. EX: E has an occurrence of 12, A has an occurrence of 9, ect. My first question is how would I make this possible without creating a gigantic array. So instead of
char arr['A' , 'A' , 'A'.....]
how would I just type
char arr['A' , 'B' , 'C']
(Type every letter once then somehow set a frequency for each letter.
My second question is how would I create a score counter for the program. In scrabble there are letters that have score values. I figured one way was to loop a ton of switch statements, but I was wondering if there's a more efficient way to do it.
I don't need any code made for me because that would take away from the whole point of learning programming, but examples would be appreciated or even an explanation of more efficient ways to create letter frequencies and score calculators. I figure that if I figure out how to do these things efficiently this early on, It would help me in the future.
Thanks for any help!
This code will have to be written in C
Okay the thing is first answer is seemingly easy if you think of using the ascii value.
What if I say that use the ASCII value as an index to an array. Any idea came?
Wait I will hint a bit more. A = 65...That means you can easily map to A using the index value 65 in your array.
Now you may wonder that for this you need a large array? Nope. Just use the offset appropriately. So now you think 65->A->0 then 66->B->1.
Second question:
Yes you can use an array here also the same way. If you need to store more data than just a score you can use array of structures. That will let you store other data also along with scores.
Things that may help you?
You can store the frequency of occurrences of characters by just incrementing the frequency array at correct position.
Design the code so that you understand where you store scores and how you handle it. Try to keep the scores in an array and or array of structures and manipulate them when needed.
To get a good design idea you can challenge yourself to store the game logic and game playing in different modules and interact with them accordingly.
Clarification
Suppose this is a char array.
char s[] = "ABCD";
for(int i = 0; i<4;i++)
Frequency[s[i]-'A']++;
This way here we are basically converting ascii value to an index of array. Here instead of directly using 65 I have subtracted that value so that every frequency is stored from index position 0.
If you consider that a character like A is actually an integral value representing the ASCII-Code of A (which is 65 in this case), then you could use this ASCII-value to index an integer array holding the maximum value for the specific letter (and the same for the actual count). See the following code illustrating this. Hope it helps:
int main() {
// ASCII, assuming character st A B C D E F, ...
char maxOccurrences[256]= { 12, 3, 4, 5, 9, 1 }; // ...
char actualOccurrences[256] = { 0 };
char *testInput = "AAABBBBCDEEEFF";
for(char *p=testInput;*p;p++) {
int charIndex = *p - 'A';
if (charIndex < 0 || charIndex > 26) {
printf("invalid character %c.\n", *p);
}
else if (actualOccurrences[charIndex] >= maxOccurrences[charIndex]) {
printf("maximum of %d for %c reached.\n",maxOccurrences[charIndex], *p);
}
else {
actualOccurrences[charIndex]++;
}
}
}
For the first question: Since you know there are exactly 27 different tiles in Scrabble (counting blanks), you can store just the frequency of each letter in an array of length 27.
For the second, you can take a similar approach and store the score of each letter in a constant array.
You then need to map each letter to the appropriate index, e.g. A should map to 0, B to 1, etc.; a clever way to do this would be to cast the character as an integer and subtract the appropriate amount.
So the second would work roughly like this, to score a = ['w','o','r',d']:
s = 0
First iteration: index of 'w' is 23, so
s += letter_scores[22]
letter_freqs[22] -= 1
Second iteration... and so on

Looping through all character combinations with increasing number of elements

What I want to achieve:
I have a function where I want to loop through all possible combinations of printable ascii-characters, starting with a single character, then two characters, then three etc.
The part that makes this difficult for me is that I want this to work for as many characters as I can (leave it overnight).
For the record: I know that abc really is 97 98 99, so a numeric representation is fine if that's easier.
This works for few characters:
I could create a list of all possible combinations for n characters, and just loop through it, but that would require a huge amount of memory already when n = 4. This approach is literally impossible for n > 5 (at least on a normal desktop computer).
In the script below, all I do is increment a counter for each combination. My real function does more advanced stuff.
If I had unlimited memory I could do (thanks to Luis Mendo):
counter = 0;
some_function = #(x) 1;
number_of_characters = 1;
max_time = 60;
max_number_of_characters = 8;
tic;
while toc < max_time && number_of_characters < max_number_of_characters
number_of_characters = number_of_characters + 1;
vectors = [repmat({' ':'~'}, 1, number_of_characters)];
n = numel(vectors);
combs = cell(1,n);
[combs{end:-1:1}] = ndgrid(vectors{end:-1:1});
combs = cat(n+1, combs{:});
combs = reshape(combs, [], n);
for ii = 1:size(combs, 1)
counter = counter + some_function(combs(ii, :));
end
end
Now, I want to loop through as many combinations as possible in a certain amount of time, 5 seconds, 10 seconds, 2 minutes, 30 minutes, so I'm hoping to create a function that's only limited by the available time, and uses only some reasonable amount of memory.
Attempts I've made (and failed at) for more characters:
I've considered pre-computing the combinations for two or three letters using one of the approaches above, and use a loop only for the last characters. This would not require much memory, since it's only one (relatively small) array, plus one or more additional characters that gets looped through.
I manage to scale this up to 4 characters, but beyond that I start getting into trouble.
I've tried to use an iterator that just counts upwards. Every time I hit any(mod(number_of_ascii .^ 1:n, iterator) == 0) I increment the m'th character by one. So, the last character just repeats the cycle !"# ... ~, and every time it hits tilde, the second character increments. Every time the second character hits tilde, the third character increments etc.
Do you have any suggestions for how I can solve this?
It looks like you're basically trying to count in base-26 (or base 52 if you need CAPS). Each number in that base will account for a specific string of character. For example,
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,10,11,12,...
Here, cap A through P are just symbols that are used to represent number symbols for base-26 system. The above simply represent this string of characters.
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,ba,bb,bc,...
Then, you can simply do this:
symbols = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E',...
'F','G','H','I','J','K','L','M','N','O','P']
characters = ['a','b','c','d','e','f','g','h','i','j','k','l',...
'm','n','o','p','q','r','s','t','u','v','w','x','y','z']
count=0;
while(true)
str_base26 = dec2base(count,26)
actual_str = % char-by-char-lookup-of-str26 to chracter string
count=count+1;
end
Of course, it does not represent characters that begin with trailing 0's. But that should be pretty simple.
You were not far with your idea of just getting an iterator that just counts upward.
What you need with this idea is a map from the integers to ASCII characters. As StewieGriffin suggested, you'd just need to work in base 95 (94 characters plus whitespace).
Why whitespace : You need something that will be mapped to 0 and be equivalent to it. Whitespace is the perfect candidate. You'd then just skip the strings containing any whitespace. If you don't do that and start directly at !, you'll not be able to represent strings like !! or !ab.
First let's define a function that will map (1:1) integers to string :
function [outstring,toskip]=dec2ASCII(m)
out=[];
while m~=0
out=[mod(m,95) out];
m=(m-out(1))/95;
end
if any(out==0)
toskip=1;
else
toskip=0;
end
outstring=char(out+32);
end
And then in your main script :
counter=1;
some_function = #(x) 1;
max_time = 60;
max_number_of_characters = 8;
currString='';
tic;
while numel(currString)<=max_number_of_characters&&toc<max_time
[currString,toskip]=dec2ASCII(counter);
if ~toskip
some_function(currString);
end
counter=counter+1;
end
Some random outputs of the dec2ASCII function :
dec2ASCII(47)
ans =
O
dec2ASCII(145273)
ans =
0)2
In terms of performance I can't really elaborate as I don't know what you want to do with your some_function. The only thing I can say is that the running time of dec2ASCII is around 2*10^(-5) s
Side note : iterating like this will be very limited in terms of speed. With the function some_function doing nothing, you'd just be able to cycle through 4 characters in around 40 minutes, and 5 characters would already take up to 64 hours. Maybe you'd want to reduce the amount of stuff you want to pass through the function you iterate on.
This code, though, is easily parallelizable, so if you want to check more combinations, I'd suggest trying to do it in a parallel manner.

Efficient search for series of values in an array? Ideally OpenCL usable?

I have a massive array I need to search (actually it's a massive array of smaller arrays, but for all intents and purposes, lets consider it one huge array). What I need to find is a specific series of numbers. Obviously, a simple for loop will work:
Pseudocode:
for(x = 0; x++) {
if(array[x] == searchfor[location])
location++;
else
location = 0;
if(location >= strlen(searchfor))
return FOUND_IT;
}
Thing is I want this to be efficient. And in a perfect world, I do NOT want to return the prepared data from an OpenCL kernel and do a simple search loop.
I'm open to non-OpenCL ideas, but something I can implement across a work group size of 64 on a target array length of 1024 would be ideal.
I'm kicking around ideas (split the target across work items, compare each item, looped, against each target, if it matches, set a flag. After all work items complete, check flags. Though as I write that, that sounds very inefficient) but I'm sure I'm missing something.
Other idea was that since the target array is uchar, to lump it together as a double, and check 8 indexes at a time. Not sure I can do that in opencl easily.
Also toying with the idea of hashing the search target with something fast, MD5 likely, then grabbing strlen(searchtarget) characters at a time, hashing it, and seeing if it matches. Not sure how much the hashing will kill my search speed though.
Oh - code is in C, so no C++ maps (something I found while googling that seems like it might help?)
Based on comments above, for future searches, it seems a simple for loop scanning the range IS the most efficient way to find matches given an OpenCL implementation.
Create an index array[sizeof uchar]. For each uchar in the search string make array[uchar] = position in search string of first occurence of uchar. The rest of array contains -1.
unsigned searchindexing[sizeof char] = { (unsigned)-1};
memcpy(searchindexing + 1, searchindexing, sizeof char - 1);
for (i = 0; i < strlen(searchfor); i++)
searchindexing[searchfor[i]] = i;
If you don't start at the beginning, an uchar occuring more than one time will get the wrong position entered into searchindexing.
Then you search the array by stepping strlen(searchfor) unless finding an uchar from searchfor.
for (i = 0; i < MAXARRAYLEN; i += strlen(searchfor))
if ((unsigned)-1 != searchindexing[array[i]]) {
i -= searchindexing[array[i]];
if (!memcmp(searchfor, &array[i], strlen(searchfor)))
return FOUND_IT;
}
If most of the uchar in array isn't in searchfor, this is probably the fastest way. Note the code has not been optimized.
Example: searchfor = "banana". strlen is 6. searchindexing['a'] = 5, ['b'] = 0, ['n'] = 4 and the rest a value not between 0 to 5, like -1 or maxuint. If array[i] is something not in banana like space, i increments by 6. If array[i] now is 'a', you might be in banana and it can be any of the 3 'a's. So we assume the last 'a' and move 5 places back and do a compare with searchfor. If succes, we found it, otherwise we step 6 places forward.

Find longest suffix of string in given array

Given a string and array of strings find the longest suffix of string in array.
for example
string = google.com.tr
array = tr, nic.tr, gov.nic.tr, org.tr, com.tr
returns com.tr
I have tried to use binary search with specific comparator, but failed.
C-code would be welcome.
Edit:
I should have said that im looking for a solution where i can do as much work as i can in preparation step (when i only have a array of suffixes, and i can sort it in every way possible, build any data-structure around it etc..), and than for given string find its suffix in this array as fast as possible. Also i know that i can build a trie out of this array, and probably this will give me best performance possible, BUT im very lazy and keeping a trie in raw C in huge peace of tangled enterprise code is no fun at all. So some binsearch-like approach will be very welcome.
Assuming constant time addressing of characters within strings this problem is isomorphic to finding the largest prefix.
Let i = 0.
Let S = null
Let c = prefix[i]
Remove strings a from A if a[i] != c and if A. Replace S with a if a.Length == i + 1.
Increment i.
Go to step 3.
Is that what you're looking for?
Example:
prefix = rt.moc.elgoog
array = rt.moc, rt.org, rt.cin.vof, rt.cin, rt
Pass 0: prefix[0] is 'r' and array[j][0] == 'r' for all j so nothing is removed from the array. i + 1 -> 0 + 1 -> 1 is our target length, but none of the strings have a length of 1, so S remains null.
Pass 1: prefix[1] is 't' and array[j][1] == 'r' for all j so nothing is removed from the array. However there is a string that has length 2, so S becomes rt.
Pass 2: prefix[2] is '.' and array[j][2] == '.' for the remaining strings so nothing changes.
Pass 3: prefix[3] is 'm' and array[j][3] != 'm' for rt.org, rt.cin.vof, and rt.cin so those strings are removed.
etc.
Another naïve, pseudo-answer.
Set boolean "found" to false. While "found" is false, iterate over the array comparing the source string to the strings in the array. If there's a match, set "found" to true and break. If there's no match, use something like strchr() to get to the segment of the string following the first period. Iterate over the array again. Continue until there's a match, or until the last segment of the source string has been compared to all the strings in the array and failed to match.
Not very efficient....
Naive, pseudo-answer:
Sort array of suffixes by length (yes, there may be strings of same length, which is a problem with the question you are asking I think)
Iterate over array and see if suffix is in given string
If it is, exit the loop because you are done! If not, continue.
Alternatively, you could skip the sorting and just iterate, assigning the biggestString if the currentString is bigger than the biggestString that has matched.
Edit 0:
Maybe you could improve this by looking at your array before hand and considering "minimal" elements that need to be checked.
For instance, if .com appears in 20 members you could just check .com against the given string to potentially eliminate 20 candidates.
Edit 1:
On second thought, in order to compare elements in the array you will need to use a string comparison. My feeling is that any gain you get out of an attempt at optimizing the list of strings for comparison might be negated by the expense of comparing them before doing so, if that makes sense. Would appreciate if a CS type could correct me here...
If your array of strings is something along the following:
char string[STRINGS][MAX_STRING_LENGTH];
string[0]="google.com.tr";
string[1]="nic.tr";
etc, then you can simply do this:
int x, max = 0;
for (x = 0; x < STRINGS; x++) {
if (strlen(string[x]) > max) {
max = strlen(string[x]);
}
}
x = 0;
while(true) {
if (string[max][x] == ".") {
GOTO out;
}
x++;
}
out:
char output[MAX_STRING_LENGTH];
int y = 0;
while (string[max][x] != NULL) {
output[y++] = string[++x];
}
(The above code may not actually work (errors, etc.), but you should get the general idea.
Why don't you use suffix arrays ? It works when you have large number of suffixes.
Complexity, O(n(logn)^2), there are O(nlogn) versions too.
Implementation in c here. You can also try googling suffix arrays.

Resources