Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I need to create POST requests with different body each one #3

Open
destructorvad opened this issue Dec 6, 2023 · 5 comments
Open
Labels
enhancement New feature or request

Comments

@destructorvad
Copy link

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!

@rabelenda
Copy link
Contributor

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 .

@destructorvad
Copy link
Author

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();
    }

@alexchernyavskiy
Copy link

alexchernyavskiy commented Dec 20, 2023

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
                    }
                }
    });
}

@rabelenda
Copy link
Contributor

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!

@rabelenda rabelenda added the enhancement New feature or request label Jan 30, 2024
@rabelenda
Copy link
Contributor

We just released 0.4 which includes CsvDataSet and Jsr223PreProcessor.

None of these are optimal solutions for your use cases, but you can use them in the meantime as workarounds or approximations while we come up with some better solution.

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants