提交 aab00fa3 authored 作者: Thomas Mueller's avatar Thomas Mueller

Formatting / javadocs

上级 ccf8ee20
......@@ -218,8 +218,7 @@ public class SetTypes {
* The type of a SET QUERY_STATISTICS_ACTIVE statement.
*/
public static final int QUERY_STATISTICS = 41;
private static final ArrayList<String> TYPES = New.arrayList();
private SetTypes() {
......
......@@ -2094,7 +2094,7 @@ public class Database implements DataHandler {
public boolean getQueryStatistics() {
return queryStatistics;
}
public QueryStatisticsData getQueryStatisticsData() {
if (!queryStatistics) {
return null;
......@@ -2108,7 +2108,7 @@ public class Database implements DataHandler {
}
return queryStatisticsData;
}
/**
* Check if the database is currently opening. This is true until all stored
* SQL statements have been executed.
......
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.engine;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Maintains query statistics.
*/
public class QueryStatisticsData {
private static final int MAX_QUERY_ENTRIES = 100;
private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR = new Comparator<QueryEntry>() {
@Override
public int compare(QueryEntry o1, QueryEntry o2) {
return (int) Math.signum(o1.lastUpdateTime - o2.lastUpdateTime);
}
};
private final HashMap<String, QueryEntry> map = new HashMap<String, QueryEntry>();
public synchronized List<QueryEntry> getQueries() {
// return a copy of the map so we don't have to worry about external synchronization
ArrayList<QueryEntry> list = new ArrayList<QueryEntry>();
list.addAll(map.values());
// only return the newest 100 entries
Collections.sort(list, QUERY_ENTRY_COMPARATOR);
return list.subList(0, Math.min(list.size(), MAX_QUERY_ENTRIES));
}
/**
* Update query statistics.
*
* @param sqlStatement the statement being executed
* @param executionTime the time in milliseconds the query/update took to execute
* @param rowCount the query or update row count
*/
public synchronized void update(String sqlStatement, long executionTime, int rowCount) {
QueryEntry entry = map.get(sqlStatement);
if (entry == null) {
entry = new QueryEntry();
entry.sqlStatement = sqlStatement;
entry.executionTimeMin = executionTime;
entry.executionTimeMax = executionTime;
entry.rowCountMin = rowCount;
entry.rowCountMax = rowCount;
entry.executionTimeMean = executionTime;
entry.rowCountMean = rowCount;
map.put(sqlStatement, entry);
} else {
entry.count++;
entry.executionTimeMin = Math.min(executionTime, entry.executionTimeMin);
entry.executionTimeMax = Math.max(executionTime, entry.executionTimeMax);
entry.rowCountMin = Math.min(rowCount, entry.rowCountMin);
entry.rowCountMax = Math.max(rowCount, entry.rowCountMax);
double delta = rowCount - entry.rowCountMean;
entry.rowCountMean += delta / entry.count;
entry.rowCountM2 += delta * (rowCount - entry.rowCountMean);
delta = executionTime - entry.executionTimeMean;
entry.executionTimeMean += delta / entry.count;
entry.executionTimeM2 += delta * (executionTime - entry.executionTimeMean);
}
entry.executionTimeCumulative += executionTime;
entry.rowCountCumulative += rowCount;
entry.lastUpdateTime = System.currentTimeMillis();
// Age-out the oldest entries if the map gets too big.
// Test against 1.5 x max-size so we don't do this too often
if (map.size() > MAX_QUERY_ENTRIES * 1.5f) {
// Sort the entries by age
ArrayList<QueryEntry> list = new ArrayList<QueryEntry>();
list.addAll(map.values());
Collections.sort(list, QUERY_ENTRY_COMPARATOR);
// Create a set of the oldest 1/3 of the entries
HashSet<QueryEntry> oldestSet = new HashSet<QueryEntry>(list.subList(0, list.size() / 3));
// Loop over the map using the set and remove the oldest 1/3 of the
// entries.
for (Iterator<Map.Entry<String, QueryEntry>> iter = map.entrySet().iterator(); iter.hasNext();) {
Map.Entry<String, QueryEntry> mapEntry = iter.next();
if (oldestSet.contains(mapEntry.getValue())) {
iter.remove();
}
}
}
}
/**
* The collected statistics for one query.
*/
public static final class QueryEntry {
public String sqlStatement;
public int count = 1;
public long lastUpdateTime;
public long executionTimeMin;
public long executionTimeMax;
public long executionTimeCumulative;
public int rowCountMin;
public int rowCountMax;
public long rowCountCumulative;
// Using Welford's method, see also
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
// http://www.johndcook.com/standard_deviation.html
public double executionTimeMean;
public double executionTimeM2;
public double rowCountMean;
public double rowCountM2;
public double getExecutionTimeStandardDeviation() {
// population standard deviation
return Math.sqrt(executionTimeM2 / count);
}
public double getRowCountStandardDeviation() {
// population standard deviation
return Math.sqrt(rowCountM2 / count);
}
}
}
/*
* Copyright 2004-2013 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.engine;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
/**
* Maintains query statistics.
*/
public class QueryStatisticsData {
private static final int MAX_QUERY_ENTRIES = 100;
private static final Comparator<QueryEntry> QUERY_ENTRY_COMPARATOR = new Comparator<QueryEntry>() {
@Override
public int compare(QueryEntry o1, QueryEntry o2) {
return (int) Math.signum(o1.lastUpdateTime - o2.lastUpdateTime);
}
};
private final HashMap<String, QueryEntry> map = new HashMap<String, QueryEntry>();
public synchronized List<QueryEntry> getQueries() {
// return a copy of the map so we don't have to
// worry about external synchronization
ArrayList<QueryEntry> list = new ArrayList<QueryEntry>();
list.addAll(map.values());
// only return the newest 100 entries
Collections.sort(list, QUERY_ENTRY_COMPARATOR);
return list.subList(0, Math.min(list.size(), MAX_QUERY_ENTRIES));
}
/**
* Update query statistics.
*
* @param sqlStatement the statement being executed
* @param executionTime the time in milliseconds the query/update took to execute
* @param rowCount the query or update row count
*/
public synchronized void update(String sqlStatement, long executionTime, int rowCount) {
QueryEntry entry = map.get(sqlStatement);
if (entry == null) {
entry = new QueryEntry();
map.put(sqlStatement, entry);
}
entry.update(executionTime, rowCount);
// Age-out the oldest entries if the map gets too big.
// Test against 1.5 x max-size so we don't do this too often
if (map.size() > MAX_QUERY_ENTRIES * 1.5f) {
// Sort the entries by age
ArrayList<QueryEntry> list = new ArrayList<QueryEntry>();
list.addAll(map.values());
Collections.sort(list, QUERY_ENTRY_COMPARATOR);
// Create a set of the oldest 1/3 of the entries
HashSet<QueryEntry> oldestSet = new HashSet<QueryEntry>(list.subList(0, list.size() / 3));
// Loop over the map using the set and remove
// the oldest 1/3 of the entries.
for (Iterator<Entry<String, QueryEntry>> it = map.entrySet().iterator(); it.hasNext();) {
Entry<String, QueryEntry> mapEntry = it.next();
if (oldestSet.contains(mapEntry.getValue())) {
it.remove();
}
}
}
}
/**
* The collected statistics for one query.
*/
public static final class QueryEntry {
/**
* The SQL statement.
*/
public String sqlStatement;
/**
* The number of times the statement was executed.
*/
public int count;
/**
* The last time the statistics for this entry were updated,
* in milliseconds since 1970.
*/
public long lastUpdateTime;
/**
* The minimum execution time, in milliseconds.
*/
public long executionTimeMin;
/**
* The maximum execution time, in milliseconds.
*/
public long executionTimeMax;
/**
* The total execution time.
*/
public long executionTimeCumulative;
/**
* The minimum number of rows.
*/
public int rowCountMin;
/**
* The maximum number of rows.
*/
public int rowCountMax;
/**
* The total number of rows.
*/
public long rowCountCumulative;
/**
* The mean execution time.
*/
public double executionTimeMean;
/**
* The mean number of rows.
*/
public double rowCountMean;
// Using Welford's method, see also
// http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
// http://www.johndcook.com/standard_deviation.html
private double executionTimeM2;
private double rowCountM2;
/**
* Update the statistics entry.
*
* @param time the execution time
* @param rows the number of rows
*/
void update(long time, int rows) {
count++;
executionTimeMin = Math.min(time, executionTimeMin);
executionTimeMax = Math.max(time, executionTimeMax);
rowCountMin = Math.min(rows, rowCountMin);
rowCountMax = Math.max(rows, rowCountMax);
double delta = rows - rowCountMean;
rowCountMean += delta / count;
rowCountM2 += delta * (rows - rowCountMean);
delta = time - executionTimeMean;
executionTimeMean += delta / count;
executionTimeM2 += delta * (time - executionTimeMean);
executionTimeCumulative += time;
rowCountCumulative += rows;
lastUpdateTime = System.currentTimeMillis();
}
public double getExecutionTimeStandardDeviation() {
// population standard deviation
return Math.sqrt(executionTimeM2 / count);
}
public double getRowCountStandardDeviation() {
// population standard deviation
return Math.sqrt(rowCountM2 / count);
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论