-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the debounce adaptive for validation job (#1973)
* Make the debounce adaptive for validation job Signed-off-by: Sheng Chen <[email protected]>
- Loading branch information
Showing
3 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/MovingAverage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.ls.core.internal; | ||
|
||
/** | ||
* A class used to calculate the cumulative moving average. | ||
* @see <a href="https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average">Definition from Wikipedia</a>. | ||
*/ | ||
public class MovingAverage { | ||
/** | ||
* The average value | ||
*/ | ||
public long value; | ||
|
||
private long n = 1; | ||
|
||
public MovingAverage() { | ||
this(0); | ||
} | ||
|
||
/** | ||
* Initialize the moving average with a initial value | ||
* @param initValue The initial value of the moving average | ||
*/ | ||
public MovingAverage(long initValue) { | ||
this.value = initValue; | ||
} | ||
|
||
/** | ||
* Update the moving average value | ||
* @param value A new value used to update the moving average | ||
* @return The <code>MovingAverage</code> instance | ||
*/ | ||
public MovingAverage update(long value) { | ||
this.value = this.value + (value - this.value) / this.n; | ||
this.n += 1; | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/MovingAverageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Microsoft Corporation and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Microsoft Corporation - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package org.eclipse.jdt.ls.core.internal; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class MovingAverageTest { | ||
|
||
@Test | ||
public void testUpdate() { | ||
MovingAverage average = new MovingAverage(400); | ||
|
||
// initialize to 400 at first | ||
assertEquals(400, average.value); | ||
|
||
average.update(200); | ||
// the first input value takes over the initial value | ||
assertEquals(200, average.value); | ||
|
||
average.update(100); | ||
// (200 + 100) / 2 | ||
assertEquals(150, average.value); | ||
} | ||
} |