-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathBenchMarkTest.java
224 lines (187 loc) · 8.75 KB
/
BenchMarkTest.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
* Tencent is pleased to support the open source community by making Mars available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* 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.
*/
//package com.tencent.mars.sample;
import com.google.protobuf.nano.CodedOutputByteBufferNano;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import com.tencent.mars.sample.proto.Main;
import com.tencent.mars.sample.wrapper.remote.MarsServiceProxy;
import com.tencent.mars.sample.wrapper.remote.NanoMarsTaskWrapper;
import com.tencent.mars.xlog.Log;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.Random;
/**
* Created by astrozhou on 17/1/13.
*/
public class BenchMarkTest {
private static final String TAG = "Mars.Sample.BenchMark";
private long start_time;
private long task_time;
private long task_cnt;
private long task_suc;
private long suc_time;
private long okhttp_start_time;
private long okhttp_task_time;
private long okhttp_task_cnt;
private long okhttp_task_suc;
private long okhttp_suc_time;
public BenchMarkTest() {
}
public enum BenchMarkScene {
SceneNormalTest,
SceneSensitivity,
}
private BenchMarkScene scene;
private class HelloTaskWrapper extends NanoMarsTaskWrapper<Main.HelloRequest, Main.HelloResponse> {
public HelloTaskWrapper() {
super(new Main.HelloRequest(), new Main.HelloResponse());
request.user = "mars";
request.text = Integer.toString((int)task_cnt);
//BenchMark: request 64KB, 128KB
/*request.dumpContent = new byte[64*1024];
Random rand = new Random();
rand.nextBytes(request.dumpContent);*/
setLongChannelSupport(false);
setShortChannelSupport(true);
}
@Override
public void onPreEncode(Main.HelloRequest req) {
}
@Override
public void onPostDecode(Main.HelloResponse response) {
}
@Override
public void onTaskEnd(final int errType, final int errCode) {
if(scene == BenchMarkScene.SceneSensitivity) {
Log.i(TAG, "mars cost:" + (System.currentTimeMillis() - task_time) + "errtype:" + errType + ", errcode:" + errCode + ", msg:" + response.errmsg);
return;
}
long curr = System.currentTimeMillis();
task_cnt++;
if(errType == 0 && errCode == 0 && response.retcode == 0) {
task_suc++;
suc_time += curr - task_time;
//Log.i(TAG, "mars suc cost:" + (curr - task_time) + ", count:" + task_cnt + ", suc count:" + task_suc + ", msg:" + response.errmsg + ", total:" + (curr - start_time));
} else {
Log.e(TAG, "mars fail cost:" + (curr - task_time) + ", count:" + task_cnt + ", total:" + (curr - start_time));
}
if((task_suc%50) == 0) {
Log.e(TAG, "mars total:" + (curr - start_time) + ", avg:" + (suc_time)/(double)task_suc + ",rate:" + task_suc/(double)task_cnt + ", count:" + task_cnt + ", task suc:" + task_suc);
}
if(task_suc < 500) {
task_time = System.currentTimeMillis();
HelloTaskWrapper task = new HelloTaskWrapper();
MarsServiceProxy.send(task.setHttpRequest("118.89.24.72", "/mars/hello2").setCmdID(8));
} else {
String msg = "final mars total:" + (curr - start_time) + ", avg:" + (suc_time)/(double)task_suc + ",rate:" + task_suc/(double)task_cnt + ", count:" + task_cnt + ", suc:" + task_suc;
Log.e(TAG, msg);
}
}
}
public void StartMarsTest() {
scene = BenchMarkScene.SceneNormalTest;
Log.i(TAG, "start mars");
start_time = task_time = task_cnt = task_suc = suc_time = 0;
start_time = System.currentTimeMillis();
HelloTaskWrapper task = new HelloTaskWrapper();
task_time = System.currentTimeMillis();
MarsServiceProxy.send(task.setHttpRequest("118.89.24.72", "/mars/hello2").setCmdID(8));
}
public void StartOkHttpTest() {
scene = BenchMarkScene.SceneNormalTest;
Log.i(TAG, "start okhttp");
okhttp_start_time = okhttp_task_time = okhttp_task_cnt = okhttp_task_suc = okhttp_suc_time = 0;
okhttp_start_time = System.currentTimeMillis();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while(true) {
doOkHttpRequest();
if((okhttp_task_suc%50) == 0) {
Log.e(TAG, "http total:" + (System.currentTimeMillis() - okhttp_start_time) + ", avg:" + (okhttp_suc_time)/(double)okhttp_task_suc + ",rate:" + (okhttp_task_suc/(double)okhttp_task_cnt) + ", count:" + okhttp_task_cnt + ", suc count:" + okhttp_task_suc);
}
if(okhttp_task_suc >= 500) {
String msg = "final http total:" + (System.currentTimeMillis() - okhttp_start_time) + ", avg:" + (okhttp_suc_time)/(double)okhttp_task_suc + ",rate:" + (okhttp_task_suc/(double)okhttp_task_cnt) + ", count:" + okhttp_task_cnt + ", suc:" + okhttp_task_suc;
Log.e(TAG, msg);
return;
}
}
}
});
thread.start();
}
/*
* benchmark scene: sensitivity test
* step 1: set network 100% Loss
* step 2: after 5s, 10s, 15s....40s, set network available
* step 3: compare the cgi result
*/
public void StartSensitivityTest() {
scene = BenchMarkScene.SceneSensitivity;
Log.i(TAG, "start sensitivity test");
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
doOkHttpRequest();
}
});
thread.start();
task_time = System.currentTimeMillis();
HelloTaskWrapper task = new HelloTaskWrapper();
MarsServiceProxy.send(task.setHttpRequest("118.89.24.72", "/mars/hello2").setCmdID(8));
}
void doOkHttpRequest() {
++okhttp_task_cnt;
try {
OkHttpClient client = new OkHttpClient();
Main.HelloRequest req = new Main.HelloRequest();
req.user = "okhttp";
req.text = Integer.toString((int)okhttp_task_cnt);
//Benchmark 64KB/128KB
/*req.dumpContent = new byte[64*1024];
Random rand = new Random();
rand.nextBytes(req.dumpContent);*/
final byte[] flatArray = new byte[req.getSerializedSize()];
final CodedOutputByteBufferNano output = CodedOutputByteBufferNano.newInstance(flatArray);
req.writeTo(output);
RequestBody reqBody = RequestBody.create(MediaType.parse("application/octet-stream"), flatArray);
//normal request
Request request = new Request.Builder()
.url("http://118.89.24.72:8080/mars/hello2")
.addHeader("Cache-Control", "no-cache")
.addHeader("Content-Type", "application/octet-stream")
.addHeader("Connection", "close")
.addHeader("Accept", "*/*")
.post(reqBody)
.build();
okhttp_task_time = System.currentTimeMillis();
// Execute the request and retrieve the response.
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
Main.HelloResponse helloResp = Main.HelloResponse.parseFrom(body.bytes());
body.close();
long curr = System.currentTimeMillis();
okhttp_suc_time += curr - okhttp_task_time;
++okhttp_task_suc;
//Log.i(TAG, "http type:" + type + ", suc cost:" + (curr - okhttp_task_time) + ", count:" + okhttp_task_cnt + ", suc count:" + okhttp_task_suc + ", ctn:" + helloResp.errmsg);
} catch(Exception e) {
Log.e(TAG, "http fail cost:" + (System.currentTimeMillis() - okhttp_task_time) + ", count:" + okhttp_task_cnt);
}
}
}