Operations 7 min read

Load Testing Create and Delete Resource APIs with a Java Automation Script

This article walks through the design, parameterization, and Java implementation of load‑testing scripts for two related APIs—one that creates a resource and another that deletes it—covering special handling of duplicate IDs, method encapsulation with default arguments, concurrent execution, and visual result analysis.

FunTester
FunTester
FunTester
Load Testing Create and Delete Resource APIs with a Java Automation Script

Simple Function Description

The first API creates a resource using parameters such as knowledge point ID (kid), class ID, class type (0‑admin class, 3‑teaching class), course type, subject ID, minicourse ID, activity name, page index, and page size. A successful response returns a JSON object containing the newly generated activity_id.

params.put("kid", kid);
params.put("class_id", class_id);
params.put("class_type", 3); // 0‑admin class, 3‑teaching class
params.put("course_type", course_type);
params.put("subject_id", subject_id);
params.put("minicourse_id", minicourse_id);
params.put("activity_name", activity_name);
params.put("page_index", 1);
params.put("page_size", 10);

Response example:

{
  "data": {
    "activity_id": 674
  },
  "meta": {
    "emsg": "",
    "ecode": 0
  }
}

Delete Interface

The second API deletes the resource identified by the activity_id returned from the first call.

JSONObject params = getParams();
params.put("activity_id", activity_id);

Response format is similar, with a JSON object containing meta information.

A special case: for the same minicourse_id, the creation request always returns the same ID, and the delete operation is actually an UPDATE in the database. To avoid interference between concurrent threads, the test binds each thread to a specific user’s test data.

Method Encapsulation

Two helper methods— createPre and delPre —are wrapped with default parameter values to simplify calls.

/** Create a lesson preparation */
public JSONObject createPre(int kid = 68, int class_id = 50167, int course_type = 4,
                           int subject_id = 6, int minicourse_id = 2109354,
                           String activity_name = "FunTester") {
    String url = MirroApi.CREATE_PRE;
    JSONObject params = getParams();
    params.put("kid", kid);
    params.put("class_id", class_id);
    params.put("class_type", 3);
    params.put("course_type", course_type);
    params.put("subject_id", subject_id);
    params.put("minicourse_id", minicourse_id);
    params.put("activity_name", activity_name);
    params.put("page_index", 1);
    params.put("page_size", 10);
    JSONObject response = getPostResponse(url, params);
    output(response);
}

/** Delete a lesson preparation */
public JSONObject delPre(int activity_id = 343) {
    String url = MirroApi.DEL_PRE;
    JSONObject params = getParams();
    params.put("activity_id", activity_id);
    JSONObject response = getPostResponse(url, params);
    output(response);
}

Load Test Script

The following Java script launches 200 concurrent threads, each creating a resource and then deleting it, while recording the activity_id for subsequent deletion.

package com.okayqa.composer;

import com.fun.base.constaint.ThreadLimitTimesCount;
import com.fun.frame.excute.Concurrent;
import com.okayqa.composer.base.OkayBase;
import com.okayqa.composer.function.Mirro;

class COMT extends OkayBase {

    public static void main(String[] args) {
        def res = [];
        200.times {
            def base1 = getBase(it);
            def mirro1 = new Mirro(base1);
            res << new TT(mirro1, 50);
        }
        new Concurrent(res, "创建备课删除备课").start();
        allOver();
    }

    static class TT extends ThreadLimitTimesCount {
        Mirro m;
        int aid = 0;

        public TT(Mirro mirro, int times) {
            super();
            this.m = mirro;
            this.times = times;
        }

        @Override
        protected void doing() throws Exception {
            def pre = m.createPre();
            if (aid == 0) {
                aid = pre.getJSONObject("data").getIntValue("activity_id");
            }
            m.delPre(aid);
        }
    }
}

Other load‑test scripts for reference (URLs):

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247485658&idx=1&sn=58b3cb51daf95633193e714da934583b

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247485693&idx=1&sn=039a59bb9011d17c7ab34b77a019930f

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247485016&idx=1&sn=c55fba30d182110faf4a47b8c367a5de

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247485058&idx=1&sn=251f94ef77a96ee058bc4704cce63246

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247484542&idx=1&sn=5c3bdfceceb41da89700e26282b4e797

https://mp.weixin.qq.com/s?__biz=MzU4MTE2NDEyMQ==∣=2247484548&idx=1&sn=f7d8dc9ddf1ed2087d50093b2b449481

Load Test Results

Three charts generated by the Java utility illustrate response times, success rates, and throughput.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentPerformance TestingLoad TestingAPI
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.