|
| 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.ArrayList; |
| 22 | +import java.util.Comparator; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import org.junit.jupiter.api.extension.AfterAllCallback; |
| 27 | +import org.junit.jupiter.api.extension.AfterEachCallback; |
| 28 | +import org.junit.jupiter.api.extension.BeforeEachCallback; |
| 29 | +import org.junit.jupiter.api.extension.ExtensionContext; |
| 30 | + |
| 31 | +public class TestDurationMeasureExtension implements BeforeEachCallback, AfterEachCallback, AfterAllCallback { |
| 32 | + record TestDuration(String test, Duration duration) { |
| 33 | + } |
| 34 | + |
| 35 | + private final Map<String, Instant> startTimeMap = new HashMap<>(); |
| 36 | + private final List<TestDuration> durationList = new ArrayList<>(); |
| 37 | + |
| 38 | + private void start(String uuid) { |
| 39 | + startTimeMap.put(uuid, Instant.now()); |
| 40 | + } |
| 41 | + |
| 42 | + private void stop(String uuid, String name) { |
| 43 | + Instant start = startTimeMap.get(uuid); |
| 44 | + if (start != null) { |
| 45 | + Duration duration = Duration.between(start, Instant.now()); |
| 46 | + durationList.add(new TestDuration(name, duration)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void beforeEach(ExtensionContext context) { |
| 52 | + start(context.getUniqueId()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void afterEach(ExtensionContext context) { |
| 57 | + stop(context.getUniqueId(), context.getDisplayName()); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void afterAll(ExtensionContext context) { |
| 62 | + System.out.println("Test durations:"); |
| 63 | + durationList.sort(Comparator.comparing(TestDuration::duration).reversed()); |
| 64 | + for (TestDuration duration : durationList) { |
| 65 | + System.out.println(duration.test() + ": " + duration.duration().toSeconds() + " sec"); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments