0

I am doing a hotel search by entering the hotel names.Below is the valid JSON response. This is a response for one hotel search. I have run 15 threads and I have 14 more JSON response for different hotels similar to this one. In this response you can see "providers" and "results" that refer to the providers with array values. My requirement is to find out how many offers ie., results each provider has in total across all the 15 hotels.

"providers":
    [
        {
            "MM_logofile":"agd.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"AGD",
            "name":"Agoda.com",
            "logo":"AGD.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"ian.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"IAN",
            "name":"Hotels.com",
            "logo":"IAN-Other.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"gar.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"GAR",
            "name":"getaroom.com",
            "logo":"GAR.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"exp.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"EXP",
            "name":"Expedia.dk",
            "logo":"EXP-DK.png",
            "isOfficial":false
        },
        {
            "MM_logofile":"acc.svg",
            "MM_isOfficialWithoutLogo":false,
            "code":"ACC",
            "name":"AccorHotels.com",
            "logo":"ACC.png",
            "isOfficial":true
        },

    ],

    "results":
    [
        {
            "roomName":"Standard Twin Rm Special Offer - Best price guarantee",
            "totalRate":918.0,
            "isCheapestRate":true,
            "hasFreeCancelation":false,
            "inclusions":
            [
            ],
            "availableRooms":null,
            "providerIndex":0,
            "includesAllTaxes":false,
            "excludedCharges":
            [
                0
            ],
            "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.495.USD.123155627&source=202-0&a_aid=72750&brandID=177977"
        },
        {
            "roomName":"Standard Double Rm Special Offer - Best price guarantee",
            "totalRate":918.0,
            "isCheapestRate":false,
            "hasFreeCancelation":false,
            "inclusions":
            [
            ],
            "availableRooms":null,
            "providerIndex":0,
            "includesAllTaxes":false,
            "excludedCharges":
            [
                0
            ],
            "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.496.USD.1523114518&source=202-1&a_aid=72750&brandID=177977"
        },
        {
            "roomName":"Standard Double Room Hot Deal - Best price guarantee",
            "totalRate":918.0,
            "isCheapestRate":false,
            "hasFreeCancelation":false,
            "inclusions":
            [
            ],
            "availableRooms":null,
            "providerIndex":1,
            "includesAllTaxes":false,
            "excludedCharges":
            [
                0
            ],
            "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.497.USD.573302441&source=202-2&a_aid=72750&brandID=177977"
        },
        {
            "roomName":"Standard Twin Room Hot Deal - Best price guarantee",
            "totalRate":918.0,
            "isCheapestRate":false,
            "hasFreeCancelation":false,
            "inclusions":
            [
            ],
            "availableRooms":null,
            "providerIndex":2,
            "includesAllTaxes":false,
            "excludedCharges":
            [
                0
            ],
            "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.11648360.-378376995.498.USD.1523907592&source=202-3&a_aid=72750&brandID=177977"
        },
        {
            "roomName":"Standard Room, 1 Double Bed",
            "totalRate":926.2,
            "isCheapestRate":false,
            "hasFreeCancelation":false,
            "inclusions":
            [
            ],
            "availableRooms":null,
            "providerIndex":3,
            "includesAllTaxes":false,
            "excludedCharges":
            [
                0
            ],
            "bookUri":"https://72750.api.hotelscombined.com/ProviderRedirect.ashx?key=0.13476094.-378377052.1210.USD.1325439035&source=202-4&a_aid=72750&brandID=177977"
        },

    ],
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
Ashley
  • 147
  • 2
  • 20
  • 1
    Where is the code ? Where is the json ? – Weedoze Nov 09 '16 at 13:31
  • try adding partial json and will build on that – Naveen Kumar R B Nov 09 '16 at 13:35
  • 1
    I am in the middle of doing it. my response is too long so not able to post it. give me a min. – Ashley Nov 09 '16 at 13:35
  • need clarification. how to map/identify whether a provider has an offer/result from the JSON response? I also think that one JSON response is for one hotel search result and you run 15 threads to run for 15 different hotels? – Naveen Kumar R B Nov 09 '16 at 13:49
  • these providers are in arrays. for example Agoda.com has providerIndex 0, Hotels.com has providerIndex 1 and so on. in the results, you could see the reference to the provider with the providerIndex. yes one response is for one hotel and 15 threads for 15 different hotel search. – Ashley Nov 09 '16 at 14:15

1 Answers1

1

Following is the beanshell code:

import java.util.HashMap;
import java.util.Map;

results_count = Integer.parseInt(vars.get("results_matchNr"));
providers_count = Integer.parseInt(vars.get("providers_matchNr"));
log.info("total results " + results_count);
Map results = new HashMap();
Map providers = new HashMap();

for(i=1; i<=providers_count; i++){
    log.info("iteration " + i);
    temp = vars.get("providers_"+i);
    log.info("provider_name " + temp);
    providers.put(i-1, temp);
}
log.info("providers: " + providers);

int provider_index = -1;
String provider_name = "";

for(i=1; i<=results_count; i++){
    log.info("iteration " + i);
    provider_index = Integer.parseInt(vars.get("results_"+i));
    log.info("provider_index " + provider_index);
    provider_name = providers.get(provider_index);
    log.info("provider name :" + provider_name);
    if(results.get(provider_name) == null){
        log.info("ading key for the first time " + provider_name);
        results.put(provider_name, 1);
    }
    else{
        log.info("second time " + provider_name);
        int existing = results.get(provider_name);
        log.info("exisiting value " + existing);
        int updateValue = existing+1;
        log.info("updated value: " + updateValue);
        results.put(provider_name, updateValue);
    }
}

log.info("results-providers mapping " + results);

int threadNum = ctx.getThreadNum();

 // if you want to log something to jmeter.log file

// Pass true if you want to append to existing file othewise false.
f = new FileOutputStream("G:\\naveen\\mywork\\testing\\performance\\tools\\jmeter\\examples\\result.csv", true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
String output = "thread number#" + threadNum + " " + results;
print(output);
f.close();

Add BeanShell PostProcessor to the sampler and add the above code.

enter image description here

Add JSONPATH Extractor to get the providers and add the following syntax:

$.providers[*].name

enter image description here

Add another JSONPATH Extractor to get the results/offers and add the following syntax:

$.results[*].providerIndex

enter image description here

Note: change the file location to store the results as per your machine in the beanshell code.

Following is the results which will be saved into the file:

    thread number#0 {Expedia.dk=1, Agoda.com=2, Hotels.com=1, getaroom.com=1}
thread number#1 {Expedia.dk=1, Agoda.com=2, Hotels.com=1, getaroom.com=1}

// two rows for two threads. here, Agoda.com provider has two offers, so the count is 2 and remaining providers has one offer each.

Note: verified the script for 2 users. there will be two entries present in the results file (one thread -> one row). chnage the foramt as per your requirements on beanshell code (code related to file writing).

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Thanks you so much. This is what I expected. Is it possible to combine all the results from all the threads and give the number of offers for each provider? like in your example, it would be Expedia.dk=2 , Agoda = 4, Hotels.com=2, getaroom.com=2 – Ashley Nov 10 '16 at 11:52
  • I think it not possible as all threads are running parallelly and each thread run its own copy of BeanShell PostProcessor. you have to do this post execution. – Naveen Kumar R B Nov 10 '16 at 11:54