Skip to content

Instantly share code, notes, and snippets.

@chris-allan
Last active September 18, 2015 13:33
Show Gist options
  • Save chris-allan/e62f5a2d12dda91b3894 to your computer and use it in GitHub Desktop.
Save chris-allan/e62f5a2d12dda91b3894 to your computer and use it in GitHub Desktop.
Test performance of annotation queries
#!/usr/bin/env python
# encoding: utf-8
"""
Tests performance of various annotation queries.
Copyright (C) 2015 Glencoe Software, Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
import time
import sys
from tempfile import NamedTemporaryFile
from omero.gateway import BlitzGateway
from omero.rtypes import rstring
from omero.model import ImageI, CommentAnnotationI
from omero.sys import ParametersI
def perform_queries(gateway, image):
query_service = gateway.getQueryService()
params = ParametersI()
params.addId(image.getId())
now = time.time()
query_service.findByQuery('''
select i from Image as i
left outer join fetch i.annotationLinks as a_link
left outer join fetch a_link.child as ann
where i.id = :id''', params)
print 'No joined files elapsed: %s' % (time.time() - now)
now = time.time()
query_service.findByQuery('''
select i from Image as i
left outer join fetch i.annotationLinks as a_link
left outer join fetch a_link.child as ann
left outer join fetch ann.file
where i.id = :id''', params)
print 'Joined files elapsed: %s' % (time.time() - now)
def test_file_annotation_speed(gateway, count):
""" Tests speed of loading file annotations. See PR: 4176 """
print 'Testing (%d) file annotation speed...' % count
ns = 'omero.gateway.test_annotation'
image = ImageI()
image.name = rstring('An image')
image = gateway.getUpdateService().saveAndReturnObject(image)
image = gateway.getObject('Image', image.id.val)
try:
f = NamedTemporaryFile()
fileText = "testFileAnnotationSpeed text"
f.write(fileText)
# use the same file to create many file annotations
for i in range(count):
fileAnn = gateway.createFileAnnfromLocalFile(
f.name, mimetype='text/plain', ns=ns)
image.linkAnnotation(fileAnn)
finally:
f.close()
perform_queries(gateway, image)
def test_comment_annotation_speed(gateway, count):
""" Tests speed of loading comment annotations. See PR: 4176 """
print 'Testing (%d) comment annotation speed...' % count
ns = 'omero.gateway.test_annotation'
image = ImageI()
image.name = rstring('An image')
for i in range(count):
annotation = CommentAnnotationI()
annotation.ns = rstring(ns)
annotation.textValue = rstring('Some value!')
image.linkAnnotation(annotation)
image = gateway.getUpdateService().saveAndReturnObject(image)
image = gateway.getObject('Image', image.id.val)
perform_queries(gateway, image)
def main():
host, port, username, password, count = sys.argv[1:]
port = int(port)
count = int(count)
gw = BlitzGateway(
host=host, port=port, username=username, passwd=password
)
gw.connect()
test_comment_annotation_speed(gw, count)
test_file_annotation_speed(gw, count)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment