|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +# pytest --benchmark-enable --benchmark-timer=time.process_time tika/tests/test_benchmark.py |
| 19 | +# pytest --benchmark-enable --benchmark-timer=time.process_time tika/tests/test_benchmark.py |
| 20 | +import os |
| 21 | +import unittest |
| 22 | +import zlib |
| 23 | + |
| 24 | +import tika.parser |
| 25 | +import tika.tika |
| 26 | +from tika.tests.utils import HTTPStatusOk, gzip_compress |
| 27 | + |
| 28 | + |
| 29 | +def test_local_binary(benchmark): |
| 30 | + """parse file binary""" |
| 31 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 32 | + response = benchmark(tika_from_binary, file) |
| 33 | + |
| 34 | + assert response['status'] == HTTPStatusOk |
| 35 | + |
| 36 | + |
| 37 | +def test_parser_buffer(benchmark): |
| 38 | + """example how to send gzip file""" |
| 39 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 40 | + response = benchmark(tika_from_buffer, file) |
| 41 | + |
| 42 | + assert response['status'] == HTTPStatusOk |
| 43 | + |
| 44 | + |
| 45 | +def test_parser_buffer_zlib_input(benchmark): |
| 46 | + """example how to send gzip file""" |
| 47 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 48 | + |
| 49 | + response = benchmark(tika_from_buffer_zlib, file) |
| 50 | + |
| 51 | + assert response['status'] == HTTPStatusOk |
| 52 | + |
| 53 | + |
| 54 | +def test_parser_buffer_gzip_input(benchmark): |
| 55 | + """parse file binary""" |
| 56 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 57 | + response = benchmark(tika_from_buffer_gzip, file) |
| 58 | + |
| 59 | + assert response['status'] == HTTPStatusOk |
| 60 | + |
| 61 | + |
| 62 | +def test_local_binary_with_gzip_output(benchmark): |
| 63 | + """parse file binary""" |
| 64 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 65 | + response = benchmark(tika_from_binary, file, headers={'Accept-Encoding': 'gzip, deflate'}) |
| 66 | + |
| 67 | + assert response['status'] == HTTPStatusOk |
| 68 | + |
| 69 | + |
| 70 | +def test_parser_buffer_with_gzip_output(benchmark): |
| 71 | + """example how to send gzip file""" |
| 72 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 73 | + response = benchmark(tika_from_buffer, file, headers={'Accept-Encoding': 'gzip, deflate'}) |
| 74 | + |
| 75 | + assert response['status'] == HTTPStatusOk |
| 76 | + |
| 77 | + |
| 78 | +def test_parser_buffer_zlib_input_and_gzip_output(benchmark): |
| 79 | + """example how to send gzip file""" |
| 80 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 81 | + |
| 82 | + response = benchmark(tika_from_buffer_zlib, file, headers={'Accept-Encoding': 'gzip, deflate'}) |
| 83 | + |
| 84 | + assert response['status'] == HTTPStatusOk |
| 85 | + |
| 86 | + |
| 87 | +def test_parser_buffer_gzip_input_and_gzip_output(benchmark): |
| 88 | + """parse file binary""" |
| 89 | + file = os.path.join(os.path.dirname(__file__), 'files', 'rwservlet.pdf') |
| 90 | + response = benchmark(tika_from_buffer_gzip, file, headers={'Accept-Encoding': 'gzip, deflate'}) |
| 91 | + |
| 92 | + assert response['status'] == HTTPStatusOk |
| 93 | + |
| 94 | + |
| 95 | +def tika_from_buffer_zlib(file, headers=None): |
| 96 | + with open(file, 'rb') as file_obj: |
| 97 | + return tika.parser.from_buffer(zlib.compress(file_obj.read()), headers=headers) |
| 98 | + |
| 99 | + |
| 100 | +def tika_from_buffer_gzip(file, headers=None): |
| 101 | + with open(file, 'rb') as file_obj: |
| 102 | + return tika.parser.from_buffer(gzip_compress(file_obj.read()), headers=headers) |
| 103 | + |
| 104 | + |
| 105 | +def tika_from_buffer(file, headers=None): |
| 106 | + with open(file, 'rb') as file_obj: |
| 107 | + return tika.parser.from_buffer(file_obj.read(), headers=headers) |
| 108 | + |
| 109 | + |
| 110 | +def tika_from_binary(file, headers=None): |
| 111 | + with open(file, 'rb') as file_obj: |
| 112 | + return tika.parser.from_file(file_obj, headers=headers) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + unittest.main() |
0 commit comments