Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    12

    Test Automatico con con scrittura su file da applicazione multi-thread

    Ciao ho bisogno di un aiuto.
    Ho creato un' applicazione multi-thread, dove ogni thread invia un certo numero, di 3 tipologie diverse, di messaggi ad un application server.
    Con l'obbiettivo di misurare le prestazioni dell' AS ho inserito nel metodo run del thread un eccezione che mi scrive su un file tutte le volte che l'AS non riesce a rispondere alla mia richiesta.
    Ora volevo creare un test automatico che mi permettesse con un lancio solo dell'applicazione di effettuare diverse misure al variare del numero di messaggi, del numero dei thread e del tempo di sleep di un thread dopo che ha effettuato.

    vi faccio vedere il codice:

    Questo e' il mio main:

    codice:
        public static void main(String[] args) throws InterruptedException {
            try {
                File outputFile = new File("TestJboss2.txt");
                outputFile.delete();
                outputFile.createNewFile();
                FileWriter fw = new FileWriter(outputFile.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write("TEST\n"); /*
                 * 1 test * Thread Number = 1 * Sleep = 0; * Message = 1 X 10;
                 */ int[] ss = new int[]{50, 50, 150};
                for (int i = 0; i < 10; i++) {
                    new Thread(new MioThread(ss, 1, 0, bw)).start();
                }
                Thread.currentThread().sleep(10000);
                bw.close();
                fw.close();
            } catch (IOException e) {
            }
        }


    l' array ' di 3 elementi (3 tipologie di messagi) e il valore corrispone al numero di messaggi.
    questa e' la classe che implementa la "run"

    codice:
    public class MioThread implements Runnable {
    
        int[] array;
        private int sleep;
        int nOfThreads;
        BufferedWriter bw;
    
        public MioThread(int[] array, int nOfTest, int sleep, BufferedWriter bw) {
            this.array = array;
            this.nOfThreads = nOfTest;
            this.sleep = sleep;
            this.bw = bw;
        }
    
        @Override
        public void run() { //Set The Filters
            GetPersonFilter filterP = GetPersonFilter.newBuilder().setName("rava").build();
            GetAnimalFilter filterA = GetAnimalFilter.newBuilder().setName("bubi").build();
            GetGodFilter filterG = GetGodFilter.newBuilder().setName("zeus").build();
            for (int i = 0; i < this.array[0]; i++) {
                try {
                    people = Exec.getPerson(filterP, i + 1);
                } catch (Exception exc) {
                    String s = new String("Sleep Time: " + sleep + " Number of Treads: " + nOfThreads + " Number Of Message: " + array[0] + "\n");
                    synchronized (bw) {
                        try {
                            bw.write(s);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException iExc) {
                    iExc.printStackTrace();
                    return;
                }
            }
            for (int j = 0; j < array[1]; j++) {
                try {
                    animal = Exec.getAnimal(filterA, j + 1);
                } catch (Exception e) {
                    String s = new String("Sleep Time: " + sleep + " Number of Threads: " + nOfThreads + " Number Of Message: " + array[1] + "\n");
                    synchronized (bw) {
                        try {
                            bw.write(s);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException iExc) {
                    iExc.printStackTrace();
                    return;
                }
            }
            for (int i = 0; i < array[2]; i++) {
                try {
                    olympo = Exec.getGod(filterG, i + 1);
                } catch (Exception e) {
                    String s = new String("Sleep Time: " + sleep + " Number of Threads: " + nOfThreads + " Number Of Message: " + array[2] + "\n");
                    synchronized (bw) {
                        try {
                            bw.write(s);
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
                try {
                    Thread.sleep(sleep);
                } catch (InterruptedException iExc) {
                    iExc.printStackTrace();
                    return;
                }
            }
        }
    Volevo fare in modo di poter lanciare piu cicli "for" nel main ognuno con parametri (numero di thread, messaggi, e sleep) in modo che, ad esempio, il secondo for, partisse una volta che l'ultimo thread del primo "for" avesse finito la sua run.
    Avevo pensato di utilizzare la "Join", ma essendo un neofita dei thread e di Java, mi sono un po incagliato.

    Avete qualche suggerimento?

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Posta il codice ben formattato, così su 1 riga è illeggibile.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #3
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,325
    Originariamente inviato da andbin
    Posta il codice ben formattato, così su 1 riga è illeggibile.
    A tal proposito, dò il solito suggerimenti all'utente Matte_84: non usare il tasto CODE (che ha questo fastidioso difetto: cioè è da usarsi solo con UNA riga di codice), ma scrivi i tag a mano, in questo modo:

    [code]
    poi incolla il codice indentato
    [/code]

    Ho modificato io il tuo post.

    PS: MAI ammazzare le eccezioni... ovunque ci sia un catch va fatto il printStackTrace() dell'eccezione, altrimenti se qualcosa va in errore non lo saprai mai e non ne capirai mai il motivo.

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    12
    Hai ragione.. e' la prima volta che scrivo, pensavo si formattasse in automatico...provo a fare meglio:

    codice:
    public class Test {
    
    	/**
    	 * @param args
    	 * @throws InterruptedException 
    	 */
    	public static void main(String[] args) throws InterruptedException {
    
    		try {
    			File outputFile = new File("TestJboss2.txt");
    			outputFile.delete();
    			outputFile.createNewFile();
    			
    
    			FileWriter fw = new FileWriter(outputFile.getAbsoluteFile());
    			BufferedWriter bw = new BufferedWriter(fw);
    			bw.write("TEST\n");
    
    			/* 1 test 
    			 * Thread Number = 1 
    			 * Sleep = 0; 
    			 * Message = 1 X 10;*/
    			int [] ss = new int[]{50, 50, 150};
    			for (int i = 0; i < 10; i++) {			
    				new Thread(new MioThread(ss, 1, 0, bw)).start();
    		}
    
    	    Thread.currentThread().sleep(10000);
    	    
    		bw.close();
    		fw.close();
    		} catch(IOException e){}
    		
    		
    	}
    		
    }
    
    public class MioThread implements Runnable {
    
    	int [] array;
    	private int sleep;
    	int nOfThreads;
    	BufferedWriter bw;
    	
    	public MioThread(int[] array, int nOfTest, int sleep, BufferedWriter bw){
    		this.array = array;
    		this.nOfThreads = nOfTest;
    		this.sleep = sleep;
    		this.bw = bw;
    	}
    	@Override
    	public void run() {
    		//Set The Filters
    		GetPersonFilter filterP = GetPersonFilter.newBuilder().setName("fava").build();
    		GetAnimalFilter filterA = GetAnimalFilter.newBuilder().setName("bubi").build();
    		GetGodFilter filterG = GetGodFilter.newBuilder().setName("zeus").build();
    
    		ArrayList<Person> people = null;
    		ArrayList<Animal> animal = null;
    		ArrayList<God> olympo = null;
    
    			for(int i = 0 ; i < this.array[0]; i++ ) {
    				try{
    					
    					people = Exec.getPerson(filterP, i + 1);
    				}catch(Exception exc){
    					
    				String s = new String("Sleep Time: " + sleep + 
    							" Number of Treads: " + nOfThreads 
    							+ " Number Of Message: " + 
    							array[0] + "\n");
    				
    				synchronized (bw) {
    					try {
    						bw.write(s);
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}	
    				}
    				try{
    					Thread.sleep(sleep);
    				}catch(InterruptedException iExc){
    					iExc.printStackTrace();
    					return;
    				}					
    			}
    			for (int j = 0; j < array[1]; j ++) {
    				try {
    					animal = Exec.getAnimal(filterA, j + 1);
    				} catch (Exception e) {
    					
    					String s = new String("Sleep Time: " + sleep + 
    							" Number of Threads: " + nOfThreads 
    							+ " Number Of Message: " + 
    							array [1] + "\n");
    					
    					synchronized (bw) {
    						try {
    							bw.write(s);
    						} catch (IOException e1) {
    							e1.printStackTrace();
    						}
    					}						
    				}
    				try{
    					Thread.sleep(sleep);
    				}catch(InterruptedException iExc){
    					iExc.printStackTrace();
    					return;
    				}						
    			}
    
    			for (int i = 0 ; i < array[2]; i ++) {
    				try {
    					olympo = Exec.getGod(filterG, i + 1);
    				} catch (Exception e) {
    					
    					String s = new String("Sleep Time: " + sleep + 
    							" Number of Threads: " + nOfThreads 
    							+ " Number Of Message: " + 
    							array[2] + "\n");
    					
    					synchronized (bw) {
    						try {
    							bw.write(s);
    						} catch (IOException e1) {
    							
    							e1.printStackTrace();
    						}
    					}	
    				}
    				try{
    					Thread.sleep(sleep);
    				}catch(InterruptedException iExc){
    					iExc.printStackTrace();
    					return;
    				}						
    		}
    	}
    }

  5. #5
    Moderatore di Programmazione L'avatar di LeleFT
    Registrato dal
    Jun 2003
    Messaggi
    17,325

    Moderazione

    Non è sufficiente incollare il codice, bisogna anche scrivere i tag a mano, come ho fatto io nell'esempio, che ti riporto nuovamente evidenziando i tag da scrivere a mano:

    [code]
    codice
    [/code]

    Ciao.
    "Perchè spendere anche solo 5 dollari per un S.O., quando posso averne uno gratis e spendere quei 5 dollari per 5 bottiglie di birra?" [Jon "maddog" Hall]
    Fatti non foste a viver come bruti, ma per seguir virtute e canoscenza

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    12
    Ti ringrazio.
    Non avevo visto il tuo messaggio.
    Grazie mille.

  7. #7
    Utente di HTML.it
    Registrato dal
    Jan 2012
    Messaggi
    12
    Ragionandoci un po' su, mi e' venuto in mente che mi servirebbe un meccanismo di questo genere:

    Il ciclo "for" mi crea 10 thread che sparano ognuno nove messagi in totale, mi servirebbe un array di flag che diventano "1" una volta il metodo "run" di uno dei 10 finisce.
    In questo modo quando tutti i thread hanno eseguito il loro compito l'array di flag e' composto da solo "1" e potrei cosi' istanziare un nuovo "for" che lancia ad esempio 20 thread che sparano 15 messaggi l'uno e cosi' via.

    Un ciclo "for" per ognuno dei miei test, che deve essere lanciato una volta che i thread del ciclo for precedente hanno finito il loro lavoro.

    Non so se mi sono spiegato.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.