`

MemcachedTest

 
阅读更多
/**
* Copyright (c) 2008 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author Greg Whalin <greg@meetup.com>
*/
package com.meetup.memcached.test;

import java.util.Hashtable;

import com.yulong.memcached.MemcachedClient;
import com.yulong.memcached.SockIOPool;

public class MemcachedTest {

// store results from threads
private static Hashtable<Integer,StringBuilder> threadInfo =
new Hashtable<Integer,StringBuilder>();
   
/**
* This runs through some simple tests of the MemcacheClient.
*
* Command line args:
* args[0] = number of threads to spawn
* args[1] = number of runs per thread
* args[2] = size of object to store
*
* @param args the command line arguments
*/
public static void main(String[] args) {

String[] serverlist = { "cache1.int.meetup.com:12345", "cache0.int.meetup.com:12345" };

// initialize the pool for memcache servers
SockIOPool pool = SockIOPool.getInstance();
pool.setServers( serverlist );

pool.setInitConn(5);
pool.setMinConn(5);
pool.setMaxConn(50);
pool.setMaintSleep(30);

pool.setNagle(false);
pool.initialize();

int threads = Integer.parseInt(args[0]);
int runs = Integer.parseInt(args[1]);
int size = 1024 * Integer.parseInt(args[2]); // how many kilobytes

// get object to store
int[] obj = new int[size];
for (int i = 0; i < size; i++) {
obj[i] = i;
}

String[] keys = new String[size];
for (int i = 0; i < size; i++) {
keys[i] = "test_key" + i;
}

for (int i = 0; i < threads; i++) {
bench b = new bench(runs, i, obj, keys);
b.start();
}

int i = 0;
while (i < threads) {
if (threadInfo.containsKey(new Integer(i))) {
System.out.println( threadInfo.get( new Integer( i ) ) );
i++;
}
else {
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}

pool.shutDown();
System.exit(1);
}

/**
* Test code per thread.
*/
private static class bench extends Thread {
private int runs;
private int threadNum;
private int[] object;
private String[] keys;
private int size;

public bench(int runs, int threadNum, int[] object, String[] keys) {
this.runs = runs;
this.threadNum = threadNum;
this.object = object;
this.keys = keys;
this.size = object.length;
}

public void run() {

StringBuilder result = new StringBuilder();

// get client instance
MemcachedClient mc = new MemcachedClient();
mc.setCompressEnable(false);
mc.setCompressThreshold(0);

// time deletes
long start = System.currentTimeMillis();
for (int i = 0; i < runs; i++) {
mc.delete(keys[i]);
}
long elapse = System.currentTimeMillis() - start;
float avg = (float) elapse / runs;
result.append("\nthread " + threadNum + ": runs: " + runs + " deletes of obj " + (size/1024) + "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");

// time stores
start = System.currentTimeMillis();
for (int i = 0; i < runs; i++) {
mc.set(keys[i], object);
}
elapse = System.currentTimeMillis() - start;
avg = (float) elapse / runs;
result.append("\nthread " + threadNum + ": runs: " + runs + " stores of obj " + (size/1024) + "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");

start = System.currentTimeMillis();
for (int i = 0; i < runs; i++) {
mc.get(keys[i]);
}
elapse = System.currentTimeMillis() - start;
avg = (float) elapse / runs;
result.append("\nthread " + threadNum + ": runs: " + runs + " gets of obj " + (size/1024) + "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)");

threadInfo.put(new Integer(threadNum), result);
}
}
}
分享到:
评论

相关推荐

    memcachedtest

    标题中的"memcachedtest"指的是一个使用Java编写的Memcached测试程序。Memcached是一种高性能、分布式内存对象缓存系统,常用于加速动态Web应用。它通过将数据存储在内存中来减少数据库的负载,提高数据读取速度。在...

    Memcached code

    `MemcachedTest`可能是项目的测试项目,其中包含各种测试用例,以确保Enyim.Caching库的功能正确性和性能表现。测试对于任何软件开发都是至关重要的,尤其是对于像Memcached这样的关键基础设施组件。 **Enyim....

    MemCached-在Widnows环境下的Java之简单应用.docx

    在测试类`MemcachedTest`中,首先初始化`MemCachedClient`,并设置服务器列表(默认是本地11211端口)。`SockIOPool`用于管理连接池,通过`setServers()`方法配置服务器地址,并调用`initialize()`方法初始化。 `...

    MemCached Provider客户端

    在实际开发中,`MemcachedTest` 文件可能包含了一些示例代码或测试用例,用于演示如何使用 `MemcachedProviders` 进行缓存操作,例如初始化客户端、设置和获取缓存项、使用计数器以及管理 Session 等。 总的来说,`...

    danga memcached使用

    压缩包中的“memcachedTest”可能是用于测试memcached功能的脚本或代码。通过运行这个测试,我们可以验证memcached的正确配置和操作,确保其正常工作。 总结,danga memcached是一个高效、轻量级的分布式缓存系统,...

    分布式缓存Memcached实例

    在提供的压缩包中,可能包含了一些关于如何使用或测试Memcached的资源,如`MemcachedTest`可能是用于测试Memcached的脚本或工具,而`网盘资源搜索-【51pansou.com】.txt`和`51pansou-海量网盘资源任意搜索.url`可能...

    Linxu下配置Memcached

    public class MemcachedTest { public static void main(String[] args) { try { MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().build(), AddrUtil.getAddresses("your_server_...

    memcached安装包及测试

    public class MemcachedTest { public static void main(String[] args) { try { MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().build(), AddrUtil.getAddresses("localhost:...

    SpringBoot 集成 Memcached的方法示例

    public class MemcachedTest { @Autowired private MemcachedClient memcachedClient; @Test public void testMemcached() { // 使用 memcachedClient 对象进行缓存操作 } } ``` 通过以上步骤,我们可以成功...

Global site tag (gtag.js) - Google Analytics