Pillow/Tests/test_core_resources.py

182 lines
6.2 KiB
Python
Raw Normal View History

2017-09-18 02:37:47 +03:00
from __future__ import division, print_function
2017-09-18 03:17:45 +03:00
import sys
2017-09-18 02:51:27 +03:00
2017-09-18 02:37:47 +03:00
from PIL import Image
2019-09-25 12:46:54 +03:00
from .helper import PillowTestCase, is_pypy, unittest
2017-09-18 03:17:45 +03:00
2017-09-18 02:37:47 +03:00
class TestCoreStats(PillowTestCase):
def test_get_stats(self):
# Create at least one image
2019-06-13 18:53:42 +03:00
Image.new("RGB", (10, 10))
2017-09-18 02:37:47 +03:00
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertIn("new_count", stats)
self.assertIn("reused_blocks", stats)
self.assertIn("freed_blocks", stats)
self.assertIn("allocated_blocks", stats)
self.assertIn("reallocated_blocks", stats)
self.assertIn("blocks_cached", stats)
2017-09-18 02:37:47 +03:00
def test_reset_stats(self):
Image.core.reset_stats()
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertEqual(stats["new_count"], 0)
self.assertEqual(stats["reused_blocks"], 0)
self.assertEqual(stats["freed_blocks"], 0)
self.assertEqual(stats["allocated_blocks"], 0)
self.assertEqual(stats["reallocated_blocks"], 0)
self.assertEqual(stats["blocks_cached"], 0)
2017-09-18 02:37:47 +03:00
class TestCoreMemory(PillowTestCase):
def tearDown(self):
# Restore default values
Image.core.set_alignment(1)
2019-06-13 18:53:42 +03:00
Image.core.set_block_size(1024 * 1024)
2017-09-18 02:37:47 +03:00
Image.core.set_blocks_max(0)
Image.core.clear_cache()
def test_get_alignment(self):
alignment = Image.core.get_alignment()
self.assertGreater(alignment, 0)
def test_set_alignment(self):
for i in [1, 2, 4, 8, 16, 32]:
Image.core.set_alignment(i)
alignment = Image.core.get_alignment()
self.assertEqual(alignment, i)
# Try to construct new image
2019-06-13 18:53:42 +03:00
Image.new("RGB", (10, 10))
2017-09-18 02:37:47 +03:00
self.assertRaises(ValueError, Image.core.set_alignment, 0)
self.assertRaises(ValueError, Image.core.set_alignment, -1)
self.assertRaises(ValueError, Image.core.set_alignment, 3)
def test_get_block_size(self):
block_size = Image.core.get_block_size()
self.assertGreaterEqual(block_size, 4096)
def test_set_block_size(self):
2019-06-13 18:53:42 +03:00
for i in [4096, 2 * 4096, 3 * 4096]:
2017-09-18 02:37:47 +03:00
Image.core.set_block_size(i)
block_size = Image.core.get_block_size()
self.assertEqual(block_size, i)
# Try to construct new image
2019-06-13 18:53:42 +03:00
Image.new("RGB", (10, 10))
2017-09-18 02:37:47 +03:00
self.assertRaises(ValueError, Image.core.set_block_size, 0)
self.assertRaises(ValueError, Image.core.set_block_size, -1)
self.assertRaises(ValueError, Image.core.set_block_size, 4000)
def test_set_block_size_stats(self):
Image.core.reset_stats()
Image.core.set_blocks_max(0)
Image.core.set_block_size(4096)
2019-06-13 18:53:42 +03:00
Image.new("RGB", (256, 256))
2017-09-18 02:37:47 +03:00
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["new_count"], 1)
self.assertGreaterEqual(stats["allocated_blocks"], 64)
2019-09-25 12:46:54 +03:00
if not is_pypy():
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["freed_blocks"], 64)
2017-09-18 02:37:47 +03:00
def test_get_blocks_max(self):
blocks_max = Image.core.get_blocks_max()
self.assertGreaterEqual(blocks_max, 0)
def test_set_blocks_max(self):
for i in [0, 1, 10]:
Image.core.set_blocks_max(i)
blocks_max = Image.core.get_blocks_max()
self.assertEqual(blocks_max, i)
# Try to construct new image
2019-06-13 18:53:42 +03:00
Image.new("RGB", (10, 10))
2017-09-18 02:37:47 +03:00
self.assertRaises(ValueError, Image.core.set_blocks_max, -1)
2019-06-26 12:09:02 +03:00
if sys.maxsize < 2 ** 32:
self.assertRaises(ValueError, Image.core.set_blocks_max, 2 ** 29)
2017-09-18 02:37:47 +03:00
2019-09-25 12:46:54 +03:00
@unittest.skipIf(is_pypy(), "images are not collected")
2017-09-18 02:37:47 +03:00
def test_set_blocks_max_stats(self):
Image.core.reset_stats()
Image.core.set_blocks_max(128)
Image.core.set_block_size(4096)
2019-06-13 18:53:42 +03:00
Image.new("RGB", (256, 256))
Image.new("RGB", (256, 256))
2017-09-18 02:37:47 +03:00
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["new_count"], 2)
self.assertGreaterEqual(stats["allocated_blocks"], 64)
self.assertGreaterEqual(stats["reused_blocks"], 64)
self.assertEqual(stats["freed_blocks"], 0)
self.assertEqual(stats["blocks_cached"], 64)
2017-09-18 02:37:47 +03:00
2019-09-25 12:46:54 +03:00
@unittest.skipIf(is_pypy(), "images are not collected")
2017-09-18 02:37:47 +03:00
def test_clear_cache_stats(self):
Image.core.reset_stats()
2017-09-18 22:41:28 +03:00
Image.core.clear_cache()
2017-09-18 02:37:47 +03:00
Image.core.set_blocks_max(128)
Image.core.set_block_size(4096)
2019-06-13 18:53:42 +03:00
Image.new("RGB", (256, 256))
Image.new("RGB", (256, 256))
2017-09-23 04:20:53 +03:00
# Keep 16 blocks in cache
Image.core.clear_cache(16)
2017-09-18 02:37:47 +03:00
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["new_count"], 2)
self.assertGreaterEqual(stats["allocated_blocks"], 64)
self.assertGreaterEqual(stats["reused_blocks"], 64)
self.assertGreaterEqual(stats["freed_blocks"], 48)
self.assertEqual(stats["blocks_cached"], 16)
2017-09-18 02:57:14 +03:00
def test_large_images(self):
Image.core.reset_stats()
Image.core.set_blocks_max(0)
Image.core.set_block_size(4096)
2019-06-13 18:53:42 +03:00
Image.new("RGB", (2048, 16))
2017-09-18 02:57:14 +03:00
Image.core.clear_cache()
stats = Image.core.get_stats()
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["new_count"], 1)
self.assertGreaterEqual(stats["allocated_blocks"], 16)
self.assertGreaterEqual(stats["reused_blocks"], 0)
self.assertEqual(stats["blocks_cached"], 0)
2019-09-25 12:46:54 +03:00
if not is_pypy():
2019-06-13 18:53:42 +03:00
self.assertGreaterEqual(stats["freed_blocks"], 16)
2017-09-19 01:00:18 +03:00
class TestEnvVars(PillowTestCase):
def tearDown(self):
# Restore default values
Image.core.set_alignment(1)
2019-06-13 18:53:42 +03:00
Image.core.set_block_size(1024 * 1024)
2017-09-19 01:00:18 +03:00
Image.core.set_blocks_max(0)
Image.core.clear_cache()
def test_units(self):
2019-06-13 18:53:42 +03:00
Image._apply_env_variables({"PILLOW_BLOCKS_MAX": "2K"})
self.assertEqual(Image.core.get_blocks_max(), 2 * 1024)
Image._apply_env_variables({"PILLOW_BLOCK_SIZE": "2m"})
self.assertEqual(Image.core.get_block_size(), 2 * 1024 * 1024)
2017-09-19 01:00:18 +03:00
def test_warnings(self):
self.assert_warning(
2019-06-13 18:53:42 +03:00
UserWarning, Image._apply_env_variables, {"PILLOW_ALIGNMENT": "15"}
)
2017-09-19 01:00:18 +03:00
self.assert_warning(
2019-06-13 18:53:42 +03:00
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCK_SIZE": "1024"}
)
2017-09-19 01:00:18 +03:00
self.assert_warning(
2019-06-13 18:53:42 +03:00
UserWarning, Image._apply_env_variables, {"PILLOW_BLOCKS_MAX": "wat"}
)