Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
beat.core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
beat
beat.core
Commits
fa7de4f1
Commit
fa7de4f1
authored
7 years ago
by
Philip ABBET
Browse files
Options
Downloads
Patches
Plain Diff
Optimization: the discovery of docker images can be cached
parent
ed164fee
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
beat/core/dock.py
+21
-1
21 additions, 1 deletion
beat/core/dock.py
beat/core/test/test_docker.py
+73
-0
73 additions, 0 deletions
beat/core/test/test_docker.py
with
94 additions
and
1 deletion
beat/core/dock.py
100644 → 100755
+
21
−
1
View file @
fa7de4f1
...
...
@@ -47,6 +47,8 @@ from . import stats
class
Host
(
object
):
'''
An object of this class can connect to the docker host and resolve stuff
'''
images_cache
=
{}
def
__init__
(
self
,
**
kwargs
):
...
...
@@ -60,6 +62,11 @@ class Host(object):
del
kwargs
[
'
port
'
]
kwargs
[
'
base_url
'
]
=
"
http://%s:%s
"
%
(
host
,
port
)
self
.
images_cache_filename
=
None
if
'
images_cache
'
in
kwargs
:
self
.
images_cache_filename
=
kwargs
.
get
(
'
images_cache
'
)
del
kwargs
[
'
images_cache
'
]
self
.
kwargs
=
kwargs
self
.
environments
=
{}
self
.
db_environments
=
{}
...
...
@@ -70,8 +77,16 @@ class Host(object):
self
.
client
=
docker
.
Client
(
**
self
.
kwargs
)
if
(
self
.
images_cache_filename
is
not
None
)
and
os
.
path
.
exists
(
self
.
images_cache_filename
):
with
open
(
self
.
images_cache_filename
,
'
r
'
)
as
f
:
Host
.
images_cache
=
simplejson
.
load
(
f
)
(
self
.
environments
,
self
.
db_environments
)
=
self
.
_discover_environments
(
raise_on_errors
)
if
self
.
images_cache_filename
is
not
None
:
with
open
(
self
.
images_cache_filename
,
'
w
'
)
as
f
:
simplejson
.
dump
(
Host
.
images_cache
,
f
,
indent
=
4
)
def
__contains__
(
self
,
key
):
return
(
key
in
self
.
environments
)
or
(
key
in
self
.
db_environments
)
...
...
@@ -149,10 +164,15 @@ class Host(object):
def
_describe
(
image
):
'''
Tries to run the
"
describe
"
app on the image, collect results
'''
if
Host
.
images_cache
.
has_key
(
image
):
return
Host
.
images_cache
[
image
]
status
,
output
=
self
.
get_statusoutput
(
image
,
[
'
describe
'
])
if
status
==
0
:
try
:
return
simplejson
.
loads
(
output
)
infos
=
simplejson
.
loads
(
output
)
Host
.
images_cache
[
image
]
=
infos
return
infos
except
Exception
as
e
:
logger
.
warn
(
"
Ignoring potential environment at `%s
'
since
"
\
"
`describe
'
output cannot be parsed: %s
"
,
image
,
str
(
e
))
...
...
This diff is collapsed.
Click to expand it.
beat/core/test/test_docker.py
+
73
−
0
View file @
fa7de4f1
...
...
@@ -34,11 +34,13 @@ import sys
import
time
import
unittest
import
pkg_resources
import
time
import
docker
import
requests
from
..dock
import
Popen
,
Host
from
.
import
tmp_prefix
# in case you want to see the printouts dynamically, set to ``True``
if
False
:
...
...
@@ -275,3 +277,74 @@ class AsyncTest(unittest.TestCase):
def
test_cpulimit_at_100percent
(
self
):
# runs 4 processes that should consume 50% of the host CPU
self
.
_run_cpulimit
(
4
,
100
,
3
)
class
HostTest
(
unittest
.
TestCase
):
def
setUp
(
self
):
Host
.
images_cache
=
{}
def
test_images_cache
(
self
):
self
.
assertEqual
(
len
(
Host
.
images_cache
),
0
)
# Might take some time
start
=
time
.
time
()
host
=
Host
()
host
.
setup
(
raise_on_errors
=
False
)
host
.
teardown
()
stop
=
time
.
time
()
nb_images
=
len
(
Host
.
images_cache
)
self
.
assertTrue
(
nb_images
>
0
)
self
.
assertTrue
(
stop
-
start
>
2.0
)
# Should be instantaneous
start
=
time
.
time
()
host
=
Host
()
host
.
setup
(
raise_on_errors
=
False
)
host
.
teardown
()
stop
=
time
.
time
()
self
.
assertEqual
(
len
(
Host
.
images_cache
),
nb_images
)
self
.
assertTrue
(
stop
-
start
<
1.0
)
def
test_images_cache_file
(
self
):
self
.
assertEqual
(
len
(
Host
.
images_cache
),
0
)
# Might take some time
start
=
time
.
time
()
host
=
Host
(
images_cache
=
os
.
path
.
join
(
tmp_prefix
,
'
images_cache.json
'
))
host
.
setup
(
raise_on_errors
=
False
)
host
.
teardown
()
stop
=
time
.
time
()
nb_images
=
len
(
Host
.
images_cache
)
self
.
assertTrue
(
nb_images
>
0
)
self
.
assertTrue
(
stop
-
start
>
2.0
)
Host
.
images_cache
=
{}
# Should be instantaneous
start
=
time
.
time
()
host
=
Host
(
images_cache
=
os
.
path
.
join
(
tmp_prefix
,
'
images_cache.json
'
))
host
.
setup
(
raise_on_errors
=
False
)
host
.
teardown
()
stop
=
time
.
time
()
self
.
assertEqual
(
len
(
Host
.
images_cache
),
nb_images
)
self
.
assertTrue
(
stop
-
start
<
1.0
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment