JS-based AB testing solution

The AB test on the client side has always been quite disgusting. Basically, it is necessary to preset the code, if-else invades the original logic, and think about all the test solutions before the release. New additions can only be released.
Of course, the ultimate solution like RN solves all the problems caused by the inability to modify the client-side logic, and there is a simpler method: AB testing based on JS engine + JS.

  1. To build a JS engine in the APP, J2V8 is pretty good, and there are some others . At present, it is relatively easy to integrate.

  2. Define an interface as a pit reserved for AB testing. This interface can be large or small, it can be a StringGetter, or it can be the entire presenter. The interaction between JS and native is based on this interface.

  3. The native layer uses Proxy to proxy all the logic of the interface to JS. This contains a mapping relationship from interface to JS file. There can be only one js file in the whole APP, or it can be mapped with class name.
  4. The AB test server no longer sends test parameters, but sends experimental logic (a command or the entire page logic). There is no more weird if-else, and the content that can be tested without a version is more flexible (of course not as good as RN).

The core code is only ten lines:

class JSInvocationHandler implements InvocationHandler {
    private V8 mEngine = V8.createV8Runtime();
    private StringBuilder mBuilder = new StringBuilder();
    private Gson mGson = new Gson();

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
      if (method.getName().equals("dispose")) {
        mEngine.release();
        return null;
      }
      mBuilder.delete(0, mBuilder.length());
      mBuilder.append(method.getName()).append("(").append(mGson.toJson(objects)).append(")");
      return mEngine.executeScript(mBuilder.substring(0));
    }
  }

A few lines have made my heart knot for several years. Refreshing. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325837284&siteId=291194637