|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2012-2024 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.it; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.Comparator; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Map; |
| 25 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 26 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 27 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 28 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +public class TestDurationMeasureExtension implements BeforeEachCallback, AfterEachCallback, AfterAllCallback { |
| 33 | + private static final Logger LOGGER = LoggerFactory.getLogger(TestDurationMeasureExtension.class); |
| 34 | + |
| 35 | + private record TestEntry(String test, Duration duration, Instant start) { |
| 36 | + public TestEntry stoppedEntry() { |
| 37 | + if(duration.isZero()) { |
| 38 | + Duration newDuration = Duration.between(start, Instant.now()); |
| 39 | + return new TestEntry(test, newDuration, start); |
| 40 | + } else { |
| 41 | + return this; |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private final Map<String, TestEntry> testEntryMap = new HashMap<>(); |
| 47 | + |
| 48 | + private synchronized void start(String uuid, String name) { |
| 49 | + testEntryMap.put(uuid, new TestEntry(name, Duration.ZERO, Instant.now())); |
| 50 | + } |
| 51 | + |
| 52 | + private synchronized void stop(String uuid) { |
| 53 | + testEntryMap.computeIfPresent(uuid, (key, entry) -> entry.stoppedEntry()); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void beforeEach(ExtensionContext context) { |
| 58 | + start(context.getUniqueId(), context.getDisplayName()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void afterEach(ExtensionContext context) { |
| 63 | + stop(context.getUniqueId()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public synchronized void afterAll(ExtensionContext context) { |
| 68 | + List<TestEntry> testEntries = testEntryMap.values().stream() |
| 69 | + .map(TestEntry::stoppedEntry) |
| 70 | + .sorted(Comparator.comparing(TestEntry::duration).reversed()) |
| 71 | + .toList(); |
| 72 | + |
| 73 | + LOGGER.info("Test durations:"); |
| 74 | + for (TestEntry entry : testEntries) { |
| 75 | + LOGGER.info("{}: {} sec", entry.test(), entry.duration().toSeconds()); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments