Skip to content
Snippets Groups Projects
Commit 447c182e authored by BBQ's avatar BBQ
Browse files

update benchmark

parent bfdbbe1f
No related branches found
No related tags found
1 merge request!3Integration of Learner
...@@ -4,9 +4,9 @@ import com.opencsv.CSVReader; ...@@ -4,9 +4,9 @@ import com.opencsv.CSVReader;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.fluent.Request; import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType; import org.apache.http.entity.ContentType;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.util.Arrays; import java.util.Arrays;
...@@ -33,9 +33,8 @@ public class Benchmark { ...@@ -33,9 +33,8 @@ public class Benchmark {
* Col 1: Activity * Col 1: Activity
* Col 2: watch brightness range "bright, medium, dimmer, dark"*/ * Col 2: watch brightness range "bright, medium, dimmer, dark"*/
private static int TIME_PERIOD = 5000; //5 second update new value private String a_csv_file_path;
private String csv_file_path; private String p_csv_file_path;
private String csv_typ;
private static final Logger logger = LogManager.getLogger(Benchmark.class); private static final Logger logger = LogManager.getLogger(Benchmark.class);
private static final String ERASER_ITEM_URI = "http://localhost:4567/model/items/"; private static final String ERASER_ITEM_URI = "http://localhost:4567/model/items/";
//TODO ITEM_NAME HAS TO BE DISCUSSED //TODO ITEM_NAME HAS TO BE DISCUSSED
...@@ -46,30 +45,61 @@ public class Benchmark { ...@@ -46,30 +45,61 @@ public class Benchmark {
private static final String[] PREFERENCE_ITEM_NAME = { private static final String[] PREFERENCE_ITEM_NAME = {
"w_brightness" "w_brightness"
}; };
private static boolean flag2 = true;
//csv_type is activity or preference //csv_type is activity or preference
Benchmark(String csv_file_path, String csv_type) { Benchmark(String a_csv_file_path, String p_csv_file_path) {
this.csv_file_path = csv_file_path; this.a_csv_file_path = a_csv_file_path;
this.csv_typ = csv_type; this.p_csv_file_path = p_csv_file_path;
updater();
} }
private void updater(){
File file; void start(){
FileReader file_reader; String PREFERENCE_URL = "http://localhost:4567/model/items/iris1_item/state";
CSVReader csv_reader; String ACTIVITY_URL = "http://localhost:4567/activity/current";
String[] next_record; int TIME_PERIOD = 5000;
file=new File(csv_file_path); File a_file;
File p_file;
FileReader a_file_reader;
FileReader p_file_reader;
CSVReader a_csv_reader;
CSVReader p_csv_reader;
String[] a_next_record;
String[] p_next_record;
boolean flag1;
a_file=new File(a_csv_file_path);
p_file= new File(p_csv_file_path);
try { try {
file_reader =new FileReader(file); a_file_reader =new FileReader(a_file);
csv_reader = new CSVReader(file_reader); p_file_reader=new FileReader(p_file);
while ((next_record = csv_reader.readNext()) != null) { a_csv_reader = new CSVReader(a_file_reader);
Thread.sleep(TIME_PERIOD); p_csv_reader = new CSVReader(p_file_reader);
if (csv_typ =="activity"){ while ((((a_next_record = a_csv_reader.readNext())!= null) && flag2)){
String[] values = Arrays.copyOf(next_record,12); try{Thread.sleep(TIME_PERIOD);}catch (InterruptedException e){e.printStackTrace();}
setNewValue(values, ACTIVITY_ITEM_NAME); String[] values = Arrays.copyOf(a_next_record,12);
setNewValue(values, ACTIVITY_ITEM_NAME,"activity");
HttpResponse response= Request.Get(ACTIVITY_URL).execute().returnResponse();
String status = response.getStatusLine().toString();
if(status.contains("200")){
flag1 = true;
logger.info("activity should be (read direct from CSV): " + a_next_record[12]);
logger.info(EntityUtils.toString(response.getEntity()));
logger.info("get activity from web server: response 200 ok");
}else{ }else{
String[] values = Arrays.copyOf(next_record,2); flag1 = false;
setNewValue(values, PREFERENCE_ITEM_NAME); flag2 = false;
logger.info("can not get the activity from the web server");
}
while((((p_next_record = p_csv_reader.readNext()) != null) && flag1)) {
try{Thread.sleep(TIME_PERIOD);}catch (InterruptedException e){e.printStackTrace();}
String[] values1 = Arrays.copyOf(p_next_record,2);
setNewValue(values1, PREFERENCE_ITEM_NAME,"preference");
HttpResponse response1= Request.Get(PREFERENCE_URL).execute().returnResponse();
String status1=response1.getStatusLine().toString();
if(status1.contains("200")){
flag2 = true;
logger.info("get the iris1_item preference from web server: response 200 ok, value is: "+EntityUtils.toString(response1.getEntity()));
}else {flag2 = false;
logger.info("can not get the iris1_item from the web server");}
break;
} }
} }
} }
...@@ -77,18 +107,22 @@ public class Benchmark { ...@@ -77,18 +107,22 @@ public class Benchmark {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void setNewValue(String[] values, String[] name){ private void setNewValue(String[] values, String[] name, String file_typ){
if(this.csv_typ.equals("activity")) if(file_typ.equals("activity"))
{ {
int i = 0; int i = 0;
for(String value : values){ for(String value : values){
String uri = ERASER_ITEM_URI + name[i] + "/state"; String uri = ERASER_ITEM_URI + name[i] + "/state";
logger.info("activtiy");
logger.info(value);
try { try {
HttpResponse httpResponse = Request.Put(uri) HttpResponse httpResponse = Request.Put(uri)
.bodyString(value, ContentType.TEXT_PLAIN) .bodyString(value, ContentType.TEXT_PLAIN)
.execute().returnResponse(); .execute().returnResponse();
String status=httpResponse.getStatusLine().toString();
if(status.contains("200")){
logger.info("put activity input name: "+name[i]+", value: "+value+"to web server: response 200 ok");
}else{
logger.info("can not put activity inputs to rest server");
}
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
...@@ -96,12 +130,14 @@ public class Benchmark { ...@@ -96,12 +130,14 @@ public class Benchmark {
} }
}else{ }else{
String uri= ERASER_ITEM_URI + "w_brightness" +"/state"; String uri= ERASER_ITEM_URI + "w_brightness" +"/state";
logger.info("preference");
logger.info(values[1]);
try { try {
HttpResponse httpResponse = Request.Put(uri) HttpResponse httpResponse = Request.Put(uri)
.bodyString(values[1], ContentType.TEXT_PLAIN) .bodyString(values[1], ContentType.TEXT_PLAIN)
.execute().returnResponse(); .execute().returnResponse();
String put_response=httpResponse.getStatusLine().toString();
if (put_response.contains("200")){logger.info("put w_brightness to web server: response 200 ok");}else{
logger.info("can not put w_brightness to rest server");
}
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -4,27 +4,9 @@ public class Main { ...@@ -4,27 +4,9 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
String A_CSV_FILE_PATH = "../datasets/backup/activity_data.csv"; String A_CSV_FILE_PATH = "../datasets/backup/activity_data.csv";
String A_CSV_TYP = "activity";
String P_CSV_FILE_PATH = "../datasets/backup/preference_data.csv"; String P_CSV_FILE_PATH = "../datasets/backup/preference_data.csv";
String P_CSV_TYP = "preference"; Benchmark benchmark=new Benchmark(A_CSV_FILE_PATH,P_CSV_FILE_PATH);
benchmark.start();
Thread thread1=new Thread(new ThreadA(A_CSV_FILE_PATH,A_CSV_TYP));
Thread thread2=new Thread(new ThreadA(P_CSV_FILE_PATH,P_CSV_TYP));
thread1.start();
thread2.start();
}
public static class ThreadA implements Runnable{
String csv_file_path;
String csv_typ;
ThreadA(String csv_file_path,String csv_typ){
this.csv_file_path=csv_file_path;
this.csv_typ=csv_typ;
}
@Override
public void run() {
new Benchmark(this.csv_file_path, this.csv_typ);
}
} }
} }
...@@ -3,6 +3,7 @@ rootProject.name = 'eraser' ...@@ -3,6 +3,7 @@ rootProject.name = 'eraser'
include ':eraser-base' include ':eraser-base'
include 'openhab-mock' include 'openhab-mock'
include 'integration' include 'integration'
include ':benchmark'
include ':commons.color' include ':commons.color'
include ':skywriter-hue-integration' include ':skywriter-hue-integration'
include ':org.openhab.action.machinelearn' include ':org.openhab.action.machinelearn'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment