In the past, I have shared two articles about the ThreadLocal class: using ThreadLocal to solve thread synchronization problems and demonstrating the ThreadLocal functionality in Java. Today, I will share a simple application of ThreadLocal in testing a link performance test I did earlier.
Requirements and Logic
demand
A requirement is the ability for the user (in login state) to de-favorites and de-favorites a resource (identified by minisource_id).
The value of key=meta in the outer JSON object in the corresponding structure is a JSON object, and the key=ecode in the value must be 0.
logic
Bookmark, then unbookmark, as a link for performance testing. This example I used in the link pressure test how to record every time request, if you are interested, you can go to see.
Train of thought
Depending on the functionality and usage scenario of the ThreadLocal class, I initialized an extremely long minisource_ID List object in the function class OKClass to store the IDS that the test might need. The index position is then marked by a thread-safe AtomicInteger object, which returns the different minisource_id in the initialValue() method.
The rule is that each time the initialValue() method is executed, the index index is incremented by 1. This ensures that the minisource_ID used by each thread when calling the method of the function object is different.
Functional transformation
There are a lot of functional class codes, so I will share the codes involved in this modification as follows:
/**
* 所有可用的id
*/
public static List<Integer> ids = RWUtil.readTxtFileByNumLine(getLongFile("ids"))
/** * index tag */
public static AtomicInteger index = new AtomicInteger(0)
/** * Threads do not share objects */
public static ThreadLocal<Integer> minisource_id = new ThreadLocal() {
@Override
public Integer initialValue(a) {
ids.get(index.getAndIncrement())
}
}
/** * collect OK wisdom lesson *@param minicourse_id
* @paramKtype 0- institution, 1- Teacher *@return* /
public JSONObject collect(int minicourse_id = minisource_id.get().int ktype = 0.int grade_id = 12) {
String url = OKClassApi.COLLECT
def params = getParams()
params.put("minicourse_id", minicourse_id);
params.put("kid_route"[640]);
params.put("ktype", ktype);
params.put("grade_id", grade_id);
params.put("link_source".1);//0- teacher space, 1- teacher machine
def response = getPostResponse(url, params)
output(response)
response
}
/** * unsubscribe *@param minicourse_id
* @param ktype
* @param grade_id
* @return* /
public JSONObject unCollect(int minicourse_id = minisource_id.get().int ktype = 0) {
String url = OKClassApi.UNCOLLECT
def params = getParams()
params.put("minicourse_id", minicourse_id);
params.put("kid_route"[82]);
params.put("ktype", ktype);
def response = getPostResponse(url, params)
output(response)
response
}
Copy the code
- The way I write it here is that it has a parameter and the default value is, this is
Groovy
Properties, you can think of it asparams.put("minicourse_id", minisource_id.get());
.
Pressure test script
The functions are not changed, share as follows:
package com.okayqa.composer.performance.master1_0
import com.fun.base.constaint.ThreadLimitTimesCount
import com.fun.frame.execute.Concurrent
import com.fun.frame.httpclient.ClientManage
import com.fun.utils.ArgsUtil
import com.okayqa.composer.base.OkayBase
import com.okayqa.composer.function.OKClass
import java.util.concurrent.atomic.AtomicInteger
class BothCollect extends OkayBase {
static AtomicInteger u = new AtomicInteger(0)
static int times = 0
static int thread
public static void main(String[] args) {
ClientManage.init(5.1.0."".0)
def util = new ArgsUtil(args)
thread = util.getIntOrdefault(0.200)
times = util.getIntOrdefault(1.100)
def funs = []
thread.times {
funs << new FunTester()
}
new Concurrent(funs, "Favorites and unfavorites.").start()
allOver()
}
static int getTimes(a) {
return times
}
static class FunTester extends ThreadLimitTimesCount {
OkayBase okayBase = getBase(u.getAndIncrement())
OKClass driver = new OKClass(okayBase)
public FunTester(a) {
super(null, getTimes(), null)}@Override
protected void doing(a) {
def collect = driver.collect()
def value = okayBase.getLastRequestId() + CONNECTOR
this.threadmark += value
if (collect.getJSONObject("meta").getIntValue("ecode") != 0) fail(value + "Request error!")
def collect1 = driver.unCollect()
def value1 = okayBase.getLastRequestId()
this.threadmark += value1
if (collect1.getJSONObject("meta").getIntValue("ecode") != 0) fail(value1 + "Request error!")}}}Copy the code
FunTester.Tencent annual author cloud, excellent lecturer | tencent cloud + community certification authority, non-famous test development, welcome to follow.
- 2020 FunTester self-summary
- How do I test a probabilistic business interface
- Understand stress and load testing in test models
- How can testers be agents of change
- 7 Steps to becoming a Good Automation Test Engineer
- Pitfalls to Avoid for Beginners to Automation (PART 1)
- Pit to avoid for automation novices (bottom) – MoCO fixed QPS interface upgrade compensation mechanism
- How do performance tests reduce native errors
- How does the command line execute methods in jar packages
- How to mock the QPS interface