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

wurstscript / WurstScript / 209

25 Oct 2023 11:42AM UTC coverage: 63.746% (-0.01%) from 63.756%
209

Pull #1081

circleci

Frotty
WIP
Pull Request #1081: More performance improvements

17267 of 27087 relevant lines covered (63.75%)

0.64 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 totalTime = 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
            if (nesting == 1) {
×
69
                totalTime += duration;
×
70
            }
71
            nesting--;
×
72
            return result;
×
73
        }
74

75
        private String withNesting(String name) {
76
            return Utils.repeat(' ', nesting) + name;
×
77
        }
78

79
        private void reportDuration(String name, long duration) {
80
            accumulatedTimes.put(name, accumulatedTimes.getOrDefault(name, 0L) + duration);
×
81
        }
×
82

83

84
        @Override
85
        public void beginPhase(String description) {
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
            if (nesting == 1) {
×
104
                totalTime += duration;
×
105
            }
106
            nesting--;
×
107
            currentPhaseDescription = null;
×
108
        }
×
109

110
        @Override
111
        public void printReport() {
112
            System.out.println("#############################");
×
113
            System.out.println("Run times:");
×
114

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

119
            System.out.println("Total runtime: " + totalTime + "ms");
×
120
            System.out.println("GC time: " + getGarbageCollectionTime() + "ms");
×
121
        }
×
122

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

131
    }
132
}
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