Skip to content

Commit

Permalink
#2 Infinispan test: test performance
Browse files Browse the repository at this point in the history
  • Loading branch information
fansy1990 committed Oct 29, 2019
1 parent 46c6609 commit e87d885
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@
<version>${infinispan.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.share.memory.controller;

import com.share.memory.infinispan.CacheType;
import com.share.memory.infinispan.InfinispanStarter;
import com.share.memory.infinispan.model.Person;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
* author : fanzhe
* email : [email protected]
* date : 2019/10/12 AM9:54.
*/
@RestController
@RequestMapping("/api")
public class InfinispanCheckSetAndRevoke {
@RequestMapping(value = "/check",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Object getPerson(@RequestParam(name = "cacheType",required = false) String cacheType,
@RequestParam(name = "key")String key){
return InfinispanStarter.check(key);
}

@RequestMapping(value = "/set",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public String setPerson(
@RequestParam(name = "key")String key,
@RequestParam(name = "name")String name,
@RequestParam(name = "age")int age
){
InfinispanStarter.put(key,new Person(name,age));
return key;
}

@RequestMapping(value = "/revoke",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Person revokePerson(
@RequestParam(name = "key")String key

){
return InfinispanStarter.revoke(key);
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/share/memory/httpclient/RemoteClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.share.memory.httpclient;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* author : fanzhe
* email : [email protected]
* date : 2019/10/28 PM11:15.
*/
public class RemoteClient {
static CloseableHttpClient httpclient = HttpClients.createDefault();

private static final String GET = "http://192.168.128.149:8080/share_memory/api/check";
private static final String SET = "http://192.168.128.148:8080/share_memory/api/set";
private static final String REVOKE = "http://192.168.128.148:8080/share_memory/api/revoke";

public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
Logger root = Logger.getLogger("");
root.setLevel(Level.INFO);
String key ;
for(int i =0 ;i< 1000;i++){
key = set();
System.out.println("key: "+ key);
check(key);
revoke(key);
}

}

public static String set() throws URISyntaxException, IOException {
long t1 = System.currentTimeMillis();
URIBuilder uriBuilder = new URIBuilder(SET);
String key = UUID.randomUUID().toString();
uriBuilder.addParameter("key", key);
uriBuilder.addParameter("name",key+"_1");
uriBuilder.addParameter("age","1000");
HttpGet get = new HttpGet(uriBuilder.build());
String result = EntityUtils.toString(httpclient.execute(get).getEntity());
System.out.println("get : "+ (System.currentTimeMillis()-t1)/1000.0 +" seconds!");
return result;
}

public static void check(String key) throws URISyntaxException, IOException, InterruptedException {
long t1 = System.currentTimeMillis();
URIBuilder uriBuilder = new URIBuilder(GET);

uriBuilder.addParameter("key", key);

HttpGet get = new HttpGet(uriBuilder.build());
String flag = EntityUtils.toString(httpclient.execute(get).getEntity());
while (!"true".equals(flag)){
Thread.sleep(200);
flag = EntityUtils.toString(httpclient.execute(get).getEntity());
}
System.out.println("check : "+ (System.currentTimeMillis()-t1)/1000.0 +" seconds!");
return ;
}
public static void revoke(String key) throws URISyntaxException, IOException, InterruptedException {
long t1 = System.currentTimeMillis();
URIBuilder uriBuilder = new URIBuilder(REVOKE);

uriBuilder.addParameter("key", key);

HttpGet get = new HttpGet(uriBuilder.build());

EntityUtils.toString(httpclient.execute(get).getEntity());

System.out.println("revoke : "+ (System.currentTimeMillis()-t1)/1000.0 +" seconds!");
return ;
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/share/memory/infinispan/InfinispanStarter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ public class InfinispanStarter implements Callable<Boolean>{
private static Cache<Person, Cat> personCatCache;


public static void put(String key, Person p){
put(CacheType.StringPerson,key,p,180);
}

public static boolean check(String key){
return (boolean)check(CacheType.StringPerson,key);
}
public static Person revoke(String key){
return personCache.remove(key);
}

public static void put(CacheType cacheType, Object key, Object value, long expireSeconds){
switch (cacheType){
case StringPerson:
Expand Down Expand Up @@ -63,13 +74,25 @@ public static Object get(CacheType cacheType, Object key){
return null;
}


public static Object check(CacheType cacheType, Object key){
switch (cacheType){
case PersonCat:
return personCatCache.containsKey(key);
case StringPerson:
return personCache.containsKey(key);
}
return null;
}

private void initial(){
if(cacheManager == null){
LOG.error("DefaultCacheManager is null");
System.exit(1);
}
personCache = cacheManager.getCache(StringPerson.name());
personCatCache = cacheManager.getCache(PersonCat.name());
LOG.info("CacheManager initialed!");
}


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/share/memory/infinispan/model/Cat.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.share.memory.infinispan.model;

import java.io.Serializable;

/**
* author : fanzhe
* email : [email protected]
* date : 2019/10/22 AM8:33.
*/
public class Cat {
public class Cat implements Serializable{
private String name;
private int age;

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/share/memory/infinispan/model/Person.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.share.memory.infinispan.model;

import java.io.Serializable;
import java.util.Arrays;

/**
* author : fanzhe
* email : [email protected]
* date : 2019/10/22 AM8:33.
*/
public class Person {
public class Person implements Serializable{
public Person(String name, int age){
this.name = name;
setAge(age);
}
private String name;
private int age;

private double[] arr= new double[10000];

public String getName() {
return name;
}
Expand All @@ -22,6 +31,7 @@ public int getAge() {
}

public void setAge(int age) {
Arrays.fill(arr,1000.0);
this.age = age;
}
}
11 changes: 11 additions & 0 deletions src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration>
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
      <encoder>
         <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
      </encoder>
   </appender>

   <root level="INFO">
      <appender-ref ref="STDOUT" />
   </root>
</configuration>

0 comments on commit e87d885

Please sign in to comment.