📄 useperformancecounter.java
字号:
/* -*- mode:java; indent-tabs-mode:nil; c-basic-offset:2 -*- * * $RCSFile$ $Revision: 1.12 $ $Date: 2006/02/01 00:20:28 $ * * Copyright (c) 2002 Autonomy Corp. All Rights Reserved. * Permission to use, copy, modify, and distribute this file is hereby * granted without fee, provided that the above copyright notice appear * in all copies. */import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.Iterator;import java.util.NoSuchElementException;import java.util.Map;import com.ultraseek.xpa.search.*;import com.ultraseek.xpa.ldap.LDAPServer;import com.ultraseek.xpa.server.UltraseekServer;import com.ultraseek.xpa.server.StatsCollection;import com.ultraseek.xpa.server.PerformanceCounters;/** * A simple demo application that searches * a running Ultraseek instance. */public class UsePerformanceCounter { UsePerformanceCounter() { } public void PrintStatCollection (StatsCollection s){ if (s == null) { System.out.println("Entry is null"); return; } try { System.out.print("Num: " + Integer.toString((int) s.getNumOccurrences()) + " "); System.out.print(" Avg val: " + Double.toString(s.getAvg()) + " "); System.out.print(" Max val: " + Double.toString(s.getMax()) + " "); System.out.print(" Min val: " + Double.toString(s.getMin()) + " "); System.out.println(" Std dev: " + Double.toString(s.getStdDev())); } catch (IllegalStateException e) { System.out.println(e.getMessage()); } } // ------------------------------ public static void main(String[] args) throws Exception { UsePerformanceCounter upc = new UsePerformanceCounter(); InputStreamReader inputStreamReader = new InputStreamReader(System.in); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String whichServer = null; Searchable searchable = null; PerformanceCounters pc = null; System.out.println(); System.out.println("This is a simple demo application that searches"); System.out.println("either an LDAP Server or a running UltraSeek instance"); System.out.println("providing relevant performance counter stats on request."); System.out.println(); for (;;) { System.out.print("Choose: a) Ultraseek b) LDAP: "); whichServer = bufferedReader.readLine(); if (whichServer.equals("a") || whichServer.equals("b")) { break; } } if (whichServer.equals("a")) { System.out.println("Enter the hostname and TCP port of your"); System.out.println("running Ultraseek instance."); System.out.print("hostname: "); String host = bufferedReader.readLine(); System.out.print("TCP port: "); int port = Integer.parseInt(bufferedReader.readLine()); UltraseekServer US = new UltraseekServer(host,port); pc = (PerformanceCounters) US; searchable = US; } else { // set the LDAP server System.out.println("Enter the hostname of your"); System.out.println("running LDAP server instance."); System.out.print("hostname: "); String host = bufferedReader.readLine(); LDAPServer LDsearchServer = new LDAPServer(); LDsearchServer.setHost(host); System.out.println(); System.out.println("Change default base DN (dc=?) of \""+LDsearchServer.getBaseDN()+ "\"? (press return to accept)"); System.out.print("new base DN: "); String baseDN = bufferedReader.readLine(); if (baseDN.trim().equals("")) { LDsearchServer.defineSearchCollection("ldap",LDsearchServer.getBaseDN()); } else { LDsearchServer.defineSearchCollection("ldap",baseDN); } SearchCollection ldapSearchCollection = LDsearchServer.getSearchCollection("ldap"); searchable = new RankingSearchable(ldapSearchCollection); pc = (PerformanceCounters) LDsearchServer; } // set the output for the log messages to System.out // searchServer.setAllLogStreams(System.out); pc.setBadConnLog(System.out); pc.setSuccessfulConnLog(System.out); for (;;) { System.out.println(); for (;;) { System.out.print("Either enter a query [q], stats [s], or exit [e]:"); String line = bufferedReader.readLine(); if (line==null) continue; if (line.equals("e")) { System.exit(0); } else if (line.equals("s")) { System.out.println("Enter the time period (in seconds) over which stats are summarized."); System.out.print("period: "); line = bufferedReader.readLine(); long timePeriod = 0; try { timePeriod = Long.parseLong(line); } catch (NumberFormatException n) { } finally { if (timePeriod <= 0) { System.out.println("Bad number format"); continue; } } // StatsCollection succConn = searchServer.getSuccessfulConnStats(timePeriod); // StatsCollection badConn = searchServer.getBadConnStats(timePeriod); // StatsCollection cacheSize = searchServer.getCacheSizeStats(timePeriod); // StatsCollection cacheHit = searchServer.getCacheHitStats(timePeriod); Map allStats = pc.getAllStats(timePeriod); StatsCollection succConn = (StatsCollection) allStats.get(PerformanceCounters.SUCC_CONN_STATKEY); StatsCollection badConn = (StatsCollection) allStats.get(PerformanceCounters.BAD_CONN_STATKEY); StatsCollection cacheSize = (StatsCollection) allStats.get(PerformanceCounters.CACHE_SIZE_STATKEY); StatsCollection cacheHit = (StatsCollection) allStats.get(PerformanceCounters.CACHE_HIT_STATKEY); System.out.print ("Successful connection stats => "); upc.PrintStatCollection(succConn); System.out.print ("Broken/problematic connection stats => "); upc.PrintStatCollection(badConn); System.out.print ("Cache size stats => "); upc.PrintStatCollection(cacheSize); System.out.print ("Cache hitrate stats => "); upc.PrintStatCollection(cacheHit); } else if (line.equals("q")) { break; } } System.out.println("Enter a query."); System.out.print("search: "); String line = bufferedReader.readLine(); if (line==null) break; Query query = Query.parse(line); SearchResultList searchResultList = searchable.search(query); System.out.println(); if (!searchResultList.isEmpty()) { // Don't print out all the SearchResult objects. That's not // the point here. System.out.println("Here are the first two SearchResult objects:"); Iterator iterator = searchResultList.iterator(); try { int i = 0; while (iterator.hasNext()) { SearchResult searchResult = (SearchResult)iterator.next(); System.out.println(); System.out.println(searchResult); if (i == 1) { break; } i++; } } catch (NoSuchElementException e) { /* No more results from server, not an error. */ } } else { System.out.println("There are no SearchResult objects."); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -