Skip to content

Commit

Permalink
major update.
Browse files Browse the repository at this point in the history
add support for parallel execution.
more configuration options.
MemoryOutOfBoundsError solved.
  • Loading branch information
NeelPatel21 committed Feb 18, 2017
1 parent 524f6ae commit 600cb89
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 33 deletions.
Binary file modified ProgramTesterUser.jar
Binary file not shown.
4 changes: 3 additions & 1 deletion config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ problem_dir :- resources\ProbDef\
source_data_dir :- resources\Data\
local_logger_dir :- resources\temp\log\
network_logger_ip :- 127.0.0.1
network_logger_port :- 8686
network_logger_port :- 8686
#parallel_execution :- false
parallel_thread :- 2
Binary file modified gmon.out
Binary file not shown.
8 changes: 5 additions & 3 deletions nbproject/private/private.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<group>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/ui/cli/CliUser.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/programtester/ProgramTester.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/userModule/IntUserFlow.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/ui/IntUI.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/userModule/result/IntProgramState.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/runTest/RunTest.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/userModule/SingleUserFlow.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/programtester/config/Configurator.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/userModule/test/Test.java</file>
<file>file:/C:/My%20folder/Fun/Data/Program%20Tester/src/lib/dT/manipulate/comparators/StringComparators.java</file>
</group>
</open-files>
</project-private>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
170215054628
@title
Write a program to check whether the entered strings are palindrome or not.
@description
Palindrome strings are the ones that are read same if reversed (By example, rar is rar if reversed).
make a program to determine that the string is palindrome or not.
Write a program to check whether the entered strings are palindrome or not.
Expand Down
Binary file modified resources/gmon.out
Binary file not shown.
2 changes: 1 addition & 1 deletion resources/prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void main()
scanf("%d",&s[i]);
merge(&s[0],l);
for(i=0;i<l;i++)
printf("%d45\n\n",s[i]);
printf("%d \n\n",s[i]);
}
void merge(long *a,long i)
{
Expand Down
Binary file modified resources/prop.exe
Binary file not shown.
6 changes: 4 additions & 2 deletions src/lib/dT/manipulate/comparators/ListManipulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ public static boolean compare(List<String>o1,List<String>o2,
}
}catch(NoSuchElementException e){
return false;
}finally{
System.gc();
}
return false;
return false;
}

/**
Expand All @@ -55,7 +57,7 @@ public static boolean compare(List<String>o1,List<String>o2,
from {@code li}.
*/
public static List<String> removeNull(List<String> li){
return li.stream().filter(s->s!=null&&s!="")
return li.stream().filter(s->!s.trim().isEmpty())
.collect(Collectors.toList());
}

Expand Down
27 changes: 16 additions & 11 deletions src/lib/runTest/ProgramExecuter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
class ProgramExecuter {
boolean executed=false;
String cmd;
ProcessBuilder pb; //used to create subprocess
//ProcessBuilder pb; //used to create subprocess
Process pr; //refer to the subprocess
Thread tin,tout; //refer to the thread which give input and output
List<String> input; //input of subprocess
List<String> output; //output of subprocess
Scanner sc; //Scanner bind to standard outputStream and error stream
//Scanner sc; //Scanner bind to standard outputStream and error stream
long time=-1; //time taken by subprocess in milli secounds.
BufferedReader in;
PrintWriter out; //bind to standard input stream of subprocess.
Expand All @@ -31,8 +31,8 @@ class ProgramExecuter {
ProgramExecuter(List<String> input,String cmd){
this.input=input;
this.cmd=cmd;
pb=new ProcessBuilder(cmd);
pb.redirectErrorStream(true); //error stream bind with the output steam of subprocess
//pb=new ProcessBuilder(cmd);
//pb.redirectErrorStream(true); //error stream bind with the output steam of subprocess
output=new ArrayList<>();
}

Expand All @@ -57,10 +57,10 @@ synchronized List<String> execute(long time) throws IOException{
pr=Runtime.getRuntime().exec(cmd);
//pr=pb.start(); //start new subprocess.
//System.out.println("ProgramExecuter.execute :- started");
//in=new BufferedReader(new InputStreamReader(pr.getInputStream()));
in=new BufferedReader(new InputStreamReader(pr.getInputStream()));
//in=new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
out=new PrintWriter(new BufferedOutputStream(pr.getOutputStream()));
sc=new Scanner(pr.getInputStream());
//sc=new Scanner(pr.getInputStream());
tin=new Thread(this::giveIn); //create thread to give input to subprocess
tout=new Thread(this::getOut); //create thread to get output of subprocess
tout.start();
Expand Down Expand Up @@ -91,8 +91,12 @@ void stop(){
pr.destroyForcibly();
}
if(tin!=null&&tin.isAlive()){
sc.close();
//tin.destroy();
try {
//sc.close();
in.close();
//tin.destroy();
} catch (IOException ex) {
}
}
if(tout!=null&&tout.isAlive()){
//tout.destroy();
Expand Down Expand Up @@ -133,17 +137,18 @@ private void giveIn(){
private void getOut(){
//System.out.println("ProgramExecuter.getOut :- started");
//for(;pr.isAlive();){
//in.lines().forEach(s->output.add(s));
sc.forEachRemaining(s->{
in.lines().limit(200000).forEach(s->output.add(s));
/*sc.forEachRemaining(s->{
try{
//String s=in.readLine();
output.add(s);
//System.out.println("ProgramExecuter.getout :- get out "+s);
}catch(NoSuchElementException ex){
//System.out.println(ex);
}
});
});*/
//System.out.println("ProgramExecuter.getOut : - ended");
//System.gc();
}

/**return runtime of subprocess in milli seconds.
Expand Down
9 changes: 6 additions & 3 deletions src/lib/userModule/SingleUserFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,16 @@ private void update(Test t){
IntResultSet rs=t.getIntResultSet();
ProblemState x=ps.stream().filter(i->i.getProgramID()==t.getProgramID())
.findAny().get();
if(rs.getAllResult().stream().allMatch(i->i.getMessageCode()>0)){
x.setState(1);
int code=rs.getAllResult().stream().mapToInt(r->r.getMessageCode())
.min().orElse(0);
if(code>x.getState()){
x.setState(code);
}
if(logger!=null)
logger.log("DateTime = "+LocalDateTime.now()
,"PID = "+x.getProgramID()
,"State = "+x.getState());
System.gc();
}

/**
Expand Down Expand Up @@ -142,7 +145,7 @@ public IntLiveResultSet execute(long pid,String cmd) {
es.submit(()->update(t));
return rt;
} catch (IOException ex) {
System.out.println("Error");
ui.showMessage("file not found !!!");
}
return null;
}
Expand Down
100 changes: 92 additions & 8 deletions src/lib/userModule/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
*/
public class Test {
//static Part

public static final int TEST_PASS=2,TEST_PRESENT_ERROR=1,TEST_FAIL=-2,
TEST_TIME_ERROR=-1,TEST_FILE_ERROR=-3;
public static long minMem=20000000;
/*
* defDir is default path veriable which is used by construtor if no path
provided explicitly.
Expand All @@ -40,6 +42,16 @@ public class Test {
//lock on defDir
static final private ReentrantReadWriteLock ldefDir=new ReentrantReadWriteLock();

/*
* defDir is default path veriable which is used by construtor if no path
provided explicitly.
* it should only accessed by getter and setter methods only.
*/
static private boolean isParallel=true;

//lock on defDir
static final private ReentrantReadWriteLock lIsParallel=new ReentrantReadWriteLock();

/**
* getter method of default path.
* it is used by constructor if the path is not provided explicitly.
Expand Down Expand Up @@ -79,6 +91,40 @@ static public boolean setDefaultDir(Path p){
ldefDir.writeLock().unlock();
}
}


/**
* getter method of default execution method.
* it is used by default.
* this method is thread safe.
* @return default execution method.
*/
static public boolean getIsParallel(){
try{
lIsParallel.readLock().lock();
return isParallel;
}
finally{
lIsParallel.readLock().unlock();
}
}

/**
* setter method of default execution method.
* this method is thread safe.<br>
* @param isParallel if the execution method is parallel or not.
*/
static public void setIsParallel(boolean isParallel){
try{
if(getIsParallel()==isParallel)
return;
lIsParallel.writeLock().lock();
Test.isParallel=isParallel;
return;
}finally{
lIsParallel.writeLock().unlock();
}
}

//local Part
private final Path dir;
Expand All @@ -104,31 +150,35 @@ private void comp(TestState us){
if(!us.isExecuted())
return;
if(us.getTime()<0){
us.setState("Time Limit Exceeded", -2);
us.setState("Time Limit Exceeded",TEST_TIME_ERROR);
return;
}
IntIODetail ori=read(us.index());
if(ori==null)
us.setState("File Not Found !!", TEST_FILE_ERROR);
//System.out.println("enter check");
if(ListManipulator.compare(us.getAllOutput(),ori.getAllOutput(),
StringComparators.getExactmatch())){
//System.out.println("perfect check");
us.setState("Pass",1);
us.setState("Pass",TEST_PASS);
}
else if(ListManipulator.compare(us.getAllOutput(),ori.getAllOutput(),
else if(ListManipulator.compare(
ListManipulator.removeNull(us.getAllOutput()),
ListManipulator.removeNull(ori.getAllOutput()),
StringComparators.getIgnoreWhiteSpace())){
//System.out.println("persentation check");
us.setState("Presentation Error",2);
us.setState("Presentation Error",TEST_PRESENT_ERROR);
}
else {
us.setState("Fail", -1);
us.setState("Fail",TEST_FAIL);
}
}catch(NullPointerException ex){
throw new IllegalArgumentException();
}
}

private synchronized void reader()throws IOException{
List<TestState> ts=IntStream.rangeClosed(1,20)
List<TestState> ts=IntStream.rangeClosed(1,30)
.mapToObj(i->read(i))
.filter(i->i!=null)
//.peek(i->System.out.println("index :- "+i.index()))
Expand Down Expand Up @@ -159,12 +209,46 @@ private void run(){
for(TestState i:ts){
if(!flag) //check if the thread should be terminated.
break;
for(;Runtime.getRuntime().freeMemory()<minMem&&
minMem*2<Runtime.getRuntime().maxMemory();){
try{
System.gc();
Thread.sleep(1000);
}catch(InterruptedException e){}
}
RunTest rt=new RunTest(i,cmd);
i.setState("Executing", 0);
i.update(rt.getIODetail());
comp(i);
i.makeFinal();
}
//System.gc();
}

private void run(boolean isParallel){
if(!isParallel){
run();
return;
}
flag=true;
ts.parallelStream().peek(i->{
if(!flag) //check if the thread should be terminated.
return;
for(;Runtime.getRuntime().freeMemory()<minMem&&
minMem*2<Runtime.getRuntime().maxMemory();){
//System.out.println("jbahbajadk");
try{
System.gc();
Thread.sleep(1000);
}catch(InterruptedException e){}
}
RunTest rt=new RunTest(i,cmd);
i.setState("Executing", 0);
i.update(rt.getIODetail());
comp(i);
i.makeFinal();
System.gc();
}).count();
}

/**
Expand Down Expand Up @@ -192,7 +276,7 @@ public synchronized IntLiveResultSet start() throws IOException{
if(t!=null)
return null;
reader();
t=new Thread(this::run,"Tester Thread");
t=new Thread(()->run(getIsParallel()),"Tester Thread");
t.start();
lrs=new LiveResultSetAdapter(ts);
return lrs;
Expand Down
5 changes: 3 additions & 2 deletions src/programtester/ProgramTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ public class ProgramTester {
* @param args the command line arguments
*/
public static void main(String[] args) {
//mainUserCli();
mainAdminCli();
mainUserCli();
//mainAdminCli();
}

public static void mainUserCli(){
Configurator.init();

//Test.setDefaultDir(Paths.get("Data").toAbsolutePath());
//System.out.println("Working Directory = " +System.getProperty("user.dir"));
//System.out.println("Data Directory = " +Test.getDefaultDir());
Expand Down
14 changes: 12 additions & 2 deletions src/programtester/config/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import lib.dT.problemManipulate.ProgramDetails;
import lib.logger.LocalLogger;
import lib.userModule.test.Test;
Expand Down Expand Up @@ -70,6 +68,18 @@ private static void proc(String s){
break;
case "network_logger_ip":
break;
case "parallel_execution":
boolean b=Boolean.parseBoolean(ar[1]);
Test.setIsParallel(b);
break;
case "parallel_thread":
try{
int i=Integer.parseInt(ar[1]);
System.setProperty("java.util.concurrent.ForkJoinPool"
+ ".common.parallelism",""+i);

}catch(NumberFormatException e){}
break;
case "network_logger_port":
break;

Expand Down

0 comments on commit 600cb89

Please sign in to comment.