網頁主動探測工具使用

來源:互聯網
上載者:User

標籤:style   http   io   ar   color   os   使用   sp   java   

?

  單位的項目是IBatis做的,每個查詢的SQL裡面都有很多判斷
   上次最佳化SQL之後,其中的一個分支報錯,但是作為dba,不可能排查每一個分支.
   所以,乾脆用爬蟲爬過所有的網頁,主動探測程式的異常.
   這樣有兩個好處 
   1.可以主動查看網頁是否異常 (500錯誤,404錯誤) 
   2.可以篩查速度較慢的網頁,從這個方向也可以定位慢SQL吧.(也有伺服器資源不足,造成網路逾時的情況) 
   前提, 
   必須是互連網公司,大多數網頁不用登入也可以瀏覽 
   首先,建表 
   CREATE SEQUENCE seq_probe_id INCREMENT BY 1 START WITH 1 NOMAXvalue NOCYCLE CACHE 2000; 
   create table probe( 
   id int primary key, 
   host varchar(40) not null, 
   path varchar(500) not null, 
   state int not null, 
   taskTime int not null, 
   type varchar(10) not null, 
   createtime date default sysdate not null 
   ) ; 
   其中host是網域名稱,path是網頁的相對路徑,state是HTTP狀態代碼,taskTime是網頁擷取時間,單位是毫秒,type是類型(html,htm,jpg等) 
   程式結構

  程式分三個主要步驟,再分別用三個隊列實現生產者消費者模式.
   1.串連.根據串連隊列的目標,使用Socket擷取網頁,然後放入解析隊列
   2.解析.根據解析隊列的內容,使用Regex擷取該網頁的合法串連,將其再放入串連隊列.然後將解析的網頁放入持久化隊列 
   3.持久化.將持久化隊列的內容存入資料庫,以便查詢。 
   程式使用三個步驟並行,每個步驟可以並發的方式.

$new_page$

  但是通常來說,解析和持久化可以分別用單線程的方式執行.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Probe {
private static final BlockingQueue<Task> CONNECTLIST = new LinkedBlockingQueue<Task>();
private static final BlockingQueue<Task> PARSELIST = new LinkedBlockingQueue<Task>();
private static final BlockingQueue<Task> PERSISTENCELIST = new LinkedBlockingQueue<Task>();
private static ExecutorService CONNECTTHREADPOOL;
private static ExecutorService PARSETHREADPOOL;
private static ExecutorService PERSISTENCETHREADPOOL;
private static final List<String> DOMAINLIST = new CopyOnWriteArrayList<>();
static {
CONNECTTHREADPOOL = Executors.newFixedThreadPool(200);
PARSETHREADPOOL = Executors.newSingleThreadExecutor();
PERSISTENCETHREADPOOL = Executors.newFixedThreadPool(1);
DOMAINLIST.add("網域名稱");
}
public static void main(String args[]) throws Exception {
long start = System.currentTimeMillis();
CONNECTLIST.put(new Task("網域名稱", 80, "/static/index.html"));
for (int i = 0; i < 600; i++) {
CONNECTTHREADPOOL.submit(new ConnectHandler(CONNECTLIST, PARSELIST));
}
PARSETHREADPOOL.submit(new ParseHandler(CONNECTLIST, PARSELIST, PERSISTENCELIST, DOMAINLIST));
PERSISTENCETHREADPOOL.submit(new PersistenceHandler(PERSISTENCELIST));
while (true) {
Thread.sleep(1000);
long end = System.currentTimeMillis();
float interval = ((end - start) / 1000);
int connectTotal = ConnectHandler.GETCOUNT();
int parseTotal = ParseHandler.GETCOUNT();
int persistenceTotal = PersistenceHandler.GETCOUNT();
int connectps = Math.round(connectTotal / interval);
int parseps = Math.round(parseTotal / interval);
int persistenceps = Math.round(persistenceTotal / interval);
System.out.print(" 串連總數:" + connectTotal + " 每秒串連:" + connectps + " 串連隊列剩餘:" + CONNECTLIST.size()
+ " 解析總數:" + parseTotal + " 每秒解析:" + parseps + " 解析隊列剩餘:" + PARSELIST.size() + " 持久化總數:"
+ persistenceTotal + " 每秒持久化:" + persistenceps + " 持久化隊列剩餘:" + PERSISTENCELIST.size());
}
}
}
class Task {
public Task() {
}
public void init(String host, int port, String path) {
this.setCurrentPath(path);
this.host = host;
this.port = port;
}
public Task(String host, int port, String path) {
init(host, port, path);
}
private String host;
private int port;
private String currentPath;
private long taskTime;
private String type;
private String content;
private int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getCurrentPath() {
return currentPath;
}
public void setCurrentPath(String currentPath) {
this.currentPath = currentPath;
this.type = currentPath.substring(currentPath.indexOf(".") + 1,
currentPath.indexOf("?") != -1 ? currentPath.indexOf("?") : currentPath.length());
}
public long getTaskTime() {
return taskTime;
}
public void setTaskTime(long taskTime) {
this.taskTime = taskTime;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
class ParseHandler implements Runnable {
private static Set<String> SET = new ConcurrentSkipListSet<String>();
public static int GETCOUNT() {
return COUNT.get();
}
private static final AtomicInteger COUNT = new AtomicInteger();
private BlockingQueue<Task> connectlist;
private BlockingQueue<Task> parselist;
private BlockingQueue<Task> persistencelist;
List<String> domainlist;

本文選自:http://www.spasvo.com/news/html/2014122144836.html

?

網頁主動探測工具使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.