Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Measure server processing and idle time
[#150061059] https://www.pivotaltracker.com/story/show/150061059
- Loading branch information
Showing
4 changed files
with
239 additions
and
5 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
server/src/main/java/org/cloudfoundry/identity/uaa/metrics/IdleTimer.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,73 @@ | ||
/* | ||
* **************************************************************************** | ||
* Cloud Foundry | ||
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved. | ||
* | ||
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this product except in compliance with the License. | ||
* | ||
* This product includes a number of subcomponents with | ||
* separate copyright notices and license terms. Your use of these | ||
* subcomponents is subject to the terms and conditions of the | ||
* subcomponent's license, as noted in the LICENSE file. | ||
* **************************************************************************** | ||
*/ | ||
|
||
package org.cloudfoundry.identity.uaa.metrics; | ||
|
||
/** | ||
* Calculates the time that a server is idle (no requests processing) | ||
* The idle time calculator starts as soon as this object is created. | ||
*/ | ||
public class IdleTimer { | ||
|
||
private int inflightRequests = 0; | ||
private long idleTime = 0; | ||
private long lastIdleStart = System.currentTimeMillis(); | ||
private final long startTime = System.currentTimeMillis(); | ||
private long requestCount = 0; | ||
|
||
public synchronized void endRequest() { | ||
switch (--inflightRequests) { | ||
case 0: | ||
lastIdleStart = System.currentTimeMillis(); | ||
break; | ||
case -1: | ||
throw new IllegalStateException("Illegal end request invocation, no request in flight"); | ||
default: | ||
break; | ||
} | ||
requestCount++; | ||
} | ||
|
||
public synchronized void startRequest() { | ||
switch (++inflightRequests) { | ||
case 1: | ||
idleTime += (System.currentTimeMillis() - lastIdleStart); | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
} | ||
|
||
public int getInflightRequests() { | ||
return inflightRequests; | ||
} | ||
|
||
public synchronized long getIdleTime() { | ||
if (inflightRequests == 0) { | ||
return (System.currentTimeMillis() - lastIdleStart) + idleTime; | ||
} else { | ||
return idleTime; | ||
} | ||
} | ||
|
||
public long getRequestCount() { | ||
return requestCount; | ||
} | ||
|
||
public long getRunTime() { | ||
return System.currentTimeMillis() - startTime; | ||
} | ||
} |
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
99 changes: 99 additions & 0 deletions
99
server/src/test/java/org/cloudfoundry/identity/uaa/metrics/IdleTimerTests.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,99 @@ | ||
/* | ||
* **************************************************************************** | ||
* Cloud Foundry | ||
* Copyright (c) [2009-2017] Pivotal Software, Inc. All Rights Reserved. | ||
* | ||
* This product is licensed to you under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this product except in compliance with the License. | ||
* | ||
* This product includes a number of subcomponents with | ||
* separate copyright notices and license terms. Your use of these | ||
* subcomponents is subject to the terms and conditions of the | ||
* subcomponent's license, as noted in the LICENSE file. | ||
* **************************************************************************** | ||
*/ | ||
|
||
package org.cloudfoundry.identity.uaa.metrics; | ||
|
||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class IdleTimerTests { | ||
|
||
public static final int LOOP_COUNT = 100000; | ||
private IdleTimer timer; | ||
|
||
@Rule | ||
public ExpectedException exception = ExpectedException.none(); | ||
public static final int THREAD_COUNT = 10; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
timer = new IdleTimer(); | ||
} | ||
|
||
@Test | ||
public void timer_started() throws Exception { | ||
Thread.sleep(10); | ||
assertEquals(0, timer.getInflightRequests()); | ||
assertThat(timer.getRunTime(), greaterThan(0l)); | ||
assertThat(timer.getIdleTime(), greaterThan(0l)); | ||
} | ||
|
||
@Test | ||
public void illegal_end_request() throws Exception { | ||
exception.expect(IllegalStateException.class); | ||
exception.expectMessage("Illegal end request invocation, no request in flight"); | ||
timer.endRequest(); | ||
} | ||
|
||
@Test | ||
public void while_inflight() throws Exception { | ||
timer.startRequest(); | ||
long idleTime = timer.getIdleTime(); | ||
assertEquals(1, timer.getInflightRequests()); | ||
timer.startRequest(); | ||
assertEquals(2, timer.getInflightRequests()); | ||
timer.endRequest(); | ||
assertEquals(1, timer.getInflightRequests()); | ||
Thread.sleep(10); | ||
assertEquals("Idle time should not have changed.", idleTime, timer.getIdleTime()); | ||
timer.endRequest(); | ||
assertEquals(0, timer.getInflightRequests()); | ||
Thread.sleep(10); | ||
assertThat("Idle time should have changed.", timer.getIdleTime(), greaterThan(idleTime)); | ||
} | ||
|
||
@Test | ||
public void concurrency_test() throws Exception { | ||
final CountDownLatch latch = new CountDownLatch(THREAD_COUNT); | ||
Thread[] threads = new Thread[THREAD_COUNT]; | ||
for (int i = 0; i< THREAD_COUNT; i++) { | ||
threads[i] = new Thread(() -> { | ||
for (int loop = 0; loop< LOOP_COUNT; loop++) { | ||
try { | ||
timer.startRequest(); | ||
} finally { | ||
timer.endRequest(); | ||
} | ||
} | ||
latch.countDown(); | ||
}); | ||
} | ||
for (int i = 0; i< THREAD_COUNT; i++) { | ||
threads[i].start(); | ||
} | ||
latch.await(); | ||
assertEquals(THREAD_COUNT * LOOP_COUNT, timer.getRequestCount()); | ||
assertEquals(0, timer.getInflightRequests()); | ||
} | ||
|
||
} |
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