Skip to content

Commit d68cdd1

Browse files
Add files via upload
1 parent 6d5695d commit d68cdd1

29 files changed

+2502
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
3+
* Basically, to get the statistics and anti-patterns detection results:
4+
5+
```$./ana.sh app_name $DIR```
6+
7+
$DIR is the path of your app as application/app/
8+
9+
For example, if you want to run a single action,
10+
```
11+
$./single_action.sh forem PostsController,index /home/forem/app/
12+
```
13+
14+
Results will stored in the ../application/forem/results/PostsController_index/*.xml.
15+
16+
* If you wish to run sin actions from an application, do:
17+
```
18+
$./ana.sh app_name /home/forem/app/
19+
```
20+
21+
Results will be saved to the folder under the app directory.
22+
The folder name is defined under global.rb, and by default is /result/.
23+
24+
For you application, we need a calls.txt file, which you can achieve as follows:
25+
26+
* in your app, run:
27+
```
28+
$rake routes | tail -n +2 | awk '{ for (i=1;i<=NF;i++) if (match($i, /.#./)) print $i}' | sed -e 's/#/,/g' | sort | uniq
29+
```
30+
to get the list of entrance controller actions that can be invoked by the app, copy it to APPDIR/calls.txt
31+
32+
33+

add_gems.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
folder = ARGV[0]
2+
gems = ['will_paginate', 'bootstrap-sass', 'sass-rails', 'render_async', 'react-rails-hot-loader', 'active_record_query_trace']
3+
re_gems = []
4+
for gem in gems
5+
has_gem_cmd = `cd #{folder}; bundle show #{gem}`
6+
if has_gem_cmd.include?"Could not find gem '#{gem}'"
7+
re_gems << gem
8+
#puts "has #{has_gem_cmd}"
9+
end
10+
end
11+
puts re_gems
12+
success_msg = "Bundle complete!"
13+
gemfilename = folder + "/" + 'Gemfile'
14+
if File.exists?gemfilename
15+
gemfile = open(gemfilename, 'a')
16+
original_file = open(gemfilename,'r').read
17+
re_gems.each do |gem|
18+
add_gem_string = "gem '#{gem}'\n"
19+
gemfile.write(add_gem_string)
20+
end
21+
gemfile.close
22+
# run bundle install
23+
bundle_msg = `cd #{folder}; bundle install`
24+
25+
# if not successful, output the error msg and restore the Gemfile
26+
if !bundle_msg.include?success_msg
27+
open(gemfilename,'w').write(original_file)
28+
puts "====gem file add error====\ndetailed reasons are listed:"
29+
puts bundle_msg
30+
end
31+
else
32+
puts "Gemfile doesn't exist"
33+
end
34+

applications/generate_dataflow_log.rb

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require 'pathname'
2+
require 'yard'
3+
require 'work_queue'
4+
apppath = "#{ARGV[0]}"
5+
non_dirs = ['controllers']
6+
load '../controller_model_analysis/traverse_ast.rb'
7+
def os_walk(dir)
8+
root = Pathname(dir)
9+
files, dirs = [], []
10+
Pathname(root).find do |path|
11+
unless path == root
12+
dirs << path if path.directory?
13+
files << path if path.file?
14+
end
15+
end
16+
return root, files, dirs
17+
end
18+
def generate_dataflow(apppath, c_a)
19+
wq = WorkQueue.new 10, 20
20+
time1 = Time.now
21+
#puts"APPPATH #{apppath}"
22+
root, files, dirs = os_walk(apppath)
23+
threads = []
24+
if c_a
25+
fn = c_a.split(',')[0]
26+
fs = fn.gsub("::", "/").gsub(/\w(?=[A-Z])/){|match| "#{match}_"}.gsub("Controller", "controller").downcase
27+
filename = apppath + '/dataflow/merged_controllers/' + fs + ".log"
28+
cfilename = apppath + '/merged_controllers/' + fs + '.rb'
29+
contents = open(cfilename, 'r').read
30+
root = YARD::Parser::Ruby::RubyParser.parse(contents).root
31+
root = getDefNode(root, c_a.split(',')[1])
32+
$const = [fn]
33+
con(root)
34+
$const_fn = $const.map{|e| e.gsub("::", "/").gsub(/\w(?=[A-Z])/){|match| "#{match}_"}.gsub("Controller", "controller").downcase}
35+
36+
end
37+
files.each do |filename|
38+
#puts"filename #{filename}"
39+
wq.enqueue_b("") do
40+
41+
time = Time.now
42+
if filename.to_s.end_with?(".rb")
43+
fname = filename.to_s
44+
n_begin = filename.to_s.rindex('/')
45+
n_end = filename.to_s.rindex('.')
46+
class_name = filename.to_s[n_begin+1...n_end]
47+
if $const_fn and !$const_fn.include?class_name
48+
next
49+
end
50+
#puts"classname #{class_name} #{fname}"
51+
if class_name == "schema"
52+
next
53+
end
54+
k = fname.index("#{apppath}")
55+
#des_file = "%s/%s.log"%(des_controller,class_name)
56+
l = fname.index(".rb")
57+
if !l
58+
next
59+
end
60+
#puts"fname: #{fname} k: #{k} l: #{l} #{}"
61+
des_file = fname[0...k+apppath.length] + "/dataflow/" + fname[k+apppath.length...l] + ".log"
62+
d_ind = des_file.rindex('/')
63+
dir_name = des_file[0...d_ind+1]
64+
if dir_name.include?"/controllers"
65+
next
66+
end
67+
#puts"dir_name = #{dir_name}"
68+
if File.exist?(dir_name) == false
69+
system("mkdir -p #{dir_name}")
70+
end
71+
cmd = "jrubyc #{fname} > #{des_file}"
72+
puts cmd
73+
system(cmd)
74+
system("rm #{fname.gsub(".rb",".class")}")
75+
76+
#thread.join
77+
#puts"DURATION: #{Time.now - time}"
78+
end
79+
end
80+
#}
81+
#threads.push(thread)
82+
#thread.join
83+
end
84+
wq.join
85+
# threads.each do |t|
86+
# t.join
87+
# end
88+
end
89+
generate_dataflow(ARGV[0], ARGV[1])
90+
#puts"JRUBY: #{Time.now - time1}"

attachments/active_query_trace.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ActiveRecordQueryTrace.enabled = true
2+
ActiveRecordQueryTrace.lines = 0
3+
ActiveRecordQueryTrace.level = :rails

attachments/arrow.png

3.49 KB
Loading

attachments/requests_controller.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class RequestsController < ApplicationController
2+
def request_handle
3+
f = open("request.log", 'w+')
4+
f.write(params[:data_value])
5+
f.close
6+
#render :nothing => true, :status => 200, :content_type => 'text/html'
7+
# wait for the code refactor
8+
# when refactor is finished, it should put "OK" in the request.log
9+
while true
10+
con = open('request.log').read
11+
if con.include?("FINISH")
12+
f = open("request.log", 'w')
13+
f.write("")
14+
f.close
15+
if con.include?('NO')
16+
respond_to do |format|
17+
format.js {render inline: "alert('You did not accept the code change');" }
18+
end
19+
else
20+
respond_to do |format|
21+
format.js {render inline: "location.reload();" }
22+
end
23+
end
24+
break
25+
end
26+
sleep(0.5)
27+
end
28+
end
29+
end

compute_performance.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
./start_analysis.sh $1 $2 $3 > running.log &

dataflow.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
app=$1
2+
output='./applications/'
3+
pre_pro='./preprocess_views'
4+
controller='./controller_model_analysis'
5+
6+
echo "jruby get dataflow"
7+
cd $output; pwd; ruby generate_dataflow_log.rb $app; cd ../
8+
echo "FINISH dataflow"
9+
10+
#echo "run analysis"
11+
#cd $controller; pwd; ruby main.rb -p $c_a -d ../$output/$app/
12+
#echo "FINISH analysis"

perf_action.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
app=$1
2+
input=$2
3+
output='./applications/'
4+
pre_pro='./preprocess_views'
5+
controller='./controller_model_analysis'
6+
call=$input/'calls.txt'
7+
schema=$input/db/'schema.rb'
8+
rm -rf $output/$app
9+
echo "remove existing"
10+
mkdir $output/$app
11+
echo "move app dir to applications "
12+
cp -r $input/app/* $output/$app/
13+
cp $call $output/$app
14+
cp $schema $output/$app
15+
echo "finished moving"
16+
17+
echo "preprocessing"
18+
echo $app
19+
cd $pre_pro; pwd; ruby main.rb -a $app; cd ../
20+
echo "finish preprocessing"
21+
22+
echo "jruby get dataflow"
23+
cd $output; pwd; ruby generate_dataflow_log.rb $app; cd ../
24+
echo "FINISH dataflow"
25+
26+
#echo "run analysis"
27+
#cd $controller; pwd; ruby main.rb -p $c_a -d ../$output/$app/
28+
#echo "FINISH analysis"

prep.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
app=$1
2+
input=$2
3+
output='./applications/'
4+
pre_pro='./preprocess_views'
5+
controller='./controller_model_analysis'
6+
call=$input/'calls.txt'
7+
schema=$input/db/'schema.rb'
8+
rm -rf $output/$app
9+
echo "remove existing"
10+
mkdir $output/$app
11+
echo "move app dir to applications "
12+
cp -r $input/app/* $output/$app/
13+
cp $call $output/$app
14+
cp $schema $output/$app
15+
echo "finished moving"
16+
17+
echo "preprocessing"
18+
echo $app
19+
cd $pre_pro; pwd; ruby main.rb -a $app; cd ../
20+
echo "finish preprocessing"

preprocess_views/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
0. run extract\_ruby and generate ruby views
2+
$ vi util.rb
3+
modify "run\_command" function to let it actually execute the commands
4+
5+
1. replace controller/helper file:
6+
if app has helper folder:
7+
$ ruby main.rb -a #{app\_name} -e
8+
else:
9+
$ ruby main.rb -a #{app\_name}
10+
11+
2. generate next actions:
12+
$ ruby main.rb -a #{app\_name} -f
13+
14+
copy the "path\_funcs.sh" to the application directory and run the script
15+
16+
copy the "path\_funcs.txt" from the application directory back to this dir
17+
18+
then generate the next action files:
19+
$ruby main.rb -a #{app\_name} -e -n

preprocess_views/check_system.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
OS=`uname -s`
2+
3+
if [ "$OS" == "Darwin" ]; then
4+
echo "Mac OS X with Darwin Kernel"
5+
cp extract_ruby_mac extract_ruby
6+
elif [ "$OS" == "Linux" ]; then
7+
echo "GNU/Linux OS with Linux Kernel"
8+
g++ -std=c++11 extract_ruby.cpp -o extract_ruby
9+
else
10+
echo "Not Mac or Linux"
11+
fi

preprocess_views/extract_ruby

18.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)