Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
夜色
committed
Dec 21, 2016
1 parent
da8f562
commit 44f7289
Showing
6 changed files
with
178 additions
and
24 deletions.
There are no files selected for viewing
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
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
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
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
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
131 changes: 131 additions & 0 deletions
131
mpush-test/src/main/java/com/mpush/test/push/PushClientTestMain2.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,131 @@ | ||
/* | ||
* (C) Copyright 2015-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Contributors: | ||
* ohun@live.cn (夜色) | ||
*/ | ||
|
||
package com.mpush.test.push; | ||
|
||
import com.mpush.api.push.*; | ||
import com.mpush.core.push.FlowControl; | ||
import com.mpush.core.push.GlobalFlowControl; | ||
import com.mpush.core.push.OverFlowException; | ||
import com.mpush.tools.log.Logs; | ||
import org.junit.Test; | ||
|
||
import java.time.LocalTime; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
/** | ||
* Created by ohun on 2016/1/7. | ||
* | ||
* @author ohun@live.cn | ||
*/ | ||
public class PushClientTestMain2 { | ||
|
||
public static void main(String[] args) throws Exception { | ||
new PushClientTestMain2().testPush(); | ||
} | ||
|
||
@Test | ||
public void testPush() throws Exception { | ||
Logs.init(); | ||
PushSender sender = PushSender.create(); | ||
sender.start().join(); | ||
Thread.sleep(1000); | ||
Statistics statistics = new Statistics(); | ||
FlowControl flowControl = new GlobalFlowControl(3000, Integer.MAX_VALUE, 1000);// qps=1000 | ||
|
||
ScheduledExecutorService service = Executors.newScheduledThreadPool(2); | ||
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> { | ||
System.out.println("time=" + LocalTime.now() | ||
+ ", flowControl=" + flowControl.report() | ||
+ ", statistics=" + statistics | ||
); | ||
}, 1, 1, TimeUnit.SECONDS); | ||
|
||
for (int k = 0; k < 100; k++) { | ||
for (int i = 0; i < 1000; i++) { | ||
service.execute(new PushTask(sender, i, service, flowControl, statistics)); | ||
} | ||
} | ||
|
||
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(30000)); | ||
} | ||
|
||
private static class PushTask implements Runnable { | ||
PushSender sender; | ||
private int i; | ||
FlowControl flowControl; | ||
Statistics statistics; | ||
ScheduledExecutorService executor; | ||
|
||
public PushTask(PushSender sender, int i, ScheduledExecutorService executor, FlowControl flowControl, Statistics statistics) { | ||
this.sender = sender; | ||
this.i = i; | ||
this.flowControl = flowControl; | ||
this.executor = executor; | ||
this.statistics = statistics; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
if (flowControl.checkQps()) { | ||
PushMsg msg = PushMsg.build(MsgType.MESSAGE, "this a first push."); | ||
msg.setMsgId("msgId_" + i); | ||
|
||
PushContext context = PushContext.build(msg) | ||
.setAckModel(AckModel.NO_ACK) | ||
.setUserId("user-" + i) | ||
.setBroadcast(false) | ||
.setTimeout(60000) | ||
.setCallback(new PushCallback() { | ||
@Override | ||
public void onResult(PushResult result) { | ||
statistics.add(result.resultCode); | ||
} | ||
}); | ||
FutureTask<PushResult> future = sender.send(context); | ||
} else { | ||
executor.schedule(this, flowControl.getRemaining(), TimeUnit.NANOSECONDS); | ||
} | ||
} | ||
} | ||
|
||
private static class Statistics { | ||
final AtomicInteger successNum = new AtomicInteger(); | ||
final AtomicInteger failureNum = new AtomicInteger(); | ||
final AtomicInteger offlineNum = new AtomicInteger(); | ||
final AtomicInteger timeoutNum = new AtomicInteger(); | ||
AtomicInteger[] counters = new AtomicInteger[]{successNum, failureNum, offlineNum, timeoutNum}; | ||
|
||
private void add(int code) { | ||
counters[code - 1].incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "{" + | ||
"successNum=" + successNum + | ||
", offlineNum=" + offlineNum + | ||
", timeoutNum=" + timeoutNum + | ||
", failureNum=" + failureNum + | ||
'}'; | ||
} | ||
} | ||
} |