forked from fanruan/design
plough
6 years ago
4 changed files with 161 additions and 14 deletions
@ -0,0 +1,72 @@
|
||||
package com.fr.design.mainframe.template.info; |
||||
|
||||
/** |
||||
* Created by plough on 2019/4/19. |
||||
*/ |
||||
public class TimeConsumeTimer { |
||||
private static final int ONE_THOUSAND = 1000; |
||||
private enum State { |
||||
RUNNING, STOPPED |
||||
} |
||||
private int timeConsume; // 单位 s
|
||||
private long startMS; // 单位 ms
|
||||
private long stopMS; |
||||
private State state; |
||||
private boolean enabled; |
||||
|
||||
public TimeConsumeTimer() { |
||||
reset(); |
||||
} |
||||
|
||||
public boolean isEnabled() { |
||||
return enabled; |
||||
} |
||||
|
||||
public void setEnabled(boolean enabled) { |
||||
this.enabled = enabled; |
||||
} |
||||
|
||||
public void start() { |
||||
if (!isEnabled() || isRunning()) { |
||||
return; |
||||
} |
||||
startMS = System.currentTimeMillis(); |
||||
state = State.RUNNING; |
||||
} |
||||
|
||||
public void stop() { |
||||
if (!isEnabled() || !isRunning()) { |
||||
return; |
||||
} |
||||
stopMS = System.currentTimeMillis(); |
||||
|
||||
timeConsume += ((stopMS - startMS) / ONE_THOUSAND); |
||||
startMS = 0; |
||||
stopMS = 0; |
||||
state = State.STOPPED; |
||||
|
||||
|
||||
System.out.println("timeConsume now: " + timeConsume); |
||||
} |
||||
|
||||
public int popTime() { |
||||
if (!isEnabled()) { |
||||
return 0; |
||||
} |
||||
stop(); |
||||
int result = timeConsume; |
||||
reset(); |
||||
return result; |
||||
} |
||||
|
||||
private boolean isRunning() { |
||||
return state == State.RUNNING; |
||||
} |
||||
|
||||
private void reset() { |
||||
timeConsume = 0; |
||||
startMS = 0; |
||||
stopMS = 0; |
||||
state = State.STOPPED; |
||||
} |
||||
} |
@ -0,0 +1,62 @@
|
||||
package com.fr.design.mainframe.template.info; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* Created by plough on 2019/4/19. |
||||
*/ |
||||
public class TimeConsumeTimerTest { |
||||
|
||||
@Test |
||||
public void testNotEnabled() throws InterruptedException { |
||||
TimeConsumeTimer consumeTimer = new TimeConsumeTimer(); |
||||
consumeTimer.start(); |
||||
Thread.sleep(1100); |
||||
consumeTimer.stop(); |
||||
assertEquals(0, consumeTimer.popTime()); |
||||
} |
||||
|
||||
@Test |
||||
public void testEnabled() throws InterruptedException { |
||||
TimeConsumeTimer consumeTimer = new TimeConsumeTimer(); |
||||
consumeTimer.setEnabled(true); |
||||
consumeTimer.start(); |
||||
Thread.sleep(1100); |
||||
consumeTimer.stop(); |
||||
assertEquals(1, consumeTimer.popTime()); |
||||
assertEquals(0, consumeTimer.popTime()); |
||||
} |
||||
|
||||
@Test |
||||
public void testMultiTimes() throws InterruptedException { |
||||
TimeConsumeTimer consumeTimer = new TimeConsumeTimer(); |
||||
consumeTimer.setEnabled(true); |
||||
|
||||
consumeTimer.start(); |
||||
Thread.sleep(1010); |
||||
consumeTimer.stop(); |
||||
|
||||
Thread.sleep(2000); |
||||
|
||||
consumeTimer.start(); |
||||
Thread.sleep(1010); |
||||
assertEquals(2, consumeTimer.popTime()); |
||||
} |
||||
|
||||
@Test |
||||
public void testStartMultiTime() throws InterruptedException { |
||||
TimeConsumeTimer consumeTimer = new TimeConsumeTimer(); |
||||
consumeTimer.setEnabled(true); |
||||
|
||||
consumeTimer.start(); |
||||
Thread.sleep(1010); |
||||
consumeTimer.start(); |
||||
Thread.sleep(1010); |
||||
consumeTimer.start(); |
||||
Thread.sleep(1010); |
||||
|
||||
assertEquals(3, consumeTimer.popTime()); |
||||
} |
||||
} |
Loading…
Reference in new issue