Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Last active April 6, 2016 13:54
Show Gist options
  • Select an option

  • Save Viacheslav77/c356beed04791c61c4912a850be5f795 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/c356beed04791c61c4912a850be5f795 to your computer and use it in GitHub Desktop.
package ServersTestStress;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestStress extends Thread{
private static int counter = 0;
private String path;
public TestStress(String path){
this.path=path;
}
public static int getCoun(){
return counter;
}
public void run(){
while (!isInterrupted()){
try {
if (getHTML(path)==0)
interrupt();
} catch (Exception ex){
System.err.println(ex);
}
}
}
//Метод, который возвращает размер запарсенной HTML в виде интовой переменной, по которой можно судить "отзывается" ли сервер
private static int getHTML(String urlStr){
StringBuilder sb = new StringBuilder();
URL u = null;
try {
u = new URL("http://"+urlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection uc = null;
try {
uc = (HttpURLConnection) u.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(uc.getResponseCode());
try { if(uc.getResponseCode() == 200 || uc.getResponseCode() == 301)
try (BufferedReader in = new BufferedReader( new InputStreamReader(uc.getInputStream()))){
String inputLine;
while ( ( inputLine = in.readLine()) != null)
sb.append(inputLine+"\n");
++counter;
}
} catch (Exception ex) {
return 0;
}
System.out.println("couter = " + counter + " the length of the page = " + sb.length());
return sb.length();
}
}
package ServersTestStress;
import java.util.ArrayList;
public class ServiceFix {
//Метод запускает заданное колличество потоков для тестирования сайта
public static void begin (String path, int nubrOfThread){
System.out.print("----------------------------------------\nMulti-threaded solution." + " Start Threads :\nBegin\n");
ArrayList<TestStress> listThreat=new ArrayList<>();
for(int i=0; i < nubrOfThread; i++){
listThreat.add(new TestStress(path));
listThreat.get(i).start();
}
for (TestStress t: listThreat){
System.out.print(".");
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("End\n----------------------------------------\nServer stress level = " + TestStress.getCoun());
}
}
----------------------------------------
Multi-threaded solution. Start Threads :
Begin
couter = 1 the length of the page = 61210
couter = 2 the length of the page = 61210
couter = 3 the length of the page = 61213
couter = 4 the length of the page = 61213
couter = 5 the length of the page = 61200
couter = 6 the length of the page = 61210
couter = 7 the length of the page = 61168
couter = 8 the length of the page = 61213
couter = 9 the length of the page = 61234
couter = 10 the length of the page = 61234
couter = 11 the length of the page = 61240
couter = 12 the length of the page = 61212
couter = 13 the length of the page = 61211
couter = 14 the length of the page = 61217
couter = 15 the length of the page = 61167
...
couter = 1778 the length of the page = 61178
couter = 1779 the length of the page = 61178
couter = 1780 the length of the page = 61178
couter = 1781 the length of the page = 61178
couter = 1782 the length of the page = 61178
couter = 1783 the length of the page = 61178
couter = 1784 the length of the page = 61178
couter = 1785 the length of the page = 61178
couter = 1786 the length of the page = 61178
couter = 1787 the length of the page = 61178
couter = 1788 the length of the page = 61178
couter = 1789 the length of the page = 61178
couter = 1790 the length of the page = 61178
couter = 1791 the length of the page = 61178
couter = 1792 the length of the page = 61178
couter = 1793 the length of the page = 61178
couter = 1794 the length of the page = 61178
couter = 1795 the length of the page = 61178
couter = 1796 the length of the page = 61178
couter = 1797 the length of the page = 61178
couter = 1798 the length of the page = 61178
couter = 1799 the length of the page = 61178
couter = 1800 the length of the page = 61178
couter = 1800 the length of the page = 0
couter = 1800 the length of the page = 0
couter = 1800 the length of the page = 0
couter = 1800 the length of the page = 0
couter = 1800 the length of the page = 0
couter = 1801 the length of the page = 61178
couter = 1801 the length of the page = 0
couter = 1801 the length of the page = 0
couter = 1801 the length of the page = 0
couter = 1802 the length of the page = 61178
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1802 the length of the page = 0
couter = 1803 the length of the page = 61178
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
.couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
...couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
.couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
.couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
......couter = 1803 the length of the page = 0
.............................couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
couter = 1803 the length of the page = 0
.......couter = 1803 the length of the page = 0
...................................................End
----------------------------------------
Server stress level = 1803
package ServersTestStress;
/*
Написать программу для стресс-тестирования веб - серверов.
*/
public class Main {
public static void main(String[] args) {
String path = "site.ua";
int numbrOfThread = 100;
ServiceFix.begin(path, numbrOfThread);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment