Skip to content

Commit 65a71c8

Browse files
committed
Version 1.6
1 parent 19280b7 commit 65a71c8

File tree

6 files changed

+156
-99
lines changed

6 files changed

+156
-99
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ for (Object aisObj in aisList){
5555
```groovy
5656
ProgressLogger.measure({log.info(it)}, { /* long work * / }, 'Doing cool work')
5757
```
58+
Or:
59+
```groovy
60+
def result = ProgressLogger.measureAndLogTime({spent-> log.debug('Operation took: ' + spent) }){
61+
println 'test' // Some long measured work
62+
return 42
63+
}
64+
assert 42 == result
65+
```
5866
6. When amount of elements or iterations is not known (f.e. stream processing or recursive calls like tree traversal)
5967
totalAmountOfElements set to -1 and simpler statistic returned:
6068
```groovy
@@ -163,6 +171,9 @@ Then you may just do in script *without any initialization*:
163171
Please note, file must include config{} closure on top level which will be stripped automatically.
164172

165173
# Changelog
174+
## version 1.6 2018-04-26 16:14
175+
* Add method `ProgressLogger.measureAndLogTime`
176+
166177
## version 1.5 2018-04-19 19:35
167178
* Add class `GlobalConfig` for easy access configuration from resources.
168179
* Update gradlew version to 4.7.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ apply plugin: 'idea'
55
apply plugin: 'signing'
66

77
group = 'info.hubbitus'
8-
version = '1.5'
8+
version = '1.6'
99

1010
repositories {
1111
mavenCentral()

src/main/groovy/info/hubbitus/utils/bench/ProgressLogger.groovy

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,34 @@ class ProgressLogger {
184184
*/
185185
static Spent measure(Consumer outMethod, Supplier execute, String objName = '', String beginMessage = null, String additionalEndMessage = ''){
186186
if (beginMessage) outMethod.accept(beginMessage)
187-
ProgressLogger pl = new ProgressLogger(1, outMethod, -1, '')
188-
if (objName) pl.objName = objName;
189-
def execRes = execute.get();
190-
Spent spent = pl.stop(additionalEndMessage ?: execRes.toString());
191-
spent.info = (additionalEndMessage ?: execRes);
192-
return spent;
187+
ProgressLogger pl = new ProgressLogger(1, outMethod, -1, (objName?:''))
188+
def execRes = execute.get()
189+
Spent spent = pl.stop(additionalEndMessage ?: execRes.toString())
190+
spent.info = (additionalEndMessage ?: execRes)
191+
return spent
192+
}
193+
194+
/**
195+
* Method to measure single invocation, log spent time and return process result of actual work
196+
* In most cases we want just execute single statement and log spent time result. It is easy with that method like:
197+
* def result = ProgressLogger.measureAndLogTime({spentTime-> bufferWrite('Operation took: ' + spentTime) } ){
198+
* println 'test' // Some long measured work
199+
* return 42
200+
* }
201+
* assert 42 == result
202+
*
203+
* @param spentTimeLogger
204+
* @param execute
205+
* @return
206+
*/
207+
static def measureAndLogTime(Consumer<Spent> spentTimeLogger, Supplier execute){
208+
ProgressLogger pl = new ProgressLogger(1, {}, -1, '')
209+
def execRes = execute.get()
210+
Spent spent = pl.stop()
211+
spent.info = execRes
212+
spentTimeLogger.accept(spent)
213+
214+
return execRes
193215
}
194216

195217
/**
@@ -198,9 +220,9 @@ class ProgressLogger {
198220
* @param currentElementNo
199221
*/
200222
private void logProgress(long currentElementNo, String iterationName = null){
201-
lastPackSpentNs = System.nanoTime() - last;
202-
spentFromStartNs = System.nanoTime() - start;
203-
leaved = this.totalAmountOfElements - currentElementNo;
223+
lastPackSpentNs = System.nanoTime() - last
224+
spentFromStartNs = System.nanoTime() - start
225+
leaved = this.totalAmountOfElements - currentElementNo
204226

205227
if ( ! (currentElementNo % packLogSize) || currentElementNo == this.totalAmountOfElements || 1 == currentElementNo){ // First, last and by pack of 'packLogSize' amount of elements
206228
outMethod.accept(
@@ -217,8 +239,8 @@ class ProgressLogger {
217239
,_FORMAT: _FORMAT // For Estimation online add
218240
).toString() // .toString for compatibility with Java methods, passed by ref
219241
);
220-
last = System.nanoTime();
221-
autoAdjust();
242+
last = System.nanoTime()
243+
autoAdjust()
222244
}
223245
}
224246

@@ -301,28 +323,27 @@ class ProgressLogger {
301323
* @param iterationName
302324
*/
303325
void next(String iterationName){
304-
logProgress(current.incrementAndGet(), iterationName);
326+
logProgress(current.incrementAndGet(), iterationName)
305327
}
306328

307329
/**
308330
* Adjust elements batch size automatically to do not SPAM log
309331
*/
310-
synchronized void autoAdjust(){
332+
synchronized private void autoAdjust(){
311333
if (autoAdjust && current.get() > 2 && lastPackSpentNs / (10 ** 9) < 1){
312-
this.autoAdjustFactor = 10;
313-
autoAdjustEach();
334+
this.autoAdjustFactor = 10
335+
autoAdjustEach()
314336
}
315337
}
316338

317339
synchronized private void autoAdjustEach(){
318340
this.packLogSize = this.totalAmountOfElements / autoAdjustFactor;
319341
if (packLogSize in [0, 1]){
320-
packLogSize = 1;
321-
return;
342+
packLogSize = 1
343+
return
322344
}
323-
int roundFactor = 10 ** (Math.ceil(Math.log10(packLogSize)) - 1);
324-
// println "Adjusting: ${autoAdjust}; autoAdjustFactor=${autoAdjustFactor}; current=${current}; lastPackSpentNs(sec)=${lastPackSpentNs / (10 ** 9)}; packLogSize=${packLogSize}; roundFactor=${roundFactor}";
345+
int roundFactor = 10 ** (Math.ceil(Math.log10(packLogSize)) - 1)
325346
// Round up to 10 ** N - 1. F.e. 15 will be rounded to 20, 234 to 300, 5678 to 5000
326-
this.packLogSize = ( Math.ceil(packLogSize / roundFactor) * roundFactor ?: 1 );
347+
this.packLogSize = ( Math.ceil(packLogSize / roundFactor) * roundFactor ?: 1 )
327348
}
328349
}
Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package info.hubbitus.utils.bench
22

33
import groovy.transform.CompileStatic
4+
import groovy.transform.ToString
45

56
import java.math.RoundingMode
67

@@ -20,24 +21,24 @@ import java.math.RoundingMode
2021
*/
2122
@CompileStatic
2223
public class Spent implements Comparable<Spent>{
23-
long spent; // Nanoseconds
24-
def info;
24+
long spent // Nanoseconds
25+
def info
2526

2627
@Lazy private String spentStr = formatTimeElapsedSinceNanosecond(spent);
2728

2829
/**
2930
* Do not format anything if it less than 1 minute by default
3031
* Nanoseconds.
3132
*/
32-
static long MINIMUM_SPENT_FOR_FORMATTING = 60000;
33+
static long MINIMUM_SPENT_FOR_FORMATTING = 60000
3334

3435
/**
3536
* Constructor from amount of nanoseconds
3637
*
3738
* @param spent
3839
*/
3940
public Spent(long spent){
40-
this.spent = spent;
41+
this.spent = spent
4142
}
4243

4344
/**
@@ -51,31 +52,31 @@ public class Spent implements Comparable<Spent>{
5152

5253
if(nanosDiff / (10 ** 6) <= MINIMUM_SPENT_FOR_FORMATTING){ return sprintf('%.3f', (nanosDiff / (10 ** 9)).setScale(3, RoundingMode.HALF_UP)) }
5354

54-
String formattedTime = '';
55-
long secondInNanos = (long)(10 ** 9); // Explicit casting only for @CompileStatic
56-
long minuteInNanos = secondInNanos * 60;
57-
long hourInNanos = minuteInNanos * 60;
58-
long dayInNanos = hourInNanos * 24;
59-
long weekInNanos = dayInNanos * 7;
60-
long monthInNanos = dayInNanos * 30;
55+
String formattedTime = ''
56+
long secondInNanos = (long)(10 ** 9) // Explicit casting only for @CompileStatic
57+
long minuteInNanos = secondInNanos * 60
58+
long hourInNanos = minuteInNanos * 60
59+
long dayInNanos = hourInNanos * 24
60+
long weekInNanos = dayInNanos * 7
61+
long monthInNanos = dayInNanos * 30
6162

62-
List timeElapsed = [];
63+
List timeElapsed = []
6364
// Define time units - plural cases are handled inside loop
6465
List timeElapsedText = [
6566
'secs', 'mins', 'hours', 'days', 'weeks', 'months' // @TODO add formatting and/or internationalization support
66-
];
67+
]
6768

68-
timeElapsed[5] = (int) (nanosDiff / monthInNanos); // months
69+
timeElapsed[5] = (int) (nanosDiff / monthInNanos) // months
6970
nanosDiff %= monthInNanos;
70-
timeElapsed[4] = (int) (nanosDiff / weekInNanos); // weeks
71-
nanosDiff %= weekInNanos;
72-
timeElapsed[3] = (int) (nanosDiff / dayInNanos); // days
73-
nanosDiff %= dayInNanos;
74-
timeElapsed[2] = (int) (nanosDiff / hourInNanos); // hours
75-
nanosDiff %= hourInNanos;
76-
timeElapsed[1] = (int) (nanosDiff / minuteInNanos); // minutes
77-
nanosDiff %= minuteInNanos;
78-
timeElapsed[0] = sprintf('%.3f', (nanosDiff / secondInNanos).setScale(3, RoundingMode.HALF_UP)); // seconds
71+
timeElapsed[4] = (int) (nanosDiff / weekInNanos) // weeks
72+
nanosDiff %= weekInNanos
73+
timeElapsed[3] = (int) (nanosDiff / dayInNanos) // days
74+
nanosDiff %= dayInNanos
75+
timeElapsed[2] = (int) (nanosDiff / hourInNanos) // hours
76+
nanosDiff %= hourInNanos
77+
timeElapsed[1] = (int) (nanosDiff / minuteInNanos) // minutes
78+
nanosDiff %= minuteInNanos
79+
timeElapsed[0] = sprintf('%.3f', (nanosDiff / secondInNanos).setScale(3, RoundingMode.HALF_UP)) // seconds
7980

8081
// Only adds 3 significant high valued units
8182
int i = (timeElapsed.size()-1);
@@ -84,25 +85,25 @@ public class Spent implements Comparable<Spent>{
8485
if(timeElapsed[i] != 0){
8586
formattedTime += ((j>0)? ", " :"") \
8687
+ timeElapsed[i] \
87-
+ " " + timeElapsedText[i];
88-
++j;
88+
+ " " + timeElapsedText[i]
89+
++j
8990
}
9091
} // end for - build string
9192

92-
return formattedTime;
93+
return formattedTime
9394
}
9495

9596
public Spent plus(Spent other){
96-
this.spent += other.spent;
97-
return this;
97+
this.spent += other.spent
98+
return this
9899
}
99100

100101
public String toString(){
101-
return spentStr + (info ? "\n$info" : '');
102+
return spentStr + (info ? "\n$info" : '')
102103
}
103104

104105
@Override
105106
int compareTo(Spent other) {
106-
(other.spent <=> this.spent);
107+
(other.spent <=> this.spent)
107108
}
108109
}

src/test/groovy/info/hubbitus/utils/bench/ProgressLoggerJavaTest.java

Lines changed: 60 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,69 @@
1616
*/
1717
@Slf4j
1818
public class ProgressLoggerJavaTest extends Assert {
19-
private final StringBuffer sb = new StringBuffer();
20-
private Consumer bufferWrite = (it) -> sb.append(it).append('\n');
19+
private final StringBuffer sb = new StringBuffer();
20+
private Consumer bufferWrite = (it) -> sb.append(it).append('\n');
2121

22-
@Test
23-
public void testWithImplicitCreateConsumer(){
24-
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
25-
ProgressLogger pl = new ProgressLogger(
26-
list
27-
,new Consumer() {
28-
@Override
29-
public void accept(Object it) {
30-
System.out.println(it);
31-
bufferWrite.accept(it);
32-
}
33-
}
34-
);
35-
list.forEach( (t) -> pl.next() );
22+
@Test
23+
public void testWithImplicitCreateConsumer(){
24+
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
25+
ProgressLogger pl = new ProgressLogger(
26+
list
27+
,new Consumer() {
28+
@Override
29+
public void accept(Object it) {
30+
System.out.println(it);
31+
bufferWrite.accept(it);
32+
}
33+
}
34+
);
35+
list.forEach( (t) -> pl.next() );
3636

37-
assertThat(sb.toString(), matchesPattern("(?ms)^Process \\[Integer\\] #1 from 10 \\(10[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n" +
38-
"Process \\[Integer\\] #2 from 10 \\(20[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 8, time: \\d+[,.]\\d{3}\n" +
39-
"Process \\[Integer\\] #3 from 10 \\(30[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 7, time: \\d+[,.]\\d{3}\n" +
40-
"Process \\[Integer\\] #4 from 10 \\(40[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 6, time: \\d+[,.]\\d{3}\n" +
41-
"Process \\[Integer\\] #5 from 10 \\(50[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 5, time: \\d+[,.]\\d{3}\n" +
42-
"Process \\[Integer\\] #6 from 10 \\(60[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 4, time: \\d+[,.]\\d{3}\n" +
43-
"Process \\[Integer\\] #7 from 10 \\(70[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 3, time: \\d+[,.]\\d{3}\n" +
44-
"Process \\[Integer\\] #8 from 10 \\(80[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 2, time: \\d+[,.]\\d{3}\n" +
45-
"Process \\[Integer\\] #9 from 10 \\(90[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 1, time: \\d+[,.]\\d{3}\n" +
46-
"Process \\[Integer\\] #10 from 10 \\(100[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n")
47-
);
48-
}
37+
assertThat(sb.toString(), matchesPattern("(?ms)^Process \\[Integer\\] #1 from 10 \\(10[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n" +
38+
"Process \\[Integer\\] #2 from 10 \\(20[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 8, time: \\d+[,.]\\d{3}\n" +
39+
"Process \\[Integer\\] #3 from 10 \\(30[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 7, time: \\d+[,.]\\d{3}\n" +
40+
"Process \\[Integer\\] #4 from 10 \\(40[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 6, time: \\d+[,.]\\d{3}\n" +
41+
"Process \\[Integer\\] #5 from 10 \\(50[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 5, time: \\d+[,.]\\d{3}\n" +
42+
"Process \\[Integer\\] #6 from 10 \\(60[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 4, time: \\d+[,.]\\d{3}\n" +
43+
"Process \\[Integer\\] #7 from 10 \\(70[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 3, time: \\d+[,.]\\d{3}\n" +
44+
"Process \\[Integer\\] #8 from 10 \\(80[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 2, time: \\d+[,.]\\d{3}\n" +
45+
"Process \\[Integer\\] #9 from 10 \\(90[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 1, time: \\d+[,.]\\d{3}\n" +
46+
"Process \\[Integer\\] #10 from 10 \\(100[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n")
47+
);
48+
}
4949

50-
@Test
51-
public void testWithLambda(){
52-
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
53-
ProgressLogger pl = new ProgressLogger(
54-
list
55-
, it -> bufferWrite.accept(it)
56-
);
57-
list.forEach( (t) -> pl.next() );
50+
@Test
51+
public void testWithLambda(){
52+
List list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
53+
ProgressLogger pl = new ProgressLogger(
54+
list
55+
, it -> bufferWrite.accept(it)
56+
);
57+
list.forEach( (t) -> pl.next() );
5858

59-
assertThat(sb.toString(), matchesPattern("(?ms)^Process \\[Integer\\] #1 from 10 \\(10[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n" +
60-
"Process \\[Integer\\] #2 from 10 \\(20[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 8, time: \\d+[,.]\\d{3}\n" +
61-
"Process \\[Integer\\] #3 from 10 \\(30[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 7, time: \\d+[,.]\\d{3}\n" +
62-
"Process \\[Integer\\] #4 from 10 \\(40[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 6, time: \\d+[,.]\\d{3}\n" +
63-
"Process \\[Integer\\] #5 from 10 \\(50[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 5, time: \\d+[,.]\\d{3}\n" +
64-
"Process \\[Integer\\] #6 from 10 \\(60[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 4, time: \\d+[,.]\\d{3}\n" +
65-
"Process \\[Integer\\] #7 from 10 \\(70[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 3, time: \\d+[,.]\\d{3}\n" +
66-
"Process \\[Integer\\] #8 from 10 \\(80[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 2, time: \\d+[,.]\\d{3}\n" +
67-
"Process \\[Integer\\] #9 from 10 \\(90[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 1, time: \\d+[,.]\\d{3}\n" +
68-
"Process \\[Integer\\] #10 from 10 \\(100[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n")
69-
);
70-
}
59+
assertThat(sb.toString(), matchesPattern("(?ms)^Process \\[Integer\\] #1 from 10 \\(10[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n" +
60+
"Process \\[Integer\\] #2 from 10 \\(20[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 8, time: \\d+[,.]\\d{3}\n" +
61+
"Process \\[Integer\\] #3 from 10 \\(30[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 7, time: \\d+[,.]\\d{3}\n" +
62+
"Process \\[Integer\\] #4 from 10 \\(40[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 6, time: \\d+[,.]\\d{3}\n" +
63+
"Process \\[Integer\\] #5 from 10 \\(50[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 5, time: \\d+[,.]\\d{3}\n" +
64+
"Process \\[Integer\\] #6 from 10 \\(60[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 4, time: \\d+[,.]\\d{3}\n" +
65+
"Process \\[Integer\\] #7 from 10 \\(70[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 3, time: \\d+[,.]\\d{3}\n" +
66+
"Process \\[Integer\\] #8 from 10 \\(80[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 2, time: \\d+[,.]\\d{3}\n" +
67+
"Process \\[Integer\\] #9 from 10 \\(90[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\\. Estimated items: 1, time: \\d+[,.]\\d{3}\n" +
68+
"Process \\[Integer\\] #10 from 10 \\(100[,.]00%\\)\\. Spent \\(pack by 1\\) time: \\d+[,.]\\d{3} \\(from start: \\d+[,.]\\d{3}\\)\n")
69+
);
70+
}
7171

72+
@Test
73+
public void measureAndLogTimeTest(){
74+
long result = (long)ProgressLogger.measureAndLogTime(
75+
spentTime-> bufferWrite.accept("Operation took: " + spentTime),
76+
() -> {
77+
System.out.println ("test"); // Some long measured work
78+
return 42L;
79+
}
80+
);
81+
assertEquals(42, result);
82+
assertThat(sb.toString(), matchesPattern("Operation took: 0[,.]\\d{3}\n42\n"));
83+
}
7284
}

src/test/groovy/info/hubbitus/utils/bench/ProgressLoggerTest.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package info.hubbitus.utils.bench
33
import groovyx.gpars.GParsPool
44
import spock.lang.Specification
55

6+
import java.util.function.Consumer
67
import java.util.function.Supplier
78

89
/**
@@ -223,4 +224,15 @@ Process \[four\] #4 from 4 \(100[,.]00%\)\. Spent \(pack by 1\) time: \d+[,.]\d{
223224
}
224225
}
225226
}
227+
228+
def measureAndLogTimeTest(){
229+
when:
230+
def result = ProgressLogger.measureAndLogTime({spent-> bufferWrite('Operation took: ' + spent) }){
231+
println 'test' // Some long measured work
232+
return 42
233+
}
234+
then:
235+
42 == result
236+
sb ==~ /Operation took: 0[,.]\d{3}\n42\n/
237+
}
226238
}

0 commit comments

Comments
 (0)