一 java 中使用redis 测试Redis的写入性能

配置文件:

<?xml version=‘1.0‘ encoding=‘UTF-8‘ ?><!-- was: <?xml version="1.0" encoding="UTF-8"?> --><beans xmlns="http://www.springframework.org/schema/beans"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:context="http://www.springframework.org/schema/context"   xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd       http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd       http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd       http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.0.xsd">   <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">      <!--最大空闲数 -->      <property name="maxIdle" value="50" />      <!--最大连接数 -->      <property name="maxTotal" value="100" />      <!--最大等待时间 -->      <property name="maxWaitMillis" value="20000" />   </bean>

<bean id="connectionFactory"      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">      <property name="hostName" value="localhost" />      <property name="port" value="6379" />      <property name="password" value="123456"/>      <property name="poolConfig" ref="poolConfig" />   </bean>

<bean id="jdkSerializationRedisSerializer"      class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

<bean id="stringRedisSerializer"      class="org.springframework.data.redis.serializer.StringRedisSerializer" />

<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">      <property name="connectionFactory" ref="connectionFactory" />      <property name="keySerializer" ref="stringRedisSerializer" />      <property name="valueSerializer" ref="jdkSerializationRedisSerializer" />   </bean></beans>

实体类:
Role
package com.ssm.chapter17.pojo;

import java.io.Serializable;

public class Role implements Serializable {

private static final long serialVersionUID = 6977402643848374753L;

private long id;   private String roleName;   private String note;

public long getId() {      return id;   }

public void setId(long id) {      this.id = id;   }

public String getRoleName() {      return roleName;   }

public void setRoleName(String roleName) {      this.roleName = roleName;   }

public String getNote() {      return note;   }

public void setNote(String note) {      this.note = note;   }

}

工具类:
testJedis
package com.ssm.chapter17.jedis;

import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;

public class JedisTest {

public void testJedis() {      Jedis jedis = testPool().getResource();      // Jedis jedis = new Jedis("localhost", 6379); //连接Redis      // jedis.auth("password");//如果需密码      int i = 0;// 记录操作次数      try {         long start = System.currentTimeMillis();// 开始毫秒数         while (true) {            long end = System.currentTimeMillis();            if (end - start >= 1000) {// 当大于等于1000毫秒(相当于1秒)时,结束操作               break;            }            i++;            jedis.set("test" + i, i + "");         }      } finally {// 关闭连接         jedis.close();      }      System.out.println("redis每秒操作:" + i + "次");// 打印1秒内对Redis的操作次数   }

private JedisPool testPool() {      JedisPoolConfig poolCfg = new JedisPoolConfig();      // 最大空闲数      poolCfg.setMaxIdle(50);      // 最大连接数      poolCfg.setMaxTotal(100);      // 最大等待毫秒数      poolCfg.setMaxWaitMillis(20000);      // 使用配置创建连接池      JedisPool pool = new JedisPool(poolCfg, "localhost");      // 从连接池中获取单个连接      Jedis jedis = pool.getResource();      // 如果需密码      // jedis.auth("password");      return pool;   }}

测试类,有原生的方法,也有Spring 提供的方法:
package com.ssm.chapter17.main;

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.core.RedisOperations;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SessionCallback;

import com.ssm.chapter17.jedis.JedisTest;import com.ssm.chapter17.pojo.Role;

public class Chapter17Main {

public static void main(String[] args) {//    testSessionCallback();      testSpring();   }

private static void testJedis() {      JedisTest jedisTest = new JedisTest();      jedisTest.testJedis();   }

private static void testSpring() {      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");      RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);      Role role = new Role();      role.setId(1L);      role.setRoleName("role_name_1");      role.setNote("note_1");      redisTemplate.opsForValue().set("role_1", role);      Role role1 = (Role) redisTemplate.opsForValue().get("role_1");      System.out.println(role1.getRoleName());

}

private static void testSessionCallback() {      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");      RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);      Role role = new Role();      role.setId(1);      role.setRoleName("role_name_1");      role.setNote("role_note_1");      SessionCallback callBack = new SessionCallback<Role>() {         @Override         public Role execute(RedisOperations ops) throws DataAccessException {            ops.boundValueOps("role_1").set(role);            return (Role) ops.boundValueOps("role_1").get();         }      };      Role savedRole = (Role) redisTemplate.execute(callBack);      System.out.println(savedRole.getId());   }

}


原文地址:https://www.cnblogs.com/chensm/p/10242132.html

时间: 2024-07-29 11:24:56

一 java 中使用redis 测试Redis的写入性能的相关文章

PHP 使用Apache 中的ab 测试网站的压力性能

打开Apache服务器的安装路径(我用的是 WampServer),在bin目录中有一个ab.exe的可执行程序,它就是要介绍的压力测试工具. 在Windows系统的命令行下,进入ab.exe程序所在目录,执行ab.exe程序.注意直接双击无法正确运行. d:(回车/进入D盘) cd wamp\bin\apache\Apache2.4.4\bin(回车/进入 bin目录) ab(回车/) 如果看到下列一些帮助信息就说明成功启动ab     开始启动ab 压力测试 ab -n 5000 -c 20

Java中调试与测试常用方法

一.断言 1.概述 根据之前的条件来对后续的结果进行预判. 2.在Java中,断言默认是不开启的,需要手动进行开启,只需添加参数-ea(enable assert的意思) 开启步骤: 选中项目路径,鼠标右键进入到Properties 进入到如下目录,按图示进行设置 进入到java设置界面,按照如下图所示逐步设置,并保存 3.断言的使用格式 assert判断条件:或者assert判断条件:提示信息: 当项目调试完毕,直接手动将断言关闭即可.用作代码调试非常方便. 示例如下: public stat

Java中使用Jedis操作Redis

转载:http://www.cnblogs.com/liuling/p/2014-4-19-04.html 使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip

java中使用jedis操作redis(连接池方式)

1 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import redis.clients.jedis.Jedis; 12 13 public class TestRedis

【转载】Java中使用Jedis操作Redis

1 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 11 import redis.clients.jedis.Jedis; 12 13 public class TestRedis

Java中XML文件的读取与写入

表现:以  ".xml"为文件扩展名的文件: 存储:树形结构: 用途:存储以及传递信息:利用相同的XML文件将不同的系统联系起来: 在Java程序中如何获取XML文件的内容? 在Java程序中读取XML文件的过程也称为"解析XML文件": 解析的目的:获取节点名.节点值.属性名.属性值: 四种解析方式:DOM  SAX  DOM4J  JDOM java学习群669823128

PHP使用Apache中的ab测试网站的压力性能及mpm介绍

打开Apache安装的bin目录 shift+鼠标右键 复制粘贴以下代码->回车 ab -n 1000 -c 100 http://localhost/test.php 上例表示总共访问http://localhost/test.php这个脚本1000次,100并发(模拟100个用户同时访问). Server Software: Apache/2.4.4 #apache版本号 Server Hostname: localhost Server Port: 80 Document Path: /t

Java中IO流文件读取、写入和复制

//构造文件File类 File f=new File(fileName); //判断是否为目录 f.isDirectory(); //获取目录下的文件名 String[] fileName=f.list(); //获取目录下的文件File[] files=f.listFiles();  1.Java怎么读取文件 package com.yyb.file; import java.io.File; import java.io.FileInputStream; import java.io.In

PHP使用Apache中的ab测试网站的压力性能

打开Apache服务器的安装路径(我用的是 WampServer),在bin目录中有一个ab.exe的可执行程序,它就是要介绍的压力测试工具. 在Windows系统的命令行下,进入ab.exe程序所在目录,执行ab.exe程序.注意直接双击无法正确运行. d:(回车/进入D盘) cd wamp\bin\apache\Apache2.4.4\bin(回车/进入 bin目录) ab(回车/) 如果看到下列一些帮助信息就说明成功启动ab     开始启动ab 压力测试 ab -n 5000 -c 20