Package eu.mosaic_cloud.sunutilities

Source Code of eu.mosaic_cloud.sunutilities.TimeAndSizeKvTemplate

//###
//# #%L
//# mosaic-java-sun-utilities
//# %%
//# Copyright (C) 2010 - 2012 Seconda Università di Napoli, Università del Sannio
//#Authors: Giuseppe Aversano,Massimiliano Rak, Antonio Cuomo, Umberto Villano
//# %%
//# Licensed under the Apache License, Version 2.0 (the "License");
//# you may not use this file except in compliance with the License.
//# You may obtain a copy of the License at
//#
//#      http://www.apache.org/licenses/LICENSE-2.0
//#
//# Unless required by applicable law or agreed to in writing, software
//# distributed under the License is distributed on an "AS IS" BASIS,
//# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//# See the License for the specific language governing permissions and
//# limitations under the License.
//# #L%
//###
package eu.mosaic_cloud.sunutilities;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.Vector;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class ClientKvStore {
 
public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException {
   
    if (args.length != 2) {
      System.out.println("Error: you must enter 2 arguments!");
      System.exit(-1);
      }
   
    String NameTest=args[0];
    String Path=args[1];
   
    int[] numMsgs = null;
    int[] NumSet = null;
    int[] NumGet=null;
    int NITERS = 0;
    int DimValueStart;
    String Start = null;
    String Ready = null;
    String Post = null;
    String mhttpgw = null;
    String Result = null;
    String Stato = null;
    String PathResult=null;
 
 
    TimeAndSizeKvTemplate[] TimeAndSizeTemplates = readTimesFromFile(Path);
     for(TimeAndSizeKvTemplate TimeAndSizeTemplate: TimeAndSizeTemplates){
           String nextAction = TimeAndSizeTemplate.getAction();
           String nextValue = TimeAndSizeTemplate.getValue()
      
          
           switch (nextAction) {
         
          case "numMsgs": numMsgs=parseIntArray(nextValue);
         
          break;
         
          case "NumSet": NumSet=parseIntArray(nextValue);
         
          break;
         
          case "NumGet": NumGet=parseIntArray(nextValue);
         
          break;
         
          case "NITERS": NITERS=Integer.parseInt(nextValue);
         
          break;
         
          case "PathResult": PathResult=nextValue;
         
          break;
         
          case "Start": Start=nextValue;
         
          break;
         
          case "Ready": Ready=nextValue;

          break;

          case "Post": Post=nextValue;

          break;

          case "mhttpgw":mhttpgw=nextValue;

          break;

          case "Result": Result=nextValue;

          break;

          case "Stato": Stato=nextValue;

          break;

          
           }
          
     }
    
    
     String[] ValueSta  = Start.split(",");
    // String[] ValueSta = readStartFromFile(Start);
     long[] filesizeKB= new long[ValueSta.length];
    
     Gson gson = new GsonBuilder().disableHtmlEscaping().create();
    
   
     if(NumSet.length==NumGet.length)  
  {
      
   
    
  for (int dim=0;dim<NumSet.length;dim++)
   {
    
     for (int b=0;b<ValueSta.length;b++)
     {
       File file = new File(ValueSta[b]);
               filesizeKB[b] = file.length()/1024;
      
   
      JsonObjectfromString obj = gson.fromJson(new BufferedReader(
          new InputStreamReader(new FileInputStream(ValueSta[b]))),
          JsonObjectfromString.class);
     
     
     
      for (int numMsg : numMsgs) {
       
        for (int i = 0; i < NITERS; i++) {
         
          //System.out.println("SET:"+NumSet[dim]);
          //System.out.println("GET:"+NumGet[dim]);
          //System.out.println("DIMBLOCK:"+numMsg);
          //System.out.println("NITERS:"+i);
          postFile(Ready,mhttpgw);
          //modifica l'oggetto come ti pare
          obj.getParameters()[0] = "" + numMsg;
          obj.getParameters()[2] = "" +NumSet[dim];
          obj.getParameters()[4] = "" +NumGet[dim];
        //  System.out.println("SET_OBJECT:"+obj.getParameters()[2]);
        //  System.out.println("GET_OBJECT:"+obj.getParameters()[4]);
          waitStatus("\"Ready\"",Stato);
          postString(gson.toJson(obj),mhttpgw);
          waitStatus("\"Stopped\"",Stato);
                        }
                    }
       }
    }
 
      postFile(Post, mhttpgw);
      Thread.sleep(5000);
      DefaultHttpClient client = new DefaultHttpClient();
      HttpGet get = new HttpGet(Result);
      HttpResponse response = client.execute(get);
      InputStream stream = response.getEntity().getContent();
      BufferedReader br = new BufferedReader(new InputStreamReader(stream));
      String line = br.readLine();
      System.out.println("Plot result is: " + line);
     
      String lineNew = line.replace("The Benchmark Results in ms are:  ","");
     
      //System.out.println("lineNew:"+lineNew);
     
      String[] ValueTime = lineNew.split("  ");
     
     
     
      DimValueStart=ValueSta.length;
           
      Calendar cal = Calendar.getInstance();
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
      String Data = sdf.format(cal.getTime());
     
                   
      ScriviFileCVS(PathResult+""+NameTest+"_"+Data+".csv",ValueTime,DimValueStart,NITERS,numMsgs,filesizeKB,NumSet,NumGet);
     
     }
     else
     {
       System.out.println("ERROR: DIM SET IS NOT EQUALS DIM GET!!!!");
     }
    
    }

    public static void postFile(String fileName, String mhttpgw)
        throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(mhttpgw);
      MultipartEntity entity = new MultipartEntity();
      entity.addPart("file", new FileBody(new File(fileName)));
      post.setEntity(entity);
      HttpResponse response = client.execute(post);

    }

    public static void postString(String string, String mhttpgw)
        throws ClientProtocolException, IOException {
      HttpClient client = new DefaultHttpClient();
      HttpPost post = new HttpPost(mhttpgw);
      MultipartEntity entity = new MultipartEntity();
      entity.addPart("file", new StringBody(string));
      post.setEntity(entity);
      HttpResponse response = client.execute(post);

    }

    public static void waitStatus(String status, String Stato) throws IllegalStateException,
        IOException, InterruptedException {
      String line;
      do {
        Thread.sleep(1000);
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(Stato);
        HttpResponse response = client.execute(get);
        InputStream stream = response.getEntity().getContent();
        BufferedReader br = new BufferedReader(
            new InputStreamReader(stream));
        line = br.readLine();
        System.out.println("Status is " + line);
      } while (line.equalsIgnoreCase(status) == false);
    }
   
    public static void ScriviFile(String fileName, String Val){
      try{
      FileWriter w=new FileWriter(fileName);
      BufferedWriter b =new BufferedWriter (w);
        PrintWriter printout = new PrintWriter(b);
        printout.println(Val);
        printout.flush();
        }
      catch (Exception e) {
              System.out.println("ERRORE: "+e);
                }
     
    }
   
    public static void ScriviFileCVS(String fileName, String[] Val, int DimValueStart, int NITERS, int[] numMsgs,long[] filesizeKB,int[] NumSet, int[] NumGet ){
      try{
   
     
        PrintStream printout = new PrintStream( new FileOutputStream(fileName) );
       
        int DimnumMsgs=numMsgs.length;
       // System.out.println("NumMsg:"+DimnumMsgs);
        int DimData=Val.length;
        //System.out.println("NumBenchmark:"+DimData);
        int DimDimSet=NumSet.length;
        int DimDimGet=NumGet.length;
        //System.out.println("NumBlock:"+DimDimblocks);
        //System.out.println("Niters:"+NITERS);
        //System.out.println("NumStart:"+DimValueStart);
        int startVal=DimData-(DimValueStart*NITERS*DimnumMsgs*DimDimSet);
        //System.out.println("StartVal:"+startVal);
        int t=startVal;
       
        printout.println("Num_Set;Num_Get;Num_MSG;DIM_MSG (KB);TIMES (ms)");
   
       
        for(int z=0;z<DimDimSet;z++)
        {
      
        for(int m=0;m<DimValueStart;m++)
        {
         
          for (int n=0;n<DimnumMsgs;n++)
          {
           
             for (int l=0;l<NITERS;l++)
             {
               printout.println(NumSet[z]+";"+NumGet[z]+";"+numMsgs[n]+";"+filesizeKB[m]+";"+Val[t]);
             
              // System.out.println("VAL:"+Val[t]);
               t++;
              
              
              }
           
           
          }
         
        }
       }
        printout.flush();
        }
      catch (Exception e) {
              System.out.println("ERRORE: "+e);
                }
     
    }
    

private static TimeAndSizeKvTemplate[]  readTimesFromFile(String fileName) throws IOException {
  Vector<TimeAndSizeKvTemplate> TimeAndSizeTemplates = new Vector<TimeAndSizeKvTemplate>();
  BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
  String line = br.readLine();
  while(line !=null){
    StringTokenizer st = new StringTokenizer(line, "=");
    String action = st.nextToken();
    String value = st.nextToken();
    TimeAndSizeTemplates.add(new TimeAndSizeKvTemplate(action, value));
    line = br.readLine();
  }
  TimeAndSizeKvTemplate[] TimeAndSizeTemplateArray = new TimeAndSizeKvTemplate[TimeAndSizeTemplates.size()];
  TimeAndSizeTemplates.toArray(TimeAndSizeTemplateArray);
  br.close();
  return TimeAndSizeTemplateArray;
}


public static int[] parseIntArray (String str)
{
    String[] vstr = str.split (" ");
    int[] vint = new int[vstr.length];

    for (int i = 0; i < vstr.length; i++){
        vint[i] = Integer.parseInt (vstr[i]);
    }
    return vint;
}
}
class TimeAndSizeKvTemplate{

private String action;
private String value;
public TimeAndSizeKvTemplate(String action, String value) {
  this.action = action;
  this.value = value;
}
public String getAction() {
  return action;
}
public String getValue() {
  return value;
}

}
TOP

Related Classes of eu.mosaic_cloud.sunutilities.TimeAndSizeKvTemplate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.