-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add jsr223PreProcessor to allow generating dynamic info per request
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Abstracta.JmeterDsl.Tests/Core/PreProcessors/DslJsr223PreProcessorTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Abstracta.JmeterDsl.Core.PreProcessors | ||
{ | ||
using static JmeterDsl; | ||
|
||
public class DslJsr223PreProcessorTest | ||
{ | ||
[Test] | ||
public void ShouldUseDefinedLabelWhenPreProcessorSettingLabelInTestPlan() | ||
{ | ||
var stats = TestPlan( | ||
ThreadGroup(1, 1, | ||
DummySampler("ok") | ||
.Children( | ||
Jsr223PreProcessor("sampler.name = 'test'") | ||
) | ||
)).Run(); | ||
Assert.That(stats.Labels["test"].SamplesCount, Is.EqualTo(1)); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Abstracta.JmeterDsl/Core/PreProcessors/DslJsr223PreProcessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Abstracta.JmeterDsl.Core.TestElements; | ||
|
||
namespace Abstracta.JmeterDsl.Core.PreProcessors | ||
{ | ||
/// <summary> | ||
/// Allows running custom logic before executing a sampler. | ||
/// <br/> | ||
/// This is a very powerful and flexible component that allows you to modify variables, sampler, | ||
/// context, etc., before running a sampler (for example to generate dynamic requests | ||
/// programmatically). | ||
/// <br/> | ||
/// By default, provided script will be interpreted as groovy script, which is the default setting | ||
/// for JMeter. If you need, you can use any of JMeter provided scripting languages (beanshell, | ||
/// javascript, jexl, etc.) by setting the <see cref="DslJsr223TestElement{T}.Language(string)"/> property. | ||
/// </summary> | ||
public class DslJsr223PreProcessor : DslJsr223TestElement<DslJsr223PreProcessor>, IMultiLevelTestElement | ||
{ | ||
public DslJsr223PreProcessor(string name, string script) | ||
: base(name, script) | ||
{ | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Abstracta.JmeterDsl/Core/TestElements/DslJsr223TestElement.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace Abstracta.JmeterDsl.Core.TestElements | ||
{ | ||
/// <summary> | ||
/// Abstracts common logic used by JSR223 test elements. | ||
/// </summary> | ||
public abstract class DslJsr223TestElement<T> : BaseTestElement | ||
where T : DslJsr223TestElement<T> | ||
{ | ||
protected readonly string _script; | ||
protected string _language; | ||
|
||
public DslJsr223TestElement(string name, string script) | ||
: base(name) | ||
{ | ||
_script = script; | ||
} | ||
|
||
public T Language(string language) | ||
{ | ||
_language = language; | ||
return (T)this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
|
||
<!-- @include: loops/index.md --> | ||
<!-- @include: csv-dataset.md --> | ||
<!-- @include: jsr223-pre-processor.md --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
### Provide request parameters programmatically per request | ||
|
||
So far we have seen a how to generate requests with information extracted from CSV, but this is not enough for some scenarios. When you need more flexibility and power you can use `jsr223preProcessor` to specify your own logic to build each request. | ||
|
||
Here is an example: | ||
|
||
```cs | ||
using System; | ||
using System.Net.Http.Headers; | ||
using System.Net.Mime; | ||
using static Abstracta.JmeterDsl.JmeterDsl; | ||
|
||
public class PerformanceTest | ||
{ | ||
[Test] | ||
public void LoadTest() | ||
{ | ||
var stats = TestPlan( | ||
ThreadGroup(5, 10, | ||
HttpSampler("http://my.service") | ||
.Post("${REQUEST_BODY}", new MediaTypeHeaderValue(MediaTypeNames.Text.Plain)) | ||
.Children(Jsr223PreProcessor("vars.put('REQUEST_BODY', '{\"time\": \"' + Instant.now() + '\"}')")) | ||
) | ||
).Run(); | ||
Assert.That(stats.Overall.SampleTimePercentile99, Is.LessThan(TimeSpan.FromSeconds(5))); | ||
} | ||
} | ||
``` | ||
|
||
::: tip | ||
For the time being only JSR223 scripts can be used. By default `Groovy` is used, but you can change to others by using the provided `Language()` method. | ||
|
||
We plan in the future to look for alternatives as to be able to use .Net code as pre processor. If you are interested in this you can let us know in [this issue](https://github.com/abstracta/jmeter-dotnet-dsl/issues/3) posting your use case. | ||
::: | ||
|
||
Check [DslJsr223PreProcessor](/Abstracta.JmeterDsl/Core/PreProcessors/DslJsr223PreProcessor.cs) for more details and additional options. |