Github user lvfangmin commented on a diff in the pull request: https://github.com/apache/zookeeper/pull/684#discussion_r229871456 --- Diff: zookeeper-server/src/test/java/org/apache/zookeeper/test/ResponseCacheTest.java --- @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ + +package org.apache.zookeeper.test; + +import java.util.Map; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.apache.zookeeper.data.Stat; +import org.apache.zookeeper.server.ServerStats; +import org.apache.zookeeper.server.ServerMetrics; +import org.apache.zookeeper.server.ZooKeeperServerMXBean; +import org.junit.Assert; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.management.JMX; +import javax.management.MBeanServerConnection; +import javax.management.ObjectName; + +public class ResponseCacheTest extends ClientBase { + protected static final Logger LOG = + LoggerFactory.getLogger(ResponseCacheTest.class); + + @Test + public void testResponseCache() throws Exception { + ObjectName bean = JMXEnv.getServerBean(); + MBeanServerConnection mbsc = JMXEnv.conn(); + ZooKeeperServerMXBean zkBean = JMX.newMBeanProxy(mbsc, bean, ZooKeeperServerMXBean.class); + ZooKeeper zk = createClient(); + + try { + performCacheTest(zk, zkBean, "/cache", true); + performCacheTest(zk, zkBean, "/nocache", false); + } + finally { + zk.close(); + } + } + + private void checkCacheStatus(long expectedHits, long expectedMisses) { + Map<String, Long> metrics = ServerMetrics.getAllValues(); + Assert.assertEquals((Long) expectedHits, metrics.get("response_packet_cache_hits")); + Assert.assertEquals((Long) expectedMisses, metrics.get("response_packet_cache_misses")); + } + + public void performCacheTest(ZooKeeper zk, ZooKeeperServerMXBean zkBean, String path, boolean useCache) throws Exception { + ServerMetrics.resetAll(); + Stat writeStat = new Stat(); + Stat readStat = new Stat(); + byte[] readData = null; + int reads = 10; + long expectedHits = 0; + long expectedMisses = 0; + + zkBean.setResponseCachingEnabled(useCache); --- End diff -- Brian, looks like we call ZooKeeperServer.setResponseCachingEnabled directly instead of introducing the JMX bean here, can you check if we can get rid of the JMX bean change in this diff?
---