protected static void initEachWastedLost()

in hugegraph-common/src/main/java/org/apache/hugegraph/perf/NormalStopwatch.java [248:301]


    protected static void initEachWastedLost() {
        int times = 100000000;

        PerfUtil.LocalStack<Stopwatch> callStack = Whitebox.getInternalState(
                                          PerfUtil.instance(), "callStack");

        long baseStart = PerfUtil.now();
        for (int i = 0; i < times; i++) {
            PerfUtil.instance();
        }
        long baseCost = PerfUtil.now() - baseStart;

        BiFunction<String, Runnable, Long> testEachCost = (name, test) -> {
            long start = PerfUtil.now();
            test.run();
            long end = PerfUtil.now();
            long cost = end - start - baseCost;
            if (cost < 0L) {
                cost = 0L;
            }
            long eachCost = cost / times;

            LOG.info("Wasted time test: cost={}ms, base_cost={}ms, {}={}ns",
                     cost / 1000000.0, baseCost / 1000000.0, name, eachCost);
            return eachCost;
        };

        String startName = "each_start_cost";
        eachStartWastedLost = testEachCost.apply(startName, () -> {
            Stopwatch watch = PerfUtil.instance().start(startName);
            PerfUtil.instance().end(startName);
            for (int i = 0; i < times; i++) {
                // Test call start()
                PerfUtil.instance().start(startName);
                // Mock end()
                watch.lastStartTime(-1L);
                callStack.pop();
            }
        });

        String endName = "each_end_cost";
        eachEndWastedLost = testEachCost.apply(endName, () -> {
            Stopwatch watch = PerfUtil.instance().start(endName);
            PerfUtil.instance().end(endName);
            for (int i = 0; i < times; i++) {
                // Mock start()
                callStack.push(watch);
                watch.lastStartTime(0L);
                // Test call start()
                PerfUtil.instance().end(endName);
                watch.totalCost(0L);
            }
        });
    }