Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix rest impl time #353

Merged
merged 10 commits into from Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,6 +17,8 @@
package com.alipay.sofa.rpc.client.lb;

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -28,6 +30,8 @@
*/
public abstract class BaseLoadBalancerTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(BaseLoadBalancerTest.class);

protected List<ProviderInfo> buildSameWeightProviderList(int size) {
List<ProviderInfo> aliveConnections = new ArrayList<ProviderInfo>();
for (int i = 0; i < size; i++) {
Expand Down
Expand Up @@ -18,6 +18,8 @@

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -31,6 +33,7 @@
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class ConsistentHashLoadBalancerTest extends BaseLoadBalancerTest {

@Test
public void doSelect() throws Exception {

Expand All @@ -53,8 +56,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int count = 0;
for (int i = 0; i < size; i++) {
Expand Down
Expand Up @@ -62,8 +62,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

for (int i = 0; i < size; i++) {
Assert.assertTrue(cnt.get(9000 + i) == 0);
Expand Down Expand Up @@ -98,8 +98,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

for (int i = 0; i < size; i++) {
Assert.assertTrue(cnt.get(9000 + i) == 0);
Expand Down
Expand Up @@ -52,8 +52,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -75,8 +75,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

Assert.assertTrue(cnt.get(9000) == 0);

Expand Down
Expand Up @@ -52,8 +52,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -73,8 +73,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

// 忽略了权重
int avg = total / size;
Expand All @@ -94,7 +94,7 @@ public static void main(String[] args) {
y = a[i + 1];
temp = gcd(x, y);
}
System.out.println(temp);
LOGGER.info(String.valueOf(temp));
}

public static int gcd(int a, int b) {
Expand All @@ -105,7 +105,7 @@ public static int gcd(int a, int b) {
b = temp;
System.out.println("gcd(" + a + ", " + b + ")=");
}
System.out.println(a);
LOGGER.info(String.valueOf(a));
return a;
}
}
Expand Up @@ -18,6 +18,8 @@

import com.alipay.sofa.rpc.client.ProviderInfo;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -31,6 +33,9 @@
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class WeightRoundRobinLoadBalancerTest extends BaseLoadBalancerTest {

private static final Logger LOGGER = LoggerFactory.getLogger(WeightRoundRobinLoadBalancerTest.class);

@Test
public void doSelect() throws Exception {
WeightRoundRobinLoadBalancer loadBalancer = new WeightRoundRobinLoadBalancer(null);
Expand All @@ -51,8 +56,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

int avg = total / size;
for (int i = 0; i < size; i++) {
Expand All @@ -73,8 +78,8 @@ public void doSelect() throws Exception {
cnt.put(port, cnt.get(port) + 1);
}
long end = System.currentTimeMillis();
System.out.println("elapsed" + (end - start) + "ms");
System.out.println("avg " + (end - start) * 1000 * 1000 / total + "ns");
LOGGER.info("elapsed" + (end - start) + "ms");
LOGGER.info("avg " + (end - start) * 1000 * 1000 / total + "ns");

Assert.assertTrue(cnt.get(9000) == 0);
int count = 0;
Expand Down
Expand Up @@ -24,7 +24,7 @@
*
* @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a>
*/
public class WeightRoundRobinLoadBalancerV2Test {
public class WeightRoundRobinLoadBalancerV2Test extends BaseLoadBalancerTest {

/**
* 上次选择的服务器
Expand Down Expand Up @@ -131,7 +131,7 @@ public static void main(String args[]) {

for (int i = 0; i < 15; i++) {
Server server = weightRoundRobin.getServer();
System.out.println("server " + server.getIp() + " weight=" + server.getWeight());
LOGGER.info("server " + server.getIp() + " weight=" + server.getWeight());
}
}

Expand Down
2 changes: 1 addition & 1 deletion core-impl/log/src/test/resources/log4j.xml
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
2 changes: 1 addition & 1 deletion core-impl/proxy/src/test/resources/log4j.xml
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
Expand Up @@ -281,6 +281,14 @@ public class RpcConstants {
* @since 5.4.0
*/
public static final String INTERNAL_KEY_CLIENT_SEND_TIME = INTERNAL_KEY_PREFIX + "client_send_time";

/**
* 内部使用的key:_server_receive_time 服务端接收时间戳,long
*
* @since 5.4.8
*/
public static final String INTERNAL_KEY_SERVER_RECEIVE_TIME = INTERNAL_KEY_PREFIX + "server_receive_time";

/**
* 内部使用的key:_router_record 路由记录,string
*/
Expand Down
Expand Up @@ -20,6 +20,8 @@
import com.alipay.sofa.rpc.client.aft.bean.HelloServiceTimeOutImpl;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.After;
import org.junit.Before;

Expand Down
Expand Up @@ -29,6 +29,8 @@
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
import com.alipay.sofa.rpc.ext.ExtensionLoaderFactory;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import com.alipay.sofa.rpc.module.FaultToleranceModule;
import com.alipay.sofa.rpc.module.Module;
import org.junit.After;
Expand All @@ -42,6 +44,9 @@
* @version $Id: FaultBaseTest.java, v 0.1 2017年11月16日 下午3:14 bystander Exp $
*/
public abstract class FaultBaseTest {

private static final Logger LOGGER = LoggerFactory.getLogger(FaultBaseTest.class);

public static final String APP_NAME1 = "testApp";
public static final String APP_NAME2 = "testAnotherApp";

Expand Down
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
Expand Up @@ -20,6 +20,8 @@
import com.alipay.sofa.rpc.context.RpcInvokeContext;
import com.alipay.sofa.rpc.context.RpcRunningState;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;
import com.alipay.sofa.rpc.log.Logger;
import com.alipay.sofa.rpc.log.LoggerFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;

Expand All @@ -30,6 +32,8 @@
*/
public abstract class BaseMeshTest {

protected static final Logger LOGGER = LoggerFactory.getLogger(BaseMeshTest.class);

@BeforeClass
public static void adBeforeClass() {
RpcRunningState.setUnitTestMode(true);
Expand Down
Expand Up @@ -49,9 +49,6 @@ public void testApplicationInfo() {
ApplicationInfoRequest request = new ApplicationInfoRequest();
request.setAppName("test");
boolean result = meshApiClient.registeApplication(request);

System.out.println(result);

Assert.assertTrue(result);
}

Expand Down Expand Up @@ -96,8 +93,6 @@ public void testPublish() {
request.setProviderMetaInfo(providerMetaInfo);
boolean result = meshApiClient.publishService(request);

System.out.println(result);

Assert.assertTrue(result);
}

Expand All @@ -109,7 +104,6 @@ public void testUnPublish() {
int result = meshApiClient.unPublishService(request);

Assert.assertEquals(1, result);
System.out.println(result);
}

@Test
Expand All @@ -123,8 +117,6 @@ public void testSubscribe() {

SubscribeServiceResult result = meshApiClient.subscribeService(request);

System.out.println(result);

Assert.assertTrue(result.isSuccess());
}

Expand All @@ -134,9 +126,6 @@ public void testUnSubscribe() {

request.setServiceName("com.alipay.rpc.common.service.facade.pb.SampleServicePb:1.0@DEFAULT");
boolean result = meshApiClient.unSubscribeService(request);

System.out.println(result);

Assert.assertTrue(result);
}

Expand Down
Expand Up @@ -132,7 +132,9 @@ public void testOnlyPublish() {
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Assert.assertTrue(!registedAppValue.booleanValue());

LOGGER.info("current registedAppValue is " + registedAppValue);
Assert.assertTrue(!registedAppValue);

ServerConfig serverConfig = new ServerConfig()
.setProtocol("bolt")
Expand All @@ -158,7 +160,9 @@ public void testOnlyPublish() {
e.printStackTrace();
}

Assert.assertTrue(registedAppValue.booleanValue());
LOGGER.info("final registedAppValue is " + registedAppValue);

Assert.assertTrue(registedAppValue);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion extension-impl/remoting-bolt/src/test/resources/log4j.xml
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
2 changes: 1 addition & 1 deletion extension-impl/remoting-http/src/test/resources/log4j.xml
Expand Up @@ -9,7 +9,7 @@
</appender>

<root>
<level value="DEBUG"/>
<level value="INFO"/>
<appender-ref ref="CONSOLE"/>
</root>

Expand Down
Expand Up @@ -18,6 +18,7 @@

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.context.RpcInternalContext;
import com.alipay.sofa.rpc.context.RpcRuntimeContext;

import javax.annotation.Priority;
import javax.ws.rs.container.ContainerRequestContext;
Expand All @@ -36,11 +37,16 @@ public class TraceResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {

// 补充服务端request和response大小
if (RpcInternalContext.isAttachmentEnable()) {
RpcInternalContext context = RpcInternalContext.getContext();
context.setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, requestContext.getLength());
context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, responseContext.getLength());
Long startTime = (Long) context.removeAttachment(RpcConstants.INTERNAL_KEY_SERVER_RECEIVE_TIME);
if (startTime != null) {
context.setAttachment(RpcConstants.INTERNAL_KEY_IMPL_ELAPSE, RpcRuntimeContext.now() - startTime);
}
}
}
}