Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Evolveum/midpoint
- Loading branch information
Showing
20 changed files
with
841 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
infra/util/src/main/java/com/evolveum/midpoint/util/histogram/Histogram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Copyright (c) 2010-2017 Evolveum | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.evolveum.midpoint.util.histogram; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* @author mederly | ||
*/ | ||
public class Histogram<T> { | ||
|
||
private final int step; | ||
private final int maxLength; | ||
|
||
public Histogram(int step, int maxLength) { | ||
this.step = step; | ||
this.maxLength = maxLength; | ||
} | ||
|
||
private final ArrayList<HistogramEntry<T>> entries = new ArrayList<>(); | ||
|
||
private long minValue, maxValue, totalValue = 0; | ||
private T minItem, maxItem; | ||
private int items = 0; | ||
|
||
public void register(T item, long value) { | ||
if (items == 0) { | ||
minValue = maxValue = value; | ||
minItem = maxItem = item; | ||
} else { | ||
if (value < minValue) { | ||
minValue = value; | ||
minItem = item; | ||
} | ||
if (value > maxValue) { | ||
maxValue = value; | ||
maxItem = item; | ||
} | ||
} | ||
totalValue += value; | ||
items++; | ||
|
||
long bucketLong = value / step; | ||
int bucket = bucketLong < maxLength-1 ? (int) bucketLong : maxLength-1; | ||
if (entries.size() <= bucket) { | ||
entries.ensureCapacity(bucket); | ||
while (entries.size() <= bucket) { | ||
entries.add(new HistogramEntry<>()); | ||
} | ||
} | ||
entries.get(bucket).record(item, value); | ||
} | ||
|
||
public int getStep() { | ||
return step; | ||
} | ||
|
||
public int getMaxLength() { | ||
return maxLength; | ||
} | ||
|
||
public ArrayList<HistogramEntry<T>> getEntries() { | ||
return entries; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public long getMinValue() { | ||
return minValue; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public long getMaxValue() { | ||
return maxValue; | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public long getTotalValue() { | ||
return totalValue; | ||
} | ||
|
||
public int getItems() { | ||
return items; | ||
} | ||
|
||
public String dump(int columns) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Count: ").append(items).append("\n"); | ||
if (items == 0) { | ||
return sb.toString(); | ||
} | ||
sb.append("Min: ").append(minValue).append(" (").append(minItem).append(")\n"); | ||
sb.append("Max: ").append(maxValue).append(" (").append(maxItem).append(")\n"); | ||
sb.append("Avg: ").append(totalValue / items).append("\n"); | ||
sb.append("\nHistogram:\n\n"); | ||
int maxIntervalLength = Math.max(getIntervalString(entries.size()).length(), 10); | ||
String rowFormatString = "%" + maxIntervalLength + "s : %6s : %s : %s\n"; | ||
//noinspection ConstantConditions | ||
int maxCount = entries.stream().mapToInt(e -> e.getItemsCount()).max().getAsInt(); | ||
sb.append(String.format(rowFormatString, "Interval", "Items", column(0, maxCount, columns), "Representative")); | ||
sb.append(StringUtils.repeat("-", maxIntervalLength + columns + 40)).append("\n"); | ||
for (int i = 0; i < entries.size(); i++) { | ||
HistogramEntry<T> entry = entries.get(i); | ||
sb.append(String.format(rowFormatString, getIntervalString(i), | ||
String.valueOf(entry.getItemsCount()), column(entry.getItemsCount(), maxCount, columns), getRepresentative(entry))); | ||
} | ||
sb.append("\n"); | ||
return sb.toString(); | ||
} | ||
|
||
private Object column(int count, int maxCount, int columns) { | ||
int bars = (int) ((double) columns * (double) count / (double) maxCount); | ||
return StringUtils.repeat("#", bars) + StringUtils.repeat(" ", columns-bars); | ||
} | ||
|
||
private String getRepresentative(HistogramEntry<T> entry) { | ||
if (entry.getRepresentativeItem() == null) { | ||
return ""; | ||
} | ||
return entry.getRepresentativeItem() + " (" + entry.getRepresentativeItemValue() + ")"; | ||
} | ||
|
||
private String getIntervalString(int i) { | ||
if (i == entries.size()-1) { | ||
return String.format("[%d-%d]", getLower(i), maxValue); | ||
} else { | ||
return String.format("[%d-%d]", getLower(i), getUpper(i)); | ||
} | ||
} | ||
|
||
private long getLower(int bucket) { | ||
return bucket * step; | ||
} | ||
|
||
private long getUpper(int bucket) { | ||
return getLower(bucket+1)-1; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
infra/util/src/main/java/com/evolveum/midpoint/util/histogram/HistogramEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2010-2017 Evolveum | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.evolveum.midpoint.util.histogram; | ||
|
||
/** | ||
* @author mederly | ||
*/ | ||
public class HistogramEntry<T> { | ||
|
||
private int itemsCount; | ||
private long representativeItemValue; | ||
private T representativeItem; | ||
|
||
public int getItemsCount() { | ||
return itemsCount; | ||
} | ||
|
||
public T getRepresentativeItem() { | ||
return representativeItem; | ||
} | ||
|
||
public long getRepresentativeItemValue() { | ||
return representativeItemValue; | ||
} | ||
|
||
public void record(T item, long value) { | ||
if (representativeItem == null || representativeItemValue < value) { | ||
representativeItem = item; | ||
representativeItemValue = value; | ||
} | ||
itemsCount++; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.