Changeset 15755 in josm


Ignore:
Timestamp:
2020-01-23T22:57:46+01:00 (4 years ago)
Author:
simon04
Message:

Introduce Stopwatch class to measure elapsed time

Location:
trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/MergeLayerAction.java

    r15450 r15755  
    2424import org.openstreetmap.josm.tools.Logging;
    2525import org.openstreetmap.josm.tools.Shortcut;
     26import org.openstreetmap.josm.tools.Stopwatch;
    2627import org.openstreetmap.josm.tools.Utils;
    2728
     
    8182
    8283                return MainApplication.worker.submit(() -> {
    83                     final long start = System.currentTimeMillis();
     84                    final Stopwatch stopwatch = Stopwatch.createStarted();
    8485
    8586                    for (int i = sortedLayers.size() - 2; i >= 0; i--) {
     
    8990                    }
    9091
    91                     Logging.info(tr("{0} completed in {1}", actionName, Utils.getDurationString(System.currentTimeMillis() - start)));
     92                    Logging.info(tr("{0} completed in {1}", actionName, stopwatch));
    9293                });
    9394            }
     
    9596
    9697        return MainApplication.worker.submit(() -> {
    97             final long start = System.currentTimeMillis();
     98            final Stopwatch stopwatch = Stopwatch.createStarted();
    9899            boolean layerMerged = false;
    99100            for (final Layer sourceLayer: sourceLayers) {
     
    113114            if (layerMerged) {
    114115                getLayerManager().setActiveLayer(targetLayer);
    115                 Logging.info(tr("{0} completed in {1}", actionName, Utils.getDurationString(System.currentTimeMillis() - start)));
     116                Logging.info(tr("{0} completed in {1}", actionName, stopwatch));
    116117            }
    117118        });
  • trunk/src/org/openstreetmap/josm/data/osm/DatasetConsistencyTest.java

    r12620 r15755  
    1010import org.openstreetmap.josm.tools.JosmRuntimeException;
    1111import org.openstreetmap.josm.tools.Logging;
    12 import org.openstreetmap.josm.tools.Utils;
     12import org.openstreetmap.josm.tools.Stopwatch;
    1313
    1414/**
     
    4646     */
    4747    public void checkReferrers() {
    48         long startTime = System.currentTimeMillis();
     48        final Stopwatch stopwatch = Stopwatch.createStarted();
    4949        // It's also error when referred primitive's dataset is null but it's already covered by referredPrimitiveNotInDataset check
    5050        for (Way way : dataSet.getWays()) {
     
    6767            }
    6868        }
    69         printElapsedTime(startTime);
     69        printElapsedTime(stopwatch);
    7070    }
    7171
     
    7474     */
    7575    public void checkCompleteWaysWithIncompleteNodes() {
    76         long startTime = System.currentTimeMillis();
     76        final Stopwatch stopwatch = Stopwatch.createStarted();
    7777        for (Way way : dataSet.getWays()) {
    7878            if (way.isUsable()) {
     
    8484            }
    8585        }
    86         printElapsedTime(startTime);
     86        printElapsedTime(stopwatch);
    8787    }
    8888
     
    9191     */
    9292    public void checkCompleteNodesWithoutCoordinates() {
    93         long startTime = System.currentTimeMillis();
     93        final Stopwatch stopwatch = Stopwatch.createStarted();
    9494        for (Node node : dataSet.getNodes()) {
    9595            if (!node.isIncomplete() && node.isVisible() && !node.isLatLonKnown()) {
     
    9797            }
    9898        }
    99         printElapsedTime(startTime);
     99        printElapsedTime(stopwatch);
    100100    }
    101101
     
    104104     */
    105105    public void searchNodes() {
    106         long startTime = System.currentTimeMillis();
     106        final Stopwatch stopwatch = Stopwatch.createStarted();
    107107        dataSet.getReadLock().lock();
    108108        try {
     
    116116            dataSet.getReadLock().unlock();
    117117        }
    118         printElapsedTime(startTime);
     118        printElapsedTime(stopwatch);
    119119    }
    120120
     
    123123     */
    124124    public void searchWays() {
    125         long startTime = System.currentTimeMillis();
     125        final Stopwatch stopwatch = Stopwatch.createStarted();
    126126        dataSet.getReadLock().lock();
    127127        try {
     
    134134            dataSet.getReadLock().unlock();
    135135        }
    136         printElapsedTime(startTime);
     136        printElapsedTime(stopwatch);
    137137    }
    138138
     
    155155     */
    156156    public void referredPrimitiveNotInDataset() {
    157         long startTime = System.currentTimeMillis();
     157        final Stopwatch stopwatch = Stopwatch.createStarted();
    158158        for (Way way : dataSet.getWays()) {
    159159            for (Node node : way.getNodes()) {
     
    167167            }
    168168        }
    169         printElapsedTime(startTime);
     169        printElapsedTime(stopwatch);
    170170    }
    171171
     
    174174     */
    175175    public void checkZeroNodesWays() {
    176         long startTime = System.currentTimeMillis();
     176        final Stopwatch stopwatch = Stopwatch.createStarted();
    177177        for (Way way : dataSet.getWays()) {
    178178            if (way.isUsable() && way.getNodesCount() == 0) {
     
    182182            }
    183183        }
    184         printElapsedTime(startTime);
    185     }
    186 
    187     private void printElapsedTime(long startTime) {
     184        printElapsedTime(stopwatch);
     185    }
     186
     187    private void printElapsedTime(Stopwatch stopwatch) {
    188188        if (Logging.isDebugEnabled()) {
    189189            StackTraceElement item = Thread.currentThread().getStackTrace()[2];
    190190            String operation = getClass().getSimpleName() + '.' + item.getMethodName();
    191             long elapsedTime = System.currentTimeMillis() - startTime;
    192191            Logging.debug(tr("Test ''{0}'' completed in {1}",
    193                     operation, Utils.getDurationString(elapsedTime)));
     192                    operation, stopwatch));
    194193        }
    195194    }
     
    200199    public void runTest() {
    201200        try {
    202             long startTime = System.currentTimeMillis();
     201            final Stopwatch stopwatch = Stopwatch.createStarted();
    203202            referredPrimitiveNotInDataset();
    204203            checkReferrers();
     
    208207            searchWays();
    209208            checkZeroNodesWays();
    210             printElapsedTime(startTime);
     209            printElapsedTime(stopwatch);
    211210            if (errorCount > MAX_ERRORS) {
    212211                writer.println((errorCount - MAX_ERRORS) + " more...");
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r15704 r15755  
    7979import org.openstreetmap.josm.tools.AlphanumComparator;
    8080import org.openstreetmap.josm.tools.Logging;
    81 import org.openstreetmap.josm.tools.Utils;
     81import org.openstreetmap.josm.tools.Stopwatch;
    8282
    8383/**
     
    609609        if (!testsInitialized) {
    610610            Logging.debug("Initializing validator tests");
    611             final long startTime = System.currentTimeMillis();
     611            final Stopwatch stopwatch = Stopwatch.createStarted();
    612612            initializeTests(getTests());
    613613            testsInitialized = true;
    614             if (Logging.isDebugEnabled()) {
    615                 final long elapsedTime = System.currentTimeMillis() - startTime;
    616                 Logging.debug("Initializing validator tests completed in {0}", Utils.getDurationString(elapsedTime));
    617             }
     614            Logging.debug("Initializing validator tests completed in {0}", stopwatch);
    618615        }
    619616    }
  • trunk/src/org/openstreetmap/josm/data/validation/Test.java

    r15683 r15755  
    2828import org.openstreetmap.josm.tools.GBC;
    2929import org.openstreetmap.josm.tools.Logging;
    30 import org.openstreetmap.josm.tools.Utils;
     30import org.openstreetmap.josm.tools.Stopwatch;
    3131
    3232/**
     
    7575
    7676    /** the start time to compute elapsed time when test finishes */
    77     protected long startTime;
     77    protected Stopwatch stopwatch;
    7878
    7979    private boolean showElementCount;
     
    154154     */
    155155    public void initialize() throws Exception {
    156         this.startTime = -1;
     156        this.stopwatch = Stopwatch.createStarted();
    157157    }
    158158
     
    168168        Logging.debug(startMessage);
    169169        this.errors = new ArrayList<>(30);
    170         this.startTime = System.currentTimeMillis();
     170        this.stopwatch = Stopwatch.createStarted();
    171171    }
    172172
     
    197197        progressMonitor.finishTask();
    198198        progressMonitor = null;
    199         if (startTime > 0) {
    200             // fix #11567 where elapsedTime is < 0
    201             long elapsedTime = Math.max(0, System.currentTimeMillis() - startTime);
    202             Logging.debug(tr("Test ''{0}'' completed in {1}", getName(), Utils.getDurationString(elapsedTime)));
     199        if (stopwatch.elapsed() > 0) {
     200            Logging.debug(tr("Test ''{0}'' completed in {1}", getName(), stopwatch));
    203201        }
    204202    }
  • trunk/src/org/openstreetmap/josm/gui/SplashScreen.java

    r15719 r15755  
    4242import org.openstreetmap.josm.tools.ImageProvider;
    4343import org.openstreetmap.josm.tools.Logging;
     44import org.openstreetmap.josm.tools.Stopwatch;
    4445import org.openstreetmap.josm.tools.Utils;
    4546
     
    150151    private static class MeasurableTask extends Task {
    151152        private final String name;
    152         private final long start;
     153        private final Stopwatch stopwatch;
    153154        private String duration = "";
    154155
    155156        MeasurableTask(String name) {
    156157            this.name = name;
    157             this.start = System.currentTimeMillis();
     158            this.stopwatch = Stopwatch.createStarted();
    158159        }
    159160
     
    162163                throw new IllegalStateException("This task has already been finished: " + name);
    163164            }
    164             long time = System.currentTimeMillis() - start;
    165             if (time >= 0) {
    166                 duration = tr(" ({0})", Utils.getDurationString(time));
     165            if (stopwatch.elapsed() >= 0) {
     166                duration = tr(" ({0})", stopwatch);
    167167            }
    168168        }
  • trunk/src/org/openstreetmap/josm/gui/layer/gpx/GpxDrawHelper.java

    r15606 r15755  
    5858import org.openstreetmap.josm.tools.JosmRuntimeException;
    5959import org.openstreetmap.josm.tools.Logging;
     60import org.openstreetmap.josm.tools.Stopwatch;
    6061import org.openstreetmap.josm.tools.Utils;
    6162
     
    420421    public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, Bounds clipBounds) {
    421422
    422         final long timeStart = System.currentTimeMillis();
     423        final Stopwatch stopwatch = Stopwatch.createStarted();
    423424
    424425        checkCache();
     
    487488        // show some debug info
    488489        if (Logging.isDebugEnabled() && !visibleSegments.isEmpty()) {
    489             final long timeDiff = System.currentTimeMillis() - timeStart;
    490 
    491490            Logging.debug("gpxdraw::draw takes " +
    492                          Utils.getDurationString(timeDiff) +
     491                         stopwatch +
    493492                         "(" +
    494493                         "segments= " + visibleSegments.size() +
    495                          ", per 10000 = " + Utils.getDurationString(10_000 * timeDiff / visibleSegments.size()) +
     494                         ", per 10000 = " + Utils.getDurationString(10_000 * stopwatch.elapsed() / visibleSegments.size()) +
    496495                         ")"
    497496              );
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r15717 r15755  
    2929import org.openstreetmap.josm.tools.ListenerList;
    3030import org.openstreetmap.josm.tools.Logging;
     31import org.openstreetmap.josm.tools.Stopwatch;
    3132import org.openstreetmap.josm.tools.Utils;
    3233
     
    321322
    322323    private static void loadStyleForFirstTime(StyleSource source) {
    323         final long startTime = System.currentTimeMillis();
     324        final Stopwatch stopwatch = Stopwatch.createStarted();
    324325        source.loadStyleSource();
    325326        if (Config.getPref().getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) {
     
    331332        }
    332333        if (Logging.isDebugEnabled() || !source.isValid()) {
    333             final long elapsedTime = System.currentTimeMillis() - startTime;
    334             String message = "Initializing map style " + source.url + " completed in " + Utils.getDurationString(elapsedTime);
     334            String message = "Initializing map style " + source.url + " completed in " + stopwatch;
    335335            if (!source.isValid()) {
    336336                Logging.warn(message + " (" + source.getErrors().size() + " errors, " + source.getWarnings().size() + " warnings)");
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresetReader.java

    r15072 r15755  
    4747import org.openstreetmap.josm.tools.I18n;
    4848import org.openstreetmap.josm.tools.Logging;
     49import org.openstreetmap.josm.tools.Stopwatch;
    4950import org.openstreetmap.josm.tools.Utils;
    5051import org.openstreetmap.josm.tools.XmlObjectParser;
     
    353354        Collection<TaggingPreset> tp;
    354355        Logging.debug("Reading presets from {0}", source);
    355         long startTime = System.currentTimeMillis();
     356        Stopwatch stopwatch = Stopwatch.createStarted();
    356357        try (
    357358            CachedFile cf = new CachedFile(source).setHttpAccept(PRESET_MIME_TYPES);
     
    368369        }
    369370        if (Logging.isDebugEnabled()) {
    370             Logging.debug("Presets read in {0}", Utils.getDurationString(System.currentTimeMillis() - startTime));
     371            Logging.debug("Presets read in {0}", stopwatch);
    371372        }
    372373        return tp;
  • trunk/src/org/openstreetmap/josm/tools/XmlUtils.java

    r14441 r15755  
    8484     */
    8585    public static Document parseSafeDOM(InputStream is) throws ParserConfigurationException, IOException, SAXException {
    86         long start = System.currentTimeMillis();
     86        Stopwatch stopwatch = Stopwatch.createStarted();
    8787        Logging.debug("Starting DOM parsing of {0}", is);
    8888        Document result = newSafeDOMBuilder().parse(is);
    89         if (Logging.isDebugEnabled()) {
    90             Logging.debug("DOM parsing done in {0}", Utils.getDurationString(System.currentTimeMillis() - start));
    91         }
     89        Logging.debug("DOM parsing done in {0}", stopwatch);
    9290        return result;
    9391    }
     
    117115     */
    118116    public static void parseSafeSAX(InputSource is, DefaultHandler dh) throws ParserConfigurationException, SAXException, IOException {
    119         long start = System.currentTimeMillis();
     117        Stopwatch stopwatch = Stopwatch.createStarted();
    120118        Logging.debug("Starting SAX parsing of {0} using {1}", is, dh);
    121119        newSafeSAXParser().parse(is, dh);
    122         if (Logging.isDebugEnabled()) {
    123             Logging.debug("SAX parsing done in {0}", Utils.getDurationString(System.currentTimeMillis() - start));
    124         }
     120        Logging.debug("SAX parsing done in {0}", stopwatch);
    125121    }
    126122
  • trunk/test/unit/org/openstreetmap/josm/io/audio/AudioPlayerTest.java

    r12327 r15755  
    1414import org.openstreetmap.josm.JOSMFixture;
    1515import org.openstreetmap.josm.TestUtils;
    16 import org.openstreetmap.josm.tools.Utils;
     16import org.openstreetmap.josm.tools.Stopwatch;
    1717
    1818/**
     
    4646            System.out.println("Playing " + w.toPath());
    4747            URL url = w.toURI().toURL();
    48             long start = System.currentTimeMillis();
     48            final Stopwatch stopwatch = Stopwatch.createStarted();
    4949            AudioPlayer.play(url);
    5050            assertTrue(AudioPlayer.playing());
     
    5454            assertTrue(AudioPlayer.paused());
    5555            AudioPlayer.play(url, AudioPlayer.position());
    56             while (AudioPlayer.playing() && (System.currentTimeMillis() - start) < MAX_DURATION) {
     56            while (AudioPlayer.playing() && stopwatch.elapsed() < MAX_DURATION) {
    5757                Thread.sleep(500);
    5858            }
    59             long duration = System.currentTimeMillis() - start;
    60             System.out.println("Play finished after " + Utils.getDurationString(duration));
    61             assertTrue(duration < MAX_DURATION);
     59            System.out.println("Play finished after " + stopwatch);
     60            assertTrue(stopwatch.elapsed() < MAX_DURATION);
    6261            AudioPlayer.reset();
    6362            Thread.sleep(1000); // precaution, see #13809
Note: See TracChangeset for help on using the changeset viewer.