package com.example.demo.thread;
/**
* @title: Pojo
* @Author kunkun
* @Date: 2022/4/13 11:08
* @Version 1.0
*/
public class Pojo {
private Integer count;
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
public Integer getOk() {
return ok;
}
public void setOk(Integer ok) {
this.ok = ok;
}
public Double getSuccessRate() {
return successRate;
}
public void setSuccessRate(Double successRate) {
this.successRate = successRate;
}
private Integer ok;
private Double successRate;
}
package com.example.demo.thread;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.auto.StringUtils;
import lombok.SneakyThrows;
import okhttp3.OkHttpClient;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 指定数量线程堆积后一次性并发测试
*/
public class LatchTest {
public static void main(String[] args) throws InterruptedException {
int threadNums = 100;
String path = "d:/ipidea"+threadNums+"测试结果1.xlsx";
List<Pojo> pojos = new ArrayList<>();
Map<String, Integer> map = new HashMap<String, Integer>();
OkHttpClient client = MyThread.okHttpClient();
String url = "https://api.live.bilibili.com/client/v1/Ip/getInfoNew";
for (int i = 0; i < 1; i++) {
AtomicInteger count = new AtomicInteger(0);
AtomicInteger iproyalcount = new AtomicInteger(0);
AtomicInteger oxylabscount = new AtomicInteger(0);
AtomicInteger lumcount = new AtomicInteger(0);
AtomicInteger packetcount = new AtomicInteger(0);
AtomicInteger errcount = new AtomicInteger(0);
Runnable taskTemp = new Runnable() {
@SneakyThrows
@Override
public void run() {
Integer randomData = RandomDataUtils.getRandomData(7, 106);
String authUser = "monitorzz-" + randomData;//死循环随机子账号
// String authUser = "monitorzz-";//for循环子账号
// String authUser = "lum-customer-c_7af1e69a-zone-zone2-route_err-pass_dyn-country-us-session-"+CodeUtils.randomPas(11);//for循环子账号
String authPassword = "IQA1LI2I";
String proxyHost = "gate.hide-proxy.pro";
// String proxyHost = "47.241.220.62";
Integer port = 1234;
String ip = ProxyConnect1.test(authUser, authPassword, proxyHost, port, url, null, client);
if(StringUtils.equals(ip,"ok")){
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : count.getAndIncrement());
}
if(StringUtils.equals(ip,"iproyal")){
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : iproyalcount.getAndIncrement());
}
if(StringUtils.equals(ip,"oxylabs")){
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : oxylabscount.getAndIncrement());
}
if(StringUtils.equals(ip,"superproxy")){
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : lumcount.getAndIncrement());
}
if(StringUtils.equals(ip,"packetstream")){
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : packetcount.getAndIncrement());
}
Thread.sleep(3000);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
ip = "err";
Integer integer = map.get(ip);
map.put(ip, (integer == null) ? 1 : errcount.getAndIncrement());
e.printStackTrace();
}
}
};
LatchTest latchTest = new LatchTest();
latchTest.startTaskAllInOnce(threadNums, taskTemp);
Pojo pojo = new Pojo();
pojo.setCount(threadNums);
pojo.setOk(map.get("ok"));
pojo.setIproyal(map.get("iproyal"));
pojo.setOxylabs(map.get("oxylabs"));
pojo.setSuperproxy(map.get("superproxy"));
pojo.setPacketstream(map.get("packetstream"));
pojo.setPacketstream(map.get("err"));
BigDecimal a = new BigDecimal(map.get("ok"));
BigDecimal b = new BigDecimal(threadNums);
BigDecimal divide = a.divide(b,4, RoundingMode.HALF_UP);
DecimalFormat df = new DecimalFormat("0.00%");
String s=df.format(divide);
pojo.setSuccessRate(s);
pojos.add(pojo);
map.remove("ok");
map.remove("iproyal");
map.remove("oxylabs");
map.remove("superproxy");
map.remove("packetstream");
map.remove("err");
// for (Map.Entry<String, Integer> entry : map.entrySet()) {
// System.out.println("Key : " + entry.getKey() + " Value : "
// + entry.getValue());
// }
}
for (Pojo pojo:pojos
) {
System.out.println("并发次数 "+pojo.getCount());
System.out.println("成功次数 "+pojo.getOk());
System.out.println("成功率 "+pojo.getSuccessRate());
System.err.println("geo.iproyal.com失败次数 "+pojo.getIproyal());
System.err.println("pr.oxylabs.io失败次数 "+pojo.getOxylabs());
System.err.println("zproxy.lum-superproxy.io失败次数 "+pojo.getSuperproxy());
System.err.println("proxy.packetstream.io失败次数 "+pojo.getPacketstream());
System.err.println("无法链接次数 "+pojo.getErr());
System.err.println("==============");
}
// 通过工具类创建writer
// ExcelWriter writer = ExcelUtil.getWriter(path);
// 合并单元格后的标题行,使用默认标题样式
// writer.merge(map.size() - 1, "混合池并发测试结果");
// 一次性写出内容,使用默认样式,强制输出标题
// writer.write(pojos, true);
// 关闭writer,释放内存
// writer.close();
}
public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endGate = new CountDownLatch(threadNums);
for(int i = 0; i < threadNums; i++) {
Thread t = new Thread() {
@Override
public void run() {
try {
// 使线程在此等待,当开始门打开时,一起涌入门中
startGate.await();
try {
task.run();
} finally {
// 将结束门减1,减到0时,就可以开启结束门了
endGate.countDown();
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
};
t.start();
}
long startTime = System.nanoTime();
System.out.println(startTime + " [" + Thread.currentThread() + "] All thread is ready, concurrent going...");
// 因开启门只需一个开关,所以立马就开启开始门
startGate.countDown();
// 等等结束门开启
endGate.await();
long endTime = System.nanoTime();
System.out.println(endTime + " [" + Thread.currentThread() + "] All thread is completed.");
return endTime - startTime;
}
}
package com.example.demo.thread;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.example.demo.auto.JsonUtils;
import com.example.demo.auto.StringUtils;
import okhttp3.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.util.List;
/**
* @title: ProxyConnect
* @Author kunkun
* @Date: 2022/3/10 8:58
* @Version 1.0
*/
public class ProxyConnect1 {
// public static void main(String[] args) throws IOException {
// test("","","",1,"",null,new OkHttpClient());
// }
public static String test(String authUser, String authPassword, String proxyHost, int proxyPort, String destURL, List<String> list,OkHttpClient okHttpClient) throws MalformedURLException,
ProtocolException,
IOException {
String ip ="";
Thread thread = Thread.currentThread();
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");
System.setProperty("jdk.http.auth.proxying.disabledSchemes", "");
okhttp3.Authenticator proxyAuthenticator = new okhttp3.Authenticator() {@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic(authUser, authPassword);
return response.request().newBuilder()
.header("Proxy-Authorization", credential)
.build();
}
};
OkHttpClient newOkHttpClient = okHttpClient.newBuilder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
.proxyAuthenticator(proxyAuthenticator)
.authenticator(proxyAuthenticator).build();
Request request = new Request.Builder().url(destURL).header("Connection", "close").build();
Response response = null;
try {
response = newOkHttpClient.newCall(request).execute();
} catch (Exception e) {
OkHttpClient client = MyThread.okHttpClient();
String qiehuanIp = "https://s.hideproxy.net/index/lk?user="+authUser+"&pass="+authPassword;
Request request1 = new Request.Builder()
.url(qiehuanIp)
.method("GET", null)
.build();
Response response1 = client.newCall(request1).execute();
if(response1.code() == 204){
String upstream = response1.header("Upstream");
System.err.print(Thread.currentThread().getName()+ " "+authUser+" 获取上游成功: "+upstream+" ");
if(upstream.contains("iproyal")){
ip = "iproyal";
}
if(upstream.contains("oxylabs")){
ip = "oxylabs";
}
if(upstream.contains("superproxy")){
ip = "superproxy";
}
if(upstream.contains("packetstream")){
ip = "packetstream";
}
}
System.err.println(e);
}
if(ObjectUtil.isNotNull(response)){
String str = response.body().string();
System.out.println(Thread.currentThread().getName()+" "+"当前子账号是:"+authUser+" "+str);
// Object succesResponse = JSON.parse(str);
// Object addr = JsonUtils.getValueByKey(succesResponse, "addr");
ip="ok";
}
// Request request = new Request.Builder().url(destURL).build();
// //异步请求
// Call call = newOkHttpClient.newCall(request);
// call.enqueue(new Callback() {
// @Override
// public void onFailure(Call call, IOException e) {
// System.out.println(Thread.currentThread().getName()+" "+"当前子账号是: "+authUser+"请求失败");
// }
// @Override
// public void onResponse(Call call, Response response) throws IOException {
// System.out.println(Thread.currentThread().getName()+" "+"当前子账号是: "+authUser+response.body().string());
// }
// });
return ip;
}
}