Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
269e0f43
Commit
269e0f43
authored
Apr 22, 2020
by
nmilojevic1
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Address MR comments
- Add specs for measuring - Rename base_data
parent
2b71e75f
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
79 additions
and
36 deletions
+79
-36
app/services/projects/gitlab_projects_import_service.rb
app/services/projects/gitlab_projects_import_service.rb
+7
-9
lib/gitlab/utils/measuring.rb
lib/gitlab/utils/measuring.rb
+17
-17
spec/lib/gitlab/utils/measuring_spec.rb
spec/lib/gitlab/utils/measuring_spec.rb
+49
-4
spec/support/shared_examples/services/gitlab_projects_import_service_shared_examples.rb
...ervices/gitlab_projects_import_service_shared_examples.rb
+6
-6
No files found.
app/services/projects/gitlab_projects_import_service.rb
View file @
269e0f43
...
...
@@ -18,7 +18,7 @@ module Projects
measurement_enabled
=
!!
options
[
:measurement_enabled
]
measurement_logger
=
options
[
:measurement_logger
]
::
Gitlab
::
Utils
::
Measuring
.
execute_with
(
measurement_enabled
,
measurement_logger
,
base_data
)
do
::
Gitlab
::
Utils
::
Measuring
.
execute_with
(
measurement_enabled
,
measurement_logger
,
base_
log_
data
)
do
prepare_template_environment
(
template_file
)
prepare_import_params
...
...
@@ -29,19 +29,19 @@ module Projects
private
def
base_data
base_data
=
{
def
base_
log_
data
base_
log_
data
=
{
class:
self
.
class
.
name
,
current_user:
current_user
.
name
,
project_full_path:
project_path
}
if
template_file
base_data
[
:import_type
]
=
'gitlab_project'
base_data
[
:file_path
]
=
template_file
.
path
base_
log_
data
[
:import_type
]
=
'gitlab_project'
base_
log_
data
[
:file_path
]
=
template_file
.
path
end
base_data
base_
log_
data
end
def
overwrite_project?
...
...
@@ -61,9 +61,7 @@ module Projects
# rubocop: enable CodeReuse/ActiveRecord
def
project_path
strong_memoize
(
:project_path
)
do
"
#{
current_namespace
.
full_path
}
/
#{
params
[
:path
]
}
"
end
"
#{
current_namespace
.
full_path
}
/
#{
params
[
:path
]
}
"
end
def
overwrite?
...
...
lib/gitlab/utils/measuring.rb
View file @
269e0f43
...
...
@@ -6,20 +6,20 @@ module Gitlab
module
Utils
class
Measuring
class
<<
self
def
execute_with
(
measurement_enabled
,
logger
,
base_data
)
measurement_enabled
?
measuring
(
logger
,
base_data
).
with_measuring
{
yield
}
:
yield
def
execute_with
(
measurement_enabled
,
logger
,
base_
log_
data
)
measurement_enabled
?
measuring
(
logger
,
base_
log_
data
).
with_measuring
{
yield
}
:
yield
end
private
def
measuring
(
logger
,
base_data
)
Gitlab
::
Utils
::
Measuring
.
new
(
logger:
logger
,
base_
data:
base
_data
)
def
measuring
(
logger
,
base_
log_
data
)
Gitlab
::
Utils
::
Measuring
.
new
(
logger:
logger
,
base_
log_data:
base_log
_data
)
end
end
def
initialize
(
logger:
nil
,
base_data:
{})
@
logger
=
logger
||
Logger
.
new
(
$stdout
)
@base_data
=
base
_data
def
initialize
(
logger:
nil
,
base_
log_
data:
{})
self
.
logger
=
logger
||
Logger
.
new
(
$stdout
)
self
.
base_log_data
=
base_log
_data
end
def
with_measuring
...
...
@@ -32,9 +32,9 @@ module Gitlab
end
log_info
(
gc_stats:
@
gc_stats
,
time_to_finish:
@
time_to_finish
,
number_of_sql_calls:
@
count
,
gc_stats:
gc_stats
,
time_to_finish:
time_to_finish
,
number_of_sql_calls:
sql_calls_
count
,
memory_usage:
"
#{
Gitlab
::
Metrics
::
System
.
memory_usage
.
to_f
/
1024
/
1024
}
MiB"
,
label:
::
Prometheus
::
PidProvider
.
worker_id
)
...
...
@@ -44,20 +44,20 @@ module Gitlab
private
attr_
reader
:logger
,
:base
_data
attr_
accessor
:gc_stats
,
:time_to_finish
,
:sql_calls_count
,
:logger
,
:base_log
_data
def
with_count_queries
(
&
block
)
@
count
=
0
self
.
sql_calls_
count
=
0
counter_f
=
->
(
_name
,
_started
,
_finished
,
_unique_id
,
payload
)
{
@
count
+=
1
unless
payload
[
:name
].
in?
%w[CACHE SCHEMA]
self
.
sql_calls_
count
+=
1
unless
payload
[
:name
].
in?
%w[CACHE SCHEMA]
}
ActiveSupport
::
Notifications
.
subscribed
(
counter_f
,
"sql.active_record"
,
&
block
)
end
def
log_info
(
details
)
details
=
base_data
.
merge
(
details
)
details
=
base_
log_
data
.
merge
(
details
)
details
=
details
.
to_yaml
if
ActiveSupport
::
Logger
.
logger_outputs_to?
(
logger
,
STDOUT
)
logger
.
info
(
details
)
end
...
...
@@ -67,7 +67,7 @@ module Gitlab
stats_before
=
GC
.
stat
result
=
yield
stats_after
=
GC
.
stat
@
gc_stats
=
stats_after
.
map
do
|
key
,
after_value
|
self
.
gc_stats
=
stats_after
.
map
do
|
key
,
after_value
|
before_value
=
stats_before
[
key
]
[
key
,
before:
before_value
,
after:
after_value
,
diff:
after_value
-
before_value
]
end
.
to_h
...
...
@@ -75,8 +75,8 @@ module Gitlab
end
def
with_measure_time
result
=
0
@
time_to_finish
=
Benchmark
.
realtime
do
result
=
nil
self
.
time_to_finish
=
Benchmark
.
realtime
do
result
=
yield
end
...
...
spec/lib/gitlab/utils/measuring_spec.rb
View file @
269e0f43
...
...
@@ -3,16 +3,61 @@
require
'fast_spec_helper'
describe
Gitlab
::
Utils
::
Measuring
do
describe
'#execute_with'
do
let
(
:measurement_logger
)
{
double
(
:logger
)
}
let
(
:measurement_enabled
)
{
true
}
let
(
:base_log_data
)
do
{
class:
described_class
.
name
}
end
let
(
:result_block
)
{
'result'
}
subject
{
described_class
.
execute_with
(
measurement_enabled
,
measurement_logger
,
base_log_data
)
{
result_block
}
}
context
'when measurement is enabled'
do
let!
(
:measurement
)
{
described_class
.
new
(
logger:
measurement_logger
,
base_log_data:
base_log_data
)
}
before
do
allow
(
measurement_logger
).
to
receive
(
:info
)
end
it
'measure execution with Gitlab::Utils::Measuring instance'
,
:aggregate_failure
do
expect
(
described_class
).
to
receive
(
:new
).
with
(
logger:
measurement_logger
,
base_log_data:
base_log_data
){
measurement
}
expect
(
measurement
).
to
receive
(
:with_measuring
)
subject
end
it
'returns result from yielded block'
do
is_expected
.
to
eq
(
result_block
)
end
end
context
'when measurement is disabled'
do
let
(
:measurement_enabled
)
{
false
}
it
'does not measure service execution'
do
expect
(
Gitlab
::
Utils
::
Measuring
).
not_to
receive
(
:new
)
subject
end
it
'returns result from yielded block'
do
is_expected
.
to
eq
(
result_block
)
end
end
end
describe
'#with_measuring'
do
let
(
:logger
)
{
double
(
:logger
)
}
let
(
:base_data
)
{
{}
}
let
(
:base_
log_
data
)
{
{}
}
let
(
:result
)
{
"result"
}
before
do
allow
(
logger
).
to
receive
(
:info
)
end
let
(
:measurement
)
{
described_class
.
new
(
logger:
logger
,
base_
data:
base
_data
)
}
let
(
:measurement
)
{
described_class
.
new
(
logger:
logger
,
base_
log_data:
base_log
_data
)
}
subject
do
measurement
.
with_measuring
{
result
}
...
...
@@ -28,8 +73,8 @@ describe Gitlab::Utils::Measuring do
is_expected
.
to
eq
(
result
)
end
context
'with base_data provided'
do
let
(
:base_data
)
{
{
test:
"data"
}
}
context
'with base_
log_
data provided'
do
let
(
:base_
log_
data
)
{
{
test:
"data"
}
}
it
'logs includes base data'
do
expect
(
logger
).
to
receive
(
:info
).
with
(
including
(
:test
,
:gc_stats
,
:time_to_finish
,
:number_of_sql_calls
,
:memory_usage
,
:label
))
...
...
spec/support/shared_examples/services/gitlab_projects_import_service_shared_examples.rb
View file @
269e0f43
...
...
@@ -55,15 +55,15 @@ RSpec.shared_examples 'gitlab projects import validations' do
end
context
'when measurable params are provided'
do
let
(
:base_data
)
do
base_data
=
{
let
(
:base_
log_
data
)
do
base_
log_
data
=
{
class:
described_class
.
name
,
current_user:
namespace
.
owner
.
name
,
project_full_path:
"
#{
namespace
.
full_path
}
/
#{
path
}
"
}
base_data
.
merge!
({
import_type:
'gitlab_project'
,
file_path:
import_params
[
:file
].
path
})
if
import_params
[
:file
]
base_
log_
data
.
merge!
({
import_type:
'gitlab_project'
,
file_path:
import_params
[
:file
].
path
})
if
import_params
[
:file
]
base_data
base_
log_
data
end
subject
{
described_class
.
new
(
namespace
.
owner
,
import_params
)
}
...
...
@@ -82,7 +82,7 @@ RSpec.shared_examples 'gitlab projects import validations' do
end
it
'measure service execution with Gitlab::Utils::Measuring'
do
expect
(
Gitlab
::
Utils
::
Measuring
).
to
receive
(
:execute_with
).
with
(
true
,
logger
,
base_data
).
and_call_original
expect
(
Gitlab
::
Utils
::
Measuring
).
to
receive
(
:execute_with
).
with
(
true
,
logger
,
base_
log_
data
).
and_call_original
expect_next_instance_of
(
Gitlab
::
Utils
::
Measuring
)
do
|
measuring
|
expect
(
measuring
).
to
receive
(
:with_measuring
).
and_call_original
end
...
...
@@ -98,7 +98,7 @@ RSpec.shared_examples 'gitlab projects import validations' do
end
it
'does not measure service execution'
do
expect
(
Gitlab
::
Utils
::
Measuring
).
to
receive
(
:execute_with
).
with
(
false
,
nil
,
base_data
).
and_call_original
expect
(
Gitlab
::
Utils
::
Measuring
).
to
receive
(
:execute_with
).
with
(
false
,
nil
,
base_
log_
data
).
and_call_original
expect
(
Gitlab
::
Utils
::
Measuring
).
not_to
receive
(
:new
)
subject
.
execute
(
measurable_options
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment