• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 220

22 Nov 2023 12:57PM UTC coverage: 62.564% (-0.04%) from 62.602%
220

Pull #1081

circleci

Frotty
Update MapRequest.java
Pull Request #1081: More performance improvements

17307 of 27663 relevant lines covered (62.56%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

14.55
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/TimeTaker.java
1
package de.peeeq.wurstio;
2

3
import de.peeeq.wurstscript.utils.Utils;
4

5
import java.lang.management.GarbageCollectorMXBean;
6
import java.lang.management.ManagementFactory;
7
import java.util.LinkedHashMap;
8
import java.util.Map;
9
import java.util.function.Supplier;
10

11
/**
12
 *
13
 */
14
public interface TimeTaker {
15
    default void measure(String name, Runnable f) {
16
        measure(name, () -> {
1✔
17
            f.run();
1✔
18
            return null;
1✔
19
        });
20
    }
1✔
21

22
    <T> T measure(String name, Supplier<T> f);
23

24
    void beginPhase(String description);
25

26
    void endPhase();
27

28
    void printReport();
29

30

31
    class Default implements TimeTaker {
1✔
32

33
        @Override
34
        public <T> T measure(String name, Supplier<T> f) {
35
            return f.get();
1✔
36
        }
37

38
        @Override
39
        public void beginPhase(String description) {
40
        }
1✔
41

42
        @Override
43
        public void endPhase() {
44
        }
1✔
45

46
        @Override
47
        public void printReport() {
48

49
        }
×
50
    }
51

52
    class Recording implements TimeTaker {
×
53
        private int nesting = 0;
×
54
        private String currentPhaseDescription;
55
        private long currentPhaseStart;
56
        private Map<String, Long> accumulatedTimes = new LinkedHashMap<>();
×
57

58
        private Long startTime = 0L;
×
59

60
        public <T> T measure(String name, Supplier<T> f) {
61
            name = withNesting(name);
×
62
            nesting++;
×
63
            accumulatedTimes.putIfAbsent(name, 0L);
×
64
            long time = System.currentTimeMillis();
×
65
            T result = f.get();
×
66
            long duration = System.currentTimeMillis() - time;
×
67
            reportDuration(name, duration);
×
68
            nesting--;
×
69
            return result;
×
70
        }
71

72
        private String withNesting(String name) {
73
            return Utils.repeat(' ', nesting) + name;
×
74
        }
75

76
        private void reportDuration(String name, long duration) {
77
            accumulatedTimes.put(name, accumulatedTimes.getOrDefault(name, 0L) + duration);
×
78
        }
×
79

80

81
        @Override
82
        public void beginPhase(String description) {
83
            if (accumulatedTimes.isEmpty()) {
×
84
                this.startTime = System.currentTimeMillis();
×
85
            }
86
            if (currentPhaseDescription != null) {
×
87
                endPhase();
×
88
            }
89
            description = withNesting(description);
×
90
            nesting++;
×
91
            accumulatedTimes.putIfAbsent(description, 0L);
×
92
            currentPhaseDescription = description;
×
93
            currentPhaseStart = System.currentTimeMillis();
×
94
        }
×
95

96
        @Override
97
        public void endPhase() {
98
            if (currentPhaseDescription == null) {
×
99
                return;
×
100
            }
101
            long duration = System.currentTimeMillis() - currentPhaseStart;
×
102
            reportDuration(currentPhaseDescription, duration);
×
103
            nesting--;
×
104
            currentPhaseDescription = null;
×
105
        }
×
106

107
        @Override
108
        public void printReport() {
109
            System.out.println("#############################");
×
110
            System.out.println("Run times:");
×
111

112
            for (Map.Entry<String, Long> e : accumulatedTimes.entrySet()) {
×
113
                System.out.println(e.getKey() + ": " + e.getValue() + "ms");
×
114
            }
×
115

116
            System.out.println("Total runtime: " + (System.currentTimeMillis() - startTime) + "ms");
×
117
            System.out.println("GC time: " + getGarbageCollectionTime() + "ms");
×
118
        }
×
119

120
        private static long getGarbageCollectionTime() {
121
            long collectionTime = 0;
×
122
            for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
×
123
                collectionTime += garbageCollectorMXBean.getCollectionTime();
×
124
            }
×
125
            return collectionTime;
×
126
        }
127

128
    }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc