2. Set the JDK path in the Run-time Settings. Because LoadRunner 11 does not support jdk1.8, this test copies a copy of the lower version of JDK1.6, so choose the Use Specified JDK and set the path of JDK1.6.
3. Using Java to operate Redis requires jedis-2.1.0.jar. Download address: http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip , put it in the include directory and Run-time Configure Classpath in Settings
4. Write Redis test code in Java Vuser script:
5. Run the script. You can see the successful execution of the data operation test through the Replay Log of LoadRunner/* * LoadRunner Java script. (Build: _build_number_) * * Script Description: * */ import lrapi.lr; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import redis.clients.jedis.Jedis; public class Actions { private Jedis jedis; public void setup() { //Connect to redis server, 172.16.1.30:6379 jedis = new Jedis("172.16.1.30", 6379); //Permission authentication //jedis.auth("admin"); } /** * redis Store string */ public void testString() { jedis.del("name","age","qq");//Clear the data first, then add the data for testing //-----Add data---------- jedis.set("name","test");//Put value -- > test into key -- > name System.out.println(jedis.get("name"));//Execution result: xinxin jedis.append("name", " is my lover"); //Splicing System.out.println(jedis.get("name")); jedis.del("name"); //Delete a key System.out.println(jedis.get("name")); //Set multiple key value pairs jedis.mset("name","smooth00","age","36","qq","1XXXXXXXX"); jedis.incr("age"); //Add 1 System.out.println(jedis.get("name") + "-" + jedis.get("age") + "-" + jedis.get("qq")); } /** * redis Operation Map */ public void testMap() { jedis.del("user");//Clear the data first, then add the data for testing //-----Add data---------- Map<String, String> map = new HashMap<String, String>(); map.put("name", "smooth-z"); map.put("age", "35"); map.put("qq", "123456"); jedis.hmset("user",map); //Take the name from the user and execute the result: [minxr] -- > note that the result is a generic List //The first parameter is the key stored in the map object in redis, followed by the key of the object placed in the map, followed by multiple keys, which are variable parameters List<String> rsmap = jedis.hmget("user", "name", "age", "qq"); System.out.println(rsmap); //Delete a key value in the map jedis.hdel("user","age"); System.out.println(jedis.hmget("user", "age")); //Because it is deleted, null is returned System.out.println(jedis.hlen("user")); //Returns the number of values stored in the key with key as user 2 System.out.println(jedis.exists("user"));//Whether there is a record with key as user returns true System.out.println(jedis.hkeys("user"));//Return all key s in the map object System.out.println(jedis.hvals("user"));//Returns all value s in the map object Iterator<String> iter=jedis.hkeys("user").iterator(); while (iter.hasNext()){ String key = iter.next(); System.out.println(key+":"+jedis.hmget("user",key)); } } /** * jedis Operation List */ public void testList() throws InterruptedException { //jedis sorting //Note that rpush and lpush here are List operations. It's a two-way List (but in terms of performance) jedis.del("a");//Clear the data first, then add the data for testing jedis.rpush("a", "1"); jedis.lpush("a","6"); jedis.lpush("a","3"); jedis.lpush("a","9"); System.out.println(jedis.lrange("a",0,-1));// [9, 3, 6, 1] System.out.println(jedis.sort("a")); //[1, 3, 6, 9] / / enter the sorting result System.out.println(jedis.lrange("a",0,-1)); } /** * jedis Operation Set */ public void testSet(){ jedis.del("user");//Clear data first //Add to jedis.sadd("user","smooth-z"); jedis.sadd("user","zheng"); jedis.sadd("user","liu"); jedis.sadd("user","nono"); jedis.sadd("user","who"); //Remove noname jedis.srem("user","who"); System.out.println(jedis.smembers("user"));//Get all added value s System.out.println(jedis.sismember("user", "who"));//Determine if who is an element of the user set System.out.println(jedis.srandmember("user")); System.out.println(jedis.scard("user"));//Returns the number of elements in the collection } public int init() throws Throwable { setup(); return 0; }//end of init public int action() throws Throwable { lr.start_transaction("testString"); testString(); lr.end_transaction("testString", lr.AUTO); lr.start_transaction("testMap"); testMap(); lr.end_transaction("testMap", lr.AUTO); lr.start_transaction("testList"); testList(); lr.end_transaction("testList", lr.AUTO); lr.start_transaction("testSet"); testSet(); lr.end_transaction("testSet", lr.AUTO); return 0; }//end of action public int end() throws Throwable { return 0; }//end of end }
6. Connect Redis through the tool, and you can also see the new key value
7. If you need to use Redis connection pool, you need to refer to commons-pool-1.5.4.jar. Download address: http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip
Provide the connection pool operation code for reference:
package com.test; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public final class RedisUtil { //Redis server IP private static String ADDR = "172.16.1.30"; //Redis port number private static int PORT = 6379; //Access password private static String AUTH = "admin"; //The maximum number of available connection instances. The default value is 8; //If the value is - 1, it means unlimited; if the pool has maxActive jedis instances assigned, the status of the pool is exhausted. private static int MAX_ACTIVE = 1024; //Controls the maximum number of jedis instances with idle status in a pool. The default value is 8. private static int MAX_IDLE = 200; //The maximum time to wait for an available connection, in milliseconds. The default value is - 1, which means never timeout. If the waiting time is exceeded, a JedisConnectionException will be thrown directly; private static int MAX_WAIT = 10000; private static int TIMEOUT = 10000; //When a jedis instance is in borrow, whether to perform validate operation in advance; if it is true, all the jedis instances obtained are available; private static boolean TEST_ON_BORROW = true; private static JedisPool jedisPool = null; /** * Initialize Redis connection pool */ static { try { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(MAX_ACTIVE); config.setMaxIdle(MAX_IDLE); config.setMaxWait(MAX_WAIT); config.setTestOnBorrow(TEST_ON_BORROW); jedisPool = new JedisPool(config, ADDR, PORT, TIMEOUT, AUTH); } catch (Exception e) { e.printStackTrace(); } } /** * Get Jedis instance * @return */ public synchronized static Jedis getJedis() { try { if (jedisPool != null) { Jedis resource = jedisPool.getResource(); return resource; } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } /** * Release jedis resources * @param jedis */ public static void returnResource(final Jedis jedis) { if (jedis != null) { jedisPool.returnResource(jedis); } } }
8. Someone asked if there is Redis connection code written in C (after all, Loadrunner's default scripting language is C, which some people are used to). The answer is yes, because Redis also provides Java, C/C + +, C ා, PHP, JavaScript, Perl, Object-C, Python, Ruby, Erlang and other clients. Here is a simple sample code (the premise is to download a C from GitHub API, and reference its related library files, download address: https://github.com/redis/hiredis):
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <stdarg.h> #include <string.h> #include <assert.h> #include <hiredis/hiredis.h> void doTest() { //redis's default listening port is 6387, which can be modified in the configuration file redisContext* c = redisConnect("127.0.0.1", 6379); if ( c->err) { redisFree(c); printf("Connect to redisServer faile\n"); return ; } printf("Connect to redisServer Success\n"); const char* command1 = "set stest1 value1"; redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r) { printf("Execut command1 failure\n"); redisFree(c); return; } if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0)) { printf("Failed to execute command[%s]\n",command1); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command1); const char* command2 = "strlen stest1"; r = (redisReply*)redisCommand(c, command2); if ( r->type != REDIS_REPLY_INTEGER) { printf("Failed to execute command[%s]\n",command2); freeReplyObject(r); redisFree(c); return; } int length = r->integer; freeReplyObject(r); printf("The length of 'stest1' is %d.\n", length); printf("Succeed to execute command[%s]\n", command2); const char* command3 = "get stest1"; r = (redisReply*)redisCommand(c, command3); if ( r->type != REDIS_REPLY_STRING) { printf("Failed to execute command[%s]\n",command3); freeReplyObject(r); redisFree(c); return; } printf("The value of 'stest1' is %s\n", r->str); freeReplyObject(r); printf("Succeed to execute command[%s]\n", command3); const char* command4 = "get stest2"; r = (redisReply*)redisCommand(c, command4); if ( r->type != REDIS_REPLY_NIL) { printf("Failed to execute command[%s]\n",command4); freeReplyObject(r); redisFree(c); return; } freeReplyObject(r); printf("Succeed to execute command[%s]\n", command4); redisFree(c); } int main() { doTest(); return 0; }