-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTestRedisRateLimiter.java
59 lines (50 loc) · 1.71 KB
/
TestRedisRateLimiter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.tay.RedisRateLimiter;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.codahale.metrics.Timer.Context;
import redis.clients.jedis.JedisPool;
public class TestRedisRateLimiter {
private static final MetricRegistry metrics = new MetricRegistry();
private static ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics).build();
private static final Meter requests = metrics.meter(MetricRegistry.name(TestRedisRateLimiter.class, "success"));
private Timer timer = metrics.timer(MetricRegistry.name(TestRedisRateLimiter.class, "totalRequest"));
@Test
public void testRedisRateLimit() throws InterruptedException {
reporter.start(10, TimeUnit.SECONDS);
ApplicationContext ac = new ClassPathXmlApplicationContext("root-context.xml");
Runnable runnable = () -> {
JedisPool pool = (JedisPool) ac.getBean("jedisPool");
RedisRateLimiter limiter = new RedisRateLimiter(pool, TimeUnit.MINUTES, 120);
while (true) {
boolean flag = false;
Context context = timer.time();
if(limiter.acquire("testMKey1")) {
flag = true;
}
context.stop();
if (flag) {
requests.mark();
}
try {
Thread.sleep(1);
}
catch(Exception e) {
}
}
};
int threadCount = 10;
for(int i = 0; i < threadCount; i++ ) {
Thread t = new Thread(runnable);
t.start();
}
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
}
}