How to stop duplicating scripts in K6? - load-testing

I have to write like 20 different scripts in K6 for an application. And most of these scripts contains common functionalities like login, choose some options, etc...
So is there a better way to write K6 scripts without duplicating these common functionalities? Can we implement common methods in somewhere and execute it inside the default function or something similar to that?

You can write your own module contains common functionalities then import them:
$ cat index.js
import { hello_world } from './modules/module.js';
export default function() {
hello_world();
}
$ cat module.js
export function hello_world() {
console.log("Hello world");
}
You can read here for more details.

Yes, you can move the common methods to separate JS files and then import them in the scripts that require them: https://docs.k6.io/docs/modules

Related

How to create global function in Jenkins shared library

I am writing a shared library for Jenkins and am running across a bit of an organizational issue.
I have a number of pipeline scripts in var, however I'm finding there are a number of repeating functions and the code is not very dry.
One solution for this has been to create helper functions inside var like var/log.groovy, var/formatter.groovy. This has worked fine and I've been calling these functions from within my pipeline scripts like var/myPipeline.groovy.
I would just like to organize my var folder a bit better and keep my helper functions inside var/utils/log.groovy for example.
The problem is I'm not sure how to access them from my pipeline scripts inside var when I put them inside a sub-directory.
How can I access them? Or is there a better way to organize my global functions?
You can put them in src in a package structure that makes sense organizationally. Them import the right things in your var scripts.
in /src/com/yourco/Formatter.groovy
package com.yourco
class Formatter {
def static String formatThis(String something) {
"this is ${something}"
}
}
In your var
import com.yourco.Formatter
..
..
..
echo Formatter.formatThis('test')

imported module.submodule namespaces interfering with eachother

So I'm pretty deep into making a custom job/process custom manager module, and when i got to integrating it I came across an interaction I hadn't seen before with modules and namespaces.
Code speaks better than words:
So there are two slightly different scripts:
test1:
import jobManager
jobManager.jobMap = {'test1':'test123'}
AND test2:
import jobManager
jobManager.jobMap = {'test2':'test222'}
Top Level Script:
import test1
import test2
print(test1.jobManager.jobMap)
print(test2.jobManager.jobMap)
So when I run the top level script it prints:
{'test2':'test222'}
{'test2':'test222'}
But my expected output is:
{'test1':'test123'}
{'test2':'test222'}
Is this just a case where test1.jobManager and test2.jobManager are actually the same namespace? Is there a way to keep them separate?
Is this just a case where test1.jobManager and test2.jobManager are actually the same namespace?
Yes.
Is there a way to keep them separate?
Not without creating another module.

Importing classes for use in grails script _Events.groovy

In Grails 2.3.7 I'm using _Events.groovy to hook into WAR packaging to do some special processing:
_Events.groovy
import demo.utils.XmlUtil
eventCreateWarStart = { name, stageDir ->
XmlUtil.doSomething()
...
log.debug('done!')
}
When building the WAR, Grails complains about XmlUtil import statement. _Events.groovy is not a class, so import statements don't work. How can I use a custom class in a script if I can't import it? And how can I perform logging instead of using println?
Update
Loading classes manually based on this and this seems to do the trick, also got logging to work thanks to Aaron's answer below:
eventCreateWarStart = { name, stageDir ->
def xmlUtil = loadRequiredClass('demo.utils.XmlUtil')
xmlUtil.doSomething()
...
grailsConsole.log('done!')
}
loadRequiredClass = {classname ->
classLoader.loadClass(classname)
}
Questions
What are all implicit objects available to Grails scripts?
It's a pain but it does make sense when you think about it. The _Events.groovy is part of the build process which is also responsible for compiling the classes that you are trying to use in _Events.groovy. Definitely a catch-22 scenario but I don't see how it could be made better without splitting _Events.groovy into separate files that compile and load at different stages of the build process.
You can use grailsConsole.log("hi") or grailsConsole.updateStatus("hi") to log output to the console.

How to write multiple unittests in dart in multiple files?

I am writing some Dart library and want to have it unittested. I created directory test and want to put my tests in here. Because I am going to have a lot of tests, I want to have them separated to multiple files. My questions is, what is the Dart convention, how to do that. I want to have my tests easily run all, however, I also want to be able to run just one file of tests.
What are your suggestions?
It is common to separate tests into multiple files. I am including an example of how you can do that.
Imagine that you have 2 files with tests, foo_test.dart, bar_test.dart that contain tests for your program. foo_test.dart could look something like this:
library foo_test;
import 'package:unittest/unittest.dart';
void main() {
test('foo test', () {
expect("foo".length, equals(3));
});
}
And bar_test.dart could look something like this:
library bar_test;
import 'package:unittest/unittest.dart';
void main() {
test('bar test', () {
expect("bar".length, equals(3));
});
}
You could run either file, and the test contained in that file would execute.
The, I would create something like an all_tests.dart file that would import the tests from foo_test.dart and bar_test.dart. Here is what all_tests.dart could look like:
import 'foo_test.dart' as foo_test;
import 'bar_test.dart' as bar_test;
void main() {
foo_test.main();
bar_test.main();
}
If you executed all_tests.dart, both the tests from foo_test.dart and bar_test.dart would execute.
One thing to note: for all this to work, you need to declare foo_test.dart and bar_test.dart as libraries (see the first line of each file). Then, in all_tests.dart, you can use import syntax to fetch the contents of the declared libraries.
This is how I organize most of my tests.
There is a tool that does exactly that, Dart Test Runner. An excerpt from that page:
Dart Test Runner will automatically detect and run all the tests in your Dart project in the correct environment (VM or Browser).
It detects any test writen in a file suffixed with _test.dart where your test code is inside a main() function. It doesn't have any problem detecting and running unittest tests.
It's pretty easy to install it and run it. Just two commands:
$ pub global activate test_runner
$ pub global run test_runner
For more options, please check Dart Test Runner page.
It's not necessary to have multiple files to isolate a test - see Running only a single test and Running a limited set of tests.
To isolate a test, change test() to solo_test().
So you can put all your tests in the same file (or into several parts).
In case it can be helpful for anybody while running a bunch of tests at once,
I was writing tests but my test file names did not end with *_test.dart
So I was not able to run all tests at once.
If you want to run all tests at once, it's mandatory to end your dart file with _test.dart.

Best practices to develop and maintaing code for complex JQuery/JQueryUI based applications

I'm working on my first very complex JQuery based application.
A single web page can contain hundreds of JQuery related code for example to JQueryUI dialogs.
Now I want to organize code in separated files.
For example I'm moving all initialization dialogs code $("#dialog-xxx").dialog({...}) in separated files and due to reuse I wrap them on single function call like
dialogs.js
function initDialog_1() {
$("#dialog-1").dialog({});
}
function initDialog_2() {
$("#dialog-2").dialog({});
}
This simplifies function code and make caller page clear
$(function() {
// do some init stuff
initDialog_1();
initTooltip_2();
});
Is this the correct pattern?
Are you using more efficient techniques?
I know that splitting code in many js files introduces an ugly band-bandwidth usage so.
Does exist some good practice or tool to 'join' files for production environments?
I imagine some tool that does more work than simply minimize and/or compress JS code.
Some suggestions I might add:
keep all your variables in a globally available, multi-structured object, something like: MyVars = { dialogs: {}, tooltips: {} } and then use that across all your scripts
use call or apply methods for dynamically calling custom function names,if you perhaps want to keep the above object lightweight
For tidying things up, you could read this: http://betterexplained.com/articles/speed-up-your-javascript-load-time
This sounds fairly okay too me. Just two notes:
Use descriptive method names. "initDialog_1" doesn't tell you anything about the dialog it initializes.
While keeping JS code split into several files eases development it harms the felt performance of your interface. You could merge all files into one during build/deployment/runtime of your app. How to do it best heavily depends on your environment though.
I'm working on something fairly complex in JS right now, and have been wondering the same thing. I looked at various "module" implementations but while they look "cool" they don't seem to offer much value.
My plan at this point is to continue referencing lots of script files from my .html page (the plan is to only have one .html page, or very few).
Then when I'm building the release version, I'll write a very simple tool to fit into my build process, which will discover all the scripts I reference from the .html pages and concatenate them into one file, and replace the multiple <script> elements with a single one, so that only one request is necessary in the "release" version.
This will allow the compression to work across all the script text instead of on each separate file (like doing tar followed by gzip) and should make a difference to the script download time (though I should stress I haven't actually implemented it yet).
You usually want to keep all of your javascript inside one file. Less HTTP requests is usually better. If you take a look at the jQuery source, you'll notice that every function and property is right there in the jQuery global object:
jQuery.fn = jQuery.prototype = {
init: function(){ ... },
animate: function() { ... },
each: function() { ... },
// etc
}
However, the pattern you seem to be interested seems similar to the "module" pattern. The YUI framework uses this pattern, and allows developers to "require" different components of the library from the core module via HTTP request. You can read more about YUI here:
http://developer.yahoo.com/yui/3/yui/

Resources