I need to create POST requests with different body each one
Opened this issue · 5 comments
Hello
I have a big model (generating using Bogus lib - it has unique email, id etc) - I want to run Post Requests to create a lot of entities. Are there any ways to implement this?
I'v found you advised to use PreProcessor here - abstracta/jmeter-java-dsl#7 - but there is no PreProcessor class in dotnet dsl.
Tnanks!
Hello, thank you for bringing this into attention!
We can easily add the jsr223PreProcessor to the dotnet library with support for specifying code in groovy. Providing support for using c# code and dotnet libraries is not that simple though.
Can you provide an example code of what you would like the DSL to support or how would your think you could express in code what you need?
One alternative for generated and distinct data might also be implementing the csvDataSet element in dotnet library (easy to do as well) so you can generate in dotnet/c# code a CSV with whatever content you may like (and using whatever library you desire) and then just use the proper variables in the HTTP Post request.
In any case, I would like to understand a little better what would be the ideal scenario for your use case and then provide a good approximation to it .
Tnx for fast reply, this is my code:
.............
using Bogus;
[Fact]
public async Task LoadTest()
{
// Arrange
var requestDto = Create_CreateCompanyRequestBogus();
var request = JsonSerializer.Serialize(requestDto);
// Act
var stats = TestPlan(
ThreadGroup(2, 5,
HttpSampler("https://tst-api.purpleunity.dev/company-and-contact/api/companies")
.Post(request, new MediaTypeHeaderValue(MediaTypeNames.Application.Json))
.Header("Authorization", AuthToken)
),
JtlWriter("jtls")
).Run();
stats.Overall.SampleTimePercentile99.Should().BeLessThan(TimeSpan.FromSeconds(5));
}
private CreateCompanyRequest Create_CreateCompanyRequestBogus()
{
var faker = new Faker<CreateCompanyRequest>()
.CustomInstantiator(f =>
new CreateCompanyRequest(
IdentityId: Guid.NewGuid().ToString().OrNull(f, 0.2f),
Name: f.Company.CompanyName(),
VatNumber: f.Random.Replace("??#########").OrNull(f, 0.2f),
Iban: f.Random.Replace("??######################").OrNull(f, 0.2f),
Bic: f.Finance.Bic().OrNull(f, 0.2f),
ChamberOfCommerceNumber : f.Random.Replace("??-???-########").OrNull(f, 0.2f),
ExternalId: Guid.NewGuid().ToString().OrNull(f, 0.2f),
IsBuyerEvaluated: f.Random.Bool().OrNull(f, 0.2f),
Remarks: f.Lorem.Sentences(1).OrNull(f, 0.2f),
AddressLine: f.Address.StreetAddress().OrNull(f, 0.2f),
Zipcode: f.Address.ZipCode().OrNull(f, 0.2f),
City: f.Address.City().OrNull(f, 0.2f),
Region: f.Address.State().OrNull(f, 0.2f),
CountryCode: f.Address.CountryCode().OrNull(f, 0.2f),
Phone: f.Phone.PhoneNumber().OrNull(f, 0.2f),
Mobile: f.Phone.PhoneNumber().OrNull(f, 0.2f),
Fax: f.Phone.PhoneNumber().OrNull(f, 0.2f),
Email: f.Internet.Email().OrNull(f, 0.2f),
WebsiteUrl: new Uri(f.Internet.Url()),
DefaultContact: null,
UseTheSameAddressForPostal: f.Random.Bool().OrNull(f, 0.2f),
PostalAddressLine: f.Address.StreetAddress().OrNull(f, 0.2f),
PostalZipCode: f.Address.ZipCode().OrNull(f, 0.2f),
PostalCity: f.Address.City().OrNull(f, 0.2f),
PostalRegion: f.Address.State().OrNull(f, 0.2f),
PostalCountry: f.Address.CountryCode().OrNull(f, 0.2f)
)
);
return faker.Generate();
}
I join the question. For each request in each iteration, a new request must be created.
My code:
[Test]
public void RegisterTest()
{
var stats =
TestPlan(
ThreadGroup(5, 2,
HttpSampler("http://localhost/register/")
.Post(GetRegRequest(), new MediaTypeHeaderValue("application/json")
))).Run();
Assert.That(stats.Overall.SampleTimePercentile99, Is.LessThan(TimeSpan.FromSeconds(2)));
}
//Random random = new Random();
private SecureRandom random = new SecureRandom();
public string GetCardCode()
{
var getCard = random.Next(0, cardsCount - 1);
logger.Debug(getCard);
var cardCode = cards.Skip(getCard).Take(1).FirstOrDefault()?.CardCode;
logger.Debug(cardCode);
while (cardCode == null)
{
cardCode = cards.Skip(random.Next(0, cardsCount - 1)).Take(1).FirstOrDefault()?.CardCode;
}
return cardCode;
}
public string GetRegRequest()
{
var license = cashes.ToList().ElementAt(random.Next(0, cashCount - 1));
Guid accessTokenGuid = license.AccessTokenGuid;
var cardCode = GetCardCode();
return JsonConvert.SerializeObject(new RegisterRequestDto
{
LicenseGuid = license.LicenseGuid,
AccessTokenGuid = accessTokenGuid,
CardCode = cardCode,
CardRegisterDateTime = DateTime.Now,
RegisterDetailDtos = new List<RegisterDetailDto>
{
new RegisterDetailDto
{
PositionId="1",
ProductCode="12345",
Quantity=1,
TotalPrice=100
}
}
});
}
Great, thank you! The information you two provide is very helpful and we have some ideas that we would like to implement to support these scenarios. It is great to see the community interest in this feature.
If some other have similar interests please let us know!