During interface testing, it is common for some interfaces to call other service interfaces or third-party interfaces. This can be a problem when doing a pressure test because it is difficult to isolate performance changes from other services and third-party interfaces. While maintaining a separate pressure test environment can solve the problem of service invocation, it requires a lot of resources and effort and is not necessarily suitable for every team. Third parties are harder to master, and it’s nice to be able to provide some performance data.

To do this we need to mock a fixed QPS interface. My solution was based on the MOCO API, and I tried to make use of the functionality provided by myself. Unfortunately, I failed. I spent an hour or so reading official documents and implementing the Demo, and I tried to find this way out.

I had to give up and expand the feature myself. The Semaphore class in the JDK controls the traffic, and then restricts the flow of the interface by creating a custom ResponseHandler. The idea is that the requesting thread that gets the token sleeps for a while before releasing the token to complete the response.

After my test, the error is within 10%. If the test plan is well designed, the error should be within 5%. Here are several rules:

  • The more threads you request, the smaller the error
  • The more requests, the smaller the error
  • When the system is fully preheated, the error is smaller

I will record a video to share with you.

Use the Demo

        HttpServer server = getServer(8088)

        server.get(urlOnly("/aaa")).response(qps(textRes("faun"), 10))

        server.response("haha")

        MocoServer drive = run(server)


        waitForKey("fan")

        drive.stop()
Copy the code

Encapsulation method


/** * create ResponseHandler with fixed QPS, default QPS=1 *@param handler
 * @return* /
    static ResponseHandler qps(ResponseHandler handler) {
        QPSHandler.newSeq(handler, 1000)}/** * create ResponseHandler * for fixed QPS@param handler
 * @param gap
 * @return* /
    static ResponseHandler qps(ResponseHandler handler,int gap) {
        QPSHandler.newSeq(handler, gap)
    }
    
Copy the code

ResponseHandler implementation class


package com.fun.moco.support


import com.github.dreamhead.moco.ResponseHandler
import com.github.dreamhead.moco.handler.AbstractResponseHandler
import com.github.dreamhead.moco.internal.SessionContext
import com.github.dreamhead.moco.util.Idles

import java.util.concurrent.Semaphore
import java.util.concurrent.TimeUnit

import static com.google.common.base.Preconditions.checkArgument 
/** ** Fixed QPS interface implementation class */
class QPSHandler extends AbstractResponseHandler {


    private static final Semaphore semaphore = new Semaphore(1.true);
    /** ** Access interval */
    private final int gap

    private final ResponseHandler handler

    private QPSHandler(ResponseHandler handler, int gap) {
        this.gap = gap
        this.handler = handler
    }

    public static ResponseHandler newSeq(final ResponseHandler handler, intgap) { checkArgument(handler ! =null."Responsehandler cannot be empty!");
        return new QPSHandler(handler, gap);
    }


/** * concrete implementation, here uses microseconds, experiments prove that microseconds more accurate *@param context
 */
    @Override
    void writeToResponse(SessionContext context) {
        semaphore.acquire()
        Idles.idle(gap * 1000, TimeUnit.MICROSECONDS)
        handler.writeToResponse(context)
        semaphore.release()
    }

}

Copy the code

  • Solemnly declare: “FunTester” first, welcome to pay attention to exchange, prohibit third party reprint. More original articles:18 original albums by FunTesterPlease contact for cooperation[email protected].

Hot article selected

  • Function This interface is used to test albums
  • Performance Testing Topics
  • Linux performance monitoring software Netdata Chinese version
  • Graphic HTTP brain map
  • Interface Test Code Coverage (JACOCO) solution sharing
  • How does the test handle Java exceptions
  • Programming thinking for everyone
  • The Definitive Guide to Java Performance
  • Selenium parallel testing best practices
  • Performance testing, stress testing, and load testing
  • How do I maintain automated tests