Hello, I've been working on a workflow that involv...
# general
p
Hello, I've been working on a workflow that involves making parallel API calls to improve processing speed. However, I've observed an unexpected increase in latency as I increase the number of parallel threads. For instance, when making 2 API calls with 1 thread each, each API takes approximately 0.5 seconds, totaling around 1 second. However, when making 2 API calls with 2 threads each, each API takes 1 second, once again totaling around 1 second. The more the threads, the slower the API, defeating the purpose of concurrency. Has anyone experienced similar behavior ?
b
promises make your code asynchronous, it doesnt allow you to make much more requests
your code can only open a finite number of requests at the same time (2)
if you try to make more at the same time, the code just waits until the prior one finishes
p
Hi @battk I am calling two http APIs in parallel in two different threads. My expectation is that latency is more or less the same, but that dosen't seem to be the case, as latency is increasing along with the number of threads.
b
what does the code look like
p
Copy code
public class MultiThreadTest {
    static final WsClient nsClient;

    static {
        try {
            nsClient = getNsClient();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, MalformedURLException, RemoteException {
        ForkJoinPool customThreadPool = new ForkJoinPool(1);
        customThreadPool.submit(
                () -> IntStream.range(1, 10).parallel()
                        .forEach(MultiThreadTest::addCustomer)
        ).get();
    }

    static void addCustomer( int i) {
        String email = "C_" + i + "@test.com";
        long timeTaken;
        try {
            long start = System.currentTimeMillis();
            nsClient.callSearch(getCustomersByEmail(email));
            long end = System.currentTimeMillis();
            timeTaken = end - start;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(" search request time taken " + timeTaken);
    }
}
b
is the nsClient good enough to handle the concurrency level of the netsuite account
p
@battk Yes, I have allocated 3 concurrency units for this. But still exhibits the same behaviour on parallel calls.
b
its the code of the nsClient that you want to worry about
p
@battk The client just creates a soap api and executes it, i've tried running isolated requests as well, there is a increased latency from netsuite side in general when we have multiple threads.
b
still not really enough information shared to debug the issue
ill simply say that using concurrency generally doesnt make each individual request take longer
if you think it does, you want to bring it up with netsuite support, though you will have to do better than what you have done here
p
@battk Thank you for your inputs. I got this client jar from NetSuite. https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_N3421207.html#Downloading-Sample-Applications Let me dig deeper into the client jar and see if there is some limitation in calling multiple requests.