|
| 1 | +/** |
| 2 | + * Copyright (c) 2008 Greg Whalin |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the BSD license |
| 7 | + * |
| 8 | + * This library is distributed in the hope that it will be |
| 9 | + * useful, but WITHOUT ANY WARRANTY; without even the implied |
| 10 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 11 | + * PURPOSE. |
| 12 | + * |
| 13 | + * You should have received a copy of the BSD License along with this |
| 14 | + * library. |
| 15 | + * |
| 16 | + * @author Greg Whalin <[email protected]> |
| 17 | + */ |
| 18 | +package com.whalin.MemCached.test; |
| 19 | + |
| 20 | +import java.util.Random; |
| 21 | + |
| 22 | +import com.whalin.MemCached.MemCachedClient; |
| 23 | +import com.whalin.MemCached.SockIOPool; |
| 24 | + |
| 25 | +public class MemcachedAvailTest { |
| 26 | + |
| 27 | + /** |
| 28 | + * This runs through some simple tests of the MemcacheClient. |
| 29 | + * |
| 30 | + * Command line args: args[0] = number of threads to spawn args[1] = number |
| 31 | + * of runs per thread args[2] = size of object to store |
| 32 | + * |
| 33 | + * @param args |
| 34 | + * the command line arguments |
| 35 | + */ |
| 36 | + public static void main(String[] args) { |
| 37 | + |
| 38 | + String[] serverlist = { "dev12:11211" }; |
| 39 | + |
| 40 | + int threadNum = Integer.parseInt(args[0]); |
| 41 | + int runs = Integer.parseInt(args[1]); |
| 42 | + int size = 1024 * Integer.parseInt(args[2]); // how many kilobytes |
| 43 | + |
| 44 | + // initialize the pool for memcache servers |
| 45 | + SockIOPool pool = SockIOPool.getInstance(); |
| 46 | + pool.setServers(serverlist); |
| 47 | + |
| 48 | + pool.initialize(); |
| 49 | + |
| 50 | + // get object to store |
| 51 | + int[] obj = new int[size]; |
| 52 | + for (int i = 0; i < size; i++) { |
| 53 | + obj[i] = i; |
| 54 | + } |
| 55 | + |
| 56 | + String[] keys = new String[size]; |
| 57 | + for (int i = 0; i < size; i++) { |
| 58 | + keys[i] = "test_key" + i; |
| 59 | + } |
| 60 | + Bench[] bens = new Bench[threadNum]; |
| 61 | + for (int i = 0; i < threadNum; i++) { |
| 62 | + bens[i] = new Bench(runs, i, obj, keys); |
| 63 | + bens[i].start(); |
| 64 | + } |
| 65 | + for (Bench ben : bens) { |
| 66 | + try { |
| 67 | + ben.join(); |
| 68 | + } catch (InterruptedException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | + pool.shutDown(); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Test code per thread. |
| 77 | + */ |
| 78 | + private static class Bench extends Thread { |
| 79 | + private int runs; |
| 80 | + private int threadNum; |
| 81 | + private int[] object; |
| 82 | + private String[] keys; |
| 83 | + private int size; |
| 84 | + |
| 85 | + public Bench(int runs, int threadNum, int[] object, String[] keys) { |
| 86 | + this.runs = runs; |
| 87 | + this.threadNum = threadNum; |
| 88 | + this.object = object; |
| 89 | + this.keys = keys; |
| 90 | + this.size = object.length; |
| 91 | + } |
| 92 | + |
| 93 | + public void run() { |
| 94 | + |
| 95 | + // get client instance |
| 96 | + MemCachedClient mc = new MemCachedClient(); |
| 97 | + |
| 98 | + // time stores |
| 99 | + long start = System.currentTimeMillis(); |
| 100 | + for (int i = 0; i < runs; i++) { |
| 101 | + mc.set(keys[i], object); |
| 102 | + } |
| 103 | + long elapse = System.currentTimeMillis() - start; |
| 104 | + float avg = (float) elapse / runs; |
| 105 | + System.out.println("thread " + threadNum + ": runs: " + runs + " stores of obj " + (size / 1024) |
| 106 | + + "KB -- avg time per req " + avg + " ms (total: " + elapse + " ms)"); |
| 107 | + Random r = new Random(); |
| 108 | + for (int i = 0; i < runs; i++) { |
| 109 | + try { |
| 110 | + Thread.sleep(r.nextInt(10)); |
| 111 | + } catch (InterruptedException e) { |
| 112 | + e.printStackTrace(); |
| 113 | + } |
| 114 | + start = System.nanoTime(); |
| 115 | + mc.get(keys[i]); |
| 116 | + elapse = (System.nanoTime() - start) / 1000000; |
| 117 | + if (elapse > avg * 3) |
| 118 | + System.err.println(elapse + "ms for get in " + Thread.currentThread().getName()); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments