Add performance test for doc#man and refactor version_changes for performance

This commit is contained in:
bry4n 2014-07-28 23:04:59 -05:00
Родитель 31aba3c901
Коммит 6720481ba6
11 изменённых файлов: 100 добавлений и 63 удалений

Просмотреть файл

@ -47,7 +47,8 @@ end
group :development, :test do
gem 'dotenv-rails'
gem "sqlite3"
# gem "debugger"
gem 'byebug'
gem 'ruby-prof'
end
group :test do
@ -56,4 +57,5 @@ group :test do
gem 'rspec-rails'
gem 'shoulda-matchers'
gem 'webmock'
gem 'rails-perftest'
end

Просмотреть файл

@ -38,6 +38,9 @@ GEM
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (3.1.2)
columnize (~> 0.8)
debugger-linecache (~> 1.2)
chunky_png (1.3.1)
coderay (1.1.0)
coffee-rails (4.0.1)
@ -47,6 +50,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.7.1)
columnize (0.8.9)
commonjs (0.2.7)
compass (0.12.7)
chunky_png (~> 1.2)
@ -59,6 +63,7 @@ GEM
dalli (2.7.2)
database_cleaner (1.3.0)
debug_inspector (0.0.2)
debugger-linecache (1.2.0)
diff-lcs (1.2.5)
dotenv (0.11.1)
dotenv-deployment (~> 0.0.2)
@ -126,6 +131,7 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.4)
sprockets-rails (~> 2.0)
rails-perftest (0.0.4)
rails_12factor (0.0.2)
rails_serve_static_assets
rails_stdout_logging
@ -161,6 +167,7 @@ GEM
rspec-mocks (~> 3.0.0)
rspec-support (~> 3.0.0)
rspec-support (3.0.3)
ruby-prof (0.15.1)
rubyzip (1.1.6)
safe_yaml (1.0.3)
sass (3.2.19)
@ -231,6 +238,7 @@ DEPENDENCIES
awesome_print
better_errors
binding_of_caller
byebug
coffee-rails
compass-rails
dalli
@ -254,9 +262,11 @@ DEPENDENCIES
pg
rack-timeout
rails (= 4.1.4)
rails-perftest
rails_12factor
redcarpet
rspec-rails
ruby-prof
rubyzip
sass-rails (= 4.0.3)
shoulda-matchers

Просмотреть файл

@ -12,30 +12,4 @@ class Doc < ActiveRecord::Base
has_many :doc_versions
# returns an array of the differences with 3 entries
# 0: additions
# 1: subtractions
# 2: 8 - (add + sub)
def self.get_diff(sha_to, sha_from)
key = "diff-#{sha_to}-#{sha_from}"
doc_to = Doc.where(:blob_sha => sha_to).first.plain.split("\n")
doc_from = Doc.where(:blob_sha => sha_from).first.plain.split("\n")
diff = Diff::LCS.diff(doc_to, doc_from)
total = adds = mins = 0
diff.first.each do |change|
adds += 1 if change.action == '+'
mins += 1 if change.action == '-'
total += 1
end
if total > 8
min = (8.0 / total)
adds = (adds * min).floor
mins = (mins * min).floor
[adds, mins, (8 - total)]
else
[adds, mins, (8 - total)]
end
rescue
[0, 0, 8]
end
end

Просмотреть файл

@ -22,8 +22,8 @@ class DocVersion < ActiveRecord::Base
for_doc(doc_name).joins([:version, :doc]).where('docs.blob_sha = ?', sha).order('versions.vorder').first
end
def self.latest_versions(doc_name)
for_doc(doc_name).includes(:version).order('versions.vorder DESC')
def self.latest_versions(doc_name, size=20)
for_doc(doc_name).includes(:version).order('versions.vorder DESC').limit(size)
end
def self.for_version(doc_name, version_name)
@ -33,13 +33,12 @@ class DocVersion < ActiveRecord::Base
def self.version_changes(file, size = 20)
versions = []
unchanged = []
vers = DocVersion.latest_versions(file)
(vers.size-2).times do |i|
v = vers[i]
prev = vers[i+1]
sha2 = v.doc.blob_sha
vers = includes(:doc, :version).order("versions.vorder DESC").limit(size)
vers.each_with_index do |v, i|
next unless prev = vers[i+1]
sha1 = prev.doc.blob_sha
if sha1 == sha2
sha2 = v.doc.blob_sha
if sha1 == sha2
unchanged << v.version.name
else
if unchanged.size > 0
@ -49,11 +48,38 @@ class DocVersion < ActiveRecord::Base
versions << {:name => "#{unchanged.last} &rarr; #{unchanged.first} no changes", :changed => false}
end
unchanged = []
else
versions << {:name => v.version.name, :time => v.version.committed, :diff => v.diff(prev), :changed => true}
end
versions << {:name => v.version.name, :time => v.version.committed, :diff => Doc.get_diff(sha2, sha1), :changed => true}
end
end
versions[0,size]
versions
end
# returns an array of the differences with 3 entries
# 0: additions
# 1: subtractions
# 2: 8 - (add + sub)
def diff(doc_version)
begin
to = self.doc.plain.split("\n")
from = doc_version.doc.plain.split("\n")
total = adds = mins = 0
diff = Diff::LCS.diff(to, from)
diff.first.each do |change|
adds += 1 if change.action == "+"
mins += 1 if change.action == "-"
total += 1
end
if total > 8
min = (8.0 / total)
adds = (adds * min).floor
mins = (mins * min).floor
end
[adds, mins, (8 - total)]
rescue
[0, 0, 8]
end
end
def index

Просмотреть файл

@ -13,16 +13,13 @@
ActiveRecord::Schema.define(version: 20121211011752) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "books", force: true do |t|
t.string "code"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "books", ["code"], name: "index_books_on_code", using: :btree
add_index "books", ["code"], name: "index_books_on_code"
create_table "chapters", force: true do |t|
t.string "title"
@ -33,7 +30,7 @@ ActiveRecord::Schema.define(version: 20121211011752) do
t.string "sha"
end
add_index "chapters", ["book_id"], name: "index_chapters_on_book_id", using: :btree
add_index "chapters", ["book_id"], name: "index_chapters_on_book_id"
create_table "doc_files", force: true do |t|
t.string "name"
@ -41,7 +38,7 @@ ActiveRecord::Schema.define(version: 20121211011752) do
t.datetime "updated_at"
end
add_index "doc_files", ["name"], name: "index_doc_files_on_name", using: :btree
add_index "doc_files", ["name"], name: "index_doc_files_on_name"
create_table "doc_versions", force: true do |t|
t.integer "version_id"
@ -59,7 +56,7 @@ ActiveRecord::Schema.define(version: 20121211011752) do
t.datetime "updated_at"
end
add_index "docs", ["blob_sha"], name: "index_docs_on_blob_sha", using: :btree
add_index "docs", ["blob_sha"], name: "index_docs_on_blob_sha"
create_table "downloads", force: true do |t|
t.string "url"
@ -94,8 +91,8 @@ ActiveRecord::Schema.define(version: 20121211011752) do
t.integer "number"
end
add_index "sections", ["chapter_id"], name: "index_sections_on_chapter_id", using: :btree
add_index "sections", ["slug"], name: "index_sections_on_slug", using: :btree
add_index "sections", ["chapter_id"], name: "index_sections_on_chapter_id"
add_index "sections", ["slug"], name: "index_sections_on_slug"
create_table "versions", force: true do |t|
t.string "name"
@ -107,6 +104,6 @@ ActiveRecord::Schema.define(version: 20121211011752) do
t.float "vorder"
end
add_index "versions", ["name"], name: "index_versions_on_name", using: :btree
add_index "versions", ["name"], name: "index_versions_on_name"
end

9
script/benchmark Executable file
Просмотреть файл

@ -0,0 +1,9 @@
#!/bin/bash
dropdb gitscm_test
createdb gitscm_test
rake db:test:load
rake test:benchmark

Просмотреть файл

@ -2,7 +2,9 @@ script/bootstrap
export OLD_PWD=$PWD
export GIT_REPO=../git/.git
rm db/*.sqlite3
dropdb gitscm_development
createdb gitscm_development
rake db:migrate
if [ -d "$GIT_REPO" ]; then
@ -15,3 +17,4 @@ cd $OLD_PWD
# Build git docs
bundle exec rake local_index
bundle exec rake remote_genbook

Просмотреть файл

@ -1,8 +1,8 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'database_cleaner'
require 'webmock/rspec'
@ -45,7 +45,7 @@ RSpec.configure do |config|
# the seed, which is printed after each run.
# --seed 1234
# config.order = "random"
config.treat_symbols_as_metadata_keys_with_true_values = true
# config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end

Просмотреть файл

@ -0,0 +1,29 @@
require 'test_helper'
require 'rails/performance_test_help'
require "database_cleaner"
class BrowsingDocTest < ActionDispatch::PerformanceTest
self.profile_options = {runs: 10, metrics: [:wall_time, :process_time]}
def setup
DatabaseCleaner.strategy = :truncation
DatabaseCleaner.clean
100.times do |i|
tree_sha = SecureRandom.hex
commit_sha = SecureRandom.hex
ascii_sha = SecureRandom.hex
version = Version.create(name: "#{i}.0.0", commit_sha: commit_sha, tree_sha: tree_sha, committed: Time.current)
doc_file = DocFile.create(name: "git-config")
doc = Doc.create(blob_sha: ascii_sha, html: "test", plain: "test")
doc_version = DocVersion.create(version_id: version.id, doc_id: doc.id, doc_file_id: doc_file.id)
end
end
def teardown
DatabaseCleaner.clean
end
test "browsing git-config" do
get '/docs/git-config'
end
end

Просмотреть файл

@ -1,12 +0,0 @@
require 'test_helper'
require 'rails/performance_test_help'
class BrowsingTest < ActionDispatch::PerformanceTest
# Refer to the documentation for all available options
# self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory]
# :output => 'tmp/performance', :formats => [:flat] }
def test_homepage
get '/'
end
end

Просмотреть файл

@ -1,7 +1,6 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'shoulda'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.