package com.taobao.stable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import junit.framework.TestCase;
import com.taobao.common.stable.Console;
import com.taobao.common.stable.CtSph;
import com.taobao.common.stable.SphD;
import com.taobao.common.stable.SphMonitor;
import com.taobao.common.stable.SphDRuntime;
import com.taobao.common.stable.ValveType;
/**
* xiaoxie
* 2010-11-16
*/
public class SphTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSphD() {
CtSph sph = new CtSph("sph8", 10, 150, ValveType.COUNT_AND_AVGELAPSED_VALVE_TYPE);
this.assertNotNull(SphD.get("sph8"));
}
public void testcountValve () {
// ������ֵ��Ч
int taskCount = 25;
CountDownLatch latch = new CountDownLatch(taskCount);
CtSph sph = new CtSph(2, 200, ValveType.COUNT_VALVE_TYPE);
SearchTask task = new SearchTask(sph);
task.setCountDownLatch(latch);
ExecutorService es = Executors.newFixedThreadPool(40);
for (int i = 0;i < taskCount;i ++) {
es.submit(task);
}
try {
latch.await();
} catch (InterruptedException e) {
}
System.out.println(task.success());
System.out.println(task.failed());
assertEquals(task.success(), 20);
assertEquals(task.failed(), 5);
// ƽ��ʱ�䷧ֵ��Ч
}
public void AvgRTValve () throws InterruptedException {
// ������ֵ��Ч
int taskCount = 1370;
CountDownLatch latch = new CountDownLatch(taskCount);
// ����500���̶߳���������140ms��ʱ
CtSph sph = new CtSph(10, 150, ValveType.COUNT_AND_AVGELAPSED_VALVE_TYPE);
SphDRuntime.put("search", sph);
SearchTask task = new SearchTask(sph);
task.setSleeps(new int[]{
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
200,200,200,200,200,200,200,200,200,200,
200,200,200,200,200,200,200,200,200,200,
200,200,200,200,200,200,200,200,200,200,
400,400,400,400,400,400,400,400,400,400,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100
});
task.setCountDownLatch(latch);
ExecutorService es = Executors.newFixedThreadPool(100);
es.submit(new Thread() {
public void run() {
while (true) {
System.out.println(Console.command("show", null));
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
});
es.submit(new SphMonitor());
for (int i = 0;i < taskCount;i ++) {
es.submit(task);
Thread.sleep(20);
}
System.out.println("�ȴ�20s");
Thread.sleep(20000);
for (int i = 0;i < taskCount;i ++) {
es.submit(task);
Thread.sleep(20);
}
try {
latch.await();
} catch (InterruptedException e) {
}
assertEquals(task.success(), 20);
assertEquals(task.failed(), 5);
// ƽ��ʱ�䷧ֵ��Ч
//es.shutdown();
}
}