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

how to get the response size (resource size of a request) #6

Open
KritikaSahare1611 opened this issue Jun 24, 2024 · 1 comment
Open

Comments

@KritikaSahare1611
Copy link

I am using this code , please help me to get

import static java.lang.String.format;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Pattern;

import com.healthkart.constants.FrameworkConstants;

import io.github.sskorol.cdt.protocol.commands.Network;
import io.github.sskorol.cdt.protocol.types.network.Request;
import io.github.sskorol.cdt.services.ChromeDevToolsService;
import io.github.sskorol.cdt.services.exceptions.WebSocketServiceException;

public class ChromeDevToolsHelper {

private static ChromeDevToolsService chromeDevTools;
private static final Pattern URL_PATTERN = Pattern.compile("^%s(?!.*(favicon|manifest|\\.svg|\\.png)).*$");
private static ConcurrentHashMap<String, Request> requests = new ConcurrentHashMap<>();
private static final Object requestsLock = new Object();

public static synchronized void traceNetworkCalls(String testCaseName, ConcurrentHashMap<String, Object> methodNames, ConcurrentHashMap<String, Object> methodNames1,
        CopyOnWriteArraySet<String> listOfNetworkCalls, String baseAppUrl, ConcurrentHashMap<String, Object> resultMap, CopyOnWriteArraySet<String> testCaseNameList)
        throws Exception, WebSocketServiceException {

    if (BrowserFactory.remoteValue.equals("true")) {
        try {
            chromeDevTools = ChromeDevToolsService.from(
                    format(FrameworkConstants.getChromeDevtoolsSelenoidUrl(), DriverManager.getDriver().getSessionId()));
        } catch (Exception e) {
        	System.out.println();
            chromeDevTools = ChromeDevToolsService.from(
                    format(FrameworkConstants.getChromeDevtoolsSelenoidUrl(), DriverManager.getDriver().getSessionId()));
        }

        synchronized (chromeDevTools) {
            Network network = getNetwork(chromeDevTools);

            network.onRequestWillBeSent(e -> {
                synchronized (requestsLock) {
                    requests.put(e.getRequestId(), e.getRequest());
                }
            });

            network.onResponseReceived(entry -> {
                synchronized (listOfNetworkCalls) {
                    synchronized (requestsLock) {
                        ConcurrentHashMap<String, Request> requestsCopy = new ConcurrentHashMap<>(requests);
                        Request request = requestsCopy.get(entry.getRequestId());
                        if (request != null) {
                            String url = request.getUrl();
                            String url1 = request.getUrl();
                            
                            System.out.println(url+" : "+entry.getResponse().getEncodedDataLength());
                            String filePath = System.getProperty("user.dir")+ "/responseSize.txt";
                            logUrlToFile(filePath, url, entry.getResponse().getEncodedDataLength());
                            String urlPattern = String.format(URL_PATTERN.pattern(), Pattern.quote(baseAppUrl));
                            Pattern pattern = Pattern.compile(urlPattern);

                            String replacement = "https://stagapi.healthkart.com/api";
                            String replacement1 = "https://stagapi/healthkart/com/api";

                            System.out.println(url);
                            if (pattern.matcher(url).matches()) {
                                if (url.contains("veronica")) {
                                    url = url.replaceFirst("https://[^/]+/veronica", replacement);
                                    url1 = url1.replaceFirst("https://[^/]+/veronica", replacement1);

                                    if(entry.getResponse().getStatus().intValue() >= 400) {
                                        methodNames1.put(url1, entry.getResponse().getStatus());
                                    }

                                    if(resultMap.containsKey(url1)){

                                        List<List<Double>> listOf = (List<List<Double>>)resultMap.get(url1);
                                        List<Double> listOfSize = new ArrayList<>();
                                        List<Double> listOfTime = new ArrayList<>();

                                        StringBuilder sb = new StringBuilder(testCaseName);
                                        sb.append(url1);
                                        String testcasename_url = sb.toString();
                                        if(!testCaseNameList.contains(testcasename_url)) {
                                            testCaseNameList.add(testcasename_url);
                                            listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                            listOfSize.add(entry.getResponse().getEncodedDataLength());
                                        }
                                        else{
                                             if(listOfSize.get(listOfSize.size()-1)<entry.getResponse().getEncodedDataLength()){
                                                  listOfSize.remove(listOfSize.size()-1);
                                                  listOfTime.remove(listOfTime.size()-1);
                                                  listOfSize.add(entry.getResponse().getEncodedDataLength());
                                                  listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                              }
                                        }
                                        listOf.add(listOfSize);
                                        listOf.add(listOfTime);

                                        resultMap.put(url1, listOf);

                                    } else {

                                        StringBuilder sb = new StringBuilder(testCaseName);
                                        sb.append(url1);
                                        String testcasename_url = sb.toString();
                                        testCaseNameList.add(testcasename_url);

                                        List<List<Double>> result = new ArrayList<>();
                                        List<Double> listOfSize = new ArrayList<>();
                                        List<Double> listOfTime = new ArrayList<>();
                                        listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                        listOfSize.add(entry.getResponse().getEncodedDataLength());
                                        result.add(listOfSize);
                                        result.add(listOfTime);

                                        resultMap.put(url1,result);
                                    }

                                }
                                listOfNetworkCalls.add(url);
                                methodNames.put(testCaseName, listOfNetworkCalls);
                            }else if (url.contains("iron-man")) {
                                    url = url.replaceFirst("https://[^/]+/iron-man", replacement);
                                    url1 = url1.replaceFirst("https://[^/]+/iron-man", replacement1);

                                    if(entry.getResponse().getStatus().intValue() >= 400) {
                                        methodNames1.put(url1, entry.getResponse().getStatus());
                                    }

                                    if(resultMap.containsKey(url1)){

                                        List<List<Double>> listOf = (List<List<Double>>)resultMap.get(url1);
                                        List<Double> listOfSize = new ArrayList<>();
                                        List<Double> listOfTime = new ArrayList<>();

                                        StringBuilder sb = new StringBuilder(testCaseName);
                                        sb.append(url1);
                                        String testcasename_url = sb.toString();
                                        if(!testCaseNameList.contains(testcasename_url)) {
                                            testCaseNameList.add(testcasename_url);
                                            listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                            listOfSize.add(entry.getResponse().getEncodedDataLength());
                                        }
                                        else{
                                            if(listOfSize.get(listOfSize.size()-1)<entry.getResponse().getEncodedDataLength()){
                                                listOfSize.remove(listOfSize.size()-1);
                                                listOfTime.remove(listOfTime.size()-1);
                                                listOfSize.add(entry.getResponse().getEncodedDataLength());
                                                listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                            }
                                        }
                                        listOf.add(listOfSize);
                                        listOf.add(listOfTime);

                                        resultMap.put(url1, listOf);

                                    } else {

                                        StringBuilder sb = new StringBuilder(testCaseName);
                                        sb.append(url1);
                                        String testcasename_url = sb.toString();
                                        testCaseNameList.add(testcasename_url);

                                        List<List<Double>> result = new ArrayList<>();
                                        List<Double> listOfSize = new ArrayList<>();
                                        List<Double> listOfTime = new ArrayList<>();
                                        listOfTime.add(entry.getResponse().getTiming().getReceiveHeadersEnd());
                                        listOfSize.add(entry.getResponse().getEncodedDataLength());
                                        result.add(listOfSize);
                                        result.add(listOfTime);

                                        resultMap.put(url1,result);
                                    }
                                    listOfNetworkCalls.add(url);
                                    methodNames.put(testCaseName, listOfNetworkCalls);
                                }

                        }
                    }
                }
            });

            network.enable();
        }
    }
}

private static Network getNetwork(ChromeDevToolsService chromeDevTools) {
ThreadLocal threadLocalNetwork = new ThreadLocal<>();
threadLocalNetwork.set(chromeDevTools.getNetwork());
return threadLocalNetwork.get();
}

public static void logUrlToFile(String filePath, String url, Double double1) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) {
writer.write(url + " : " + double1);
writer.newLine(); // Add a newline for readability
} catch (IOException e) {
e.printStackTrace(); // Handle the exception as needed
}
}

}

@vania-pooh
Copy link
Member

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

No branches or pull requests

2 participants