| 
 | 1 | +--TEST--  | 
 | 2 | +Memcached compression test  | 
 | 3 | +--SKIPIF--  | 
 | 4 | +<?php if (!extension_loaded("memcached")) print "skip"; ?>  | 
 | 5 | +--FILE--  | 
 | 6 | +<?php  | 
 | 7 | +include dirname (__FILE__) . '/config.inc';  | 
 | 8 | +$m = memc_get_instance ();  | 
 | 9 | + | 
 | 10 | +$short_data = "abcdefg";  | 
 | 11 | +$data = file_get_contents(dirname(__FILE__) . '/testdata.res');  | 
 | 12 | + | 
 | 13 | +set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {  | 
 | 14 | +	echo "$errstr\n";  | 
 | 15 | +	return true;  | 
 | 16 | +}, E_WARNING);  | 
 | 17 | + | 
 | 18 | +function get_compression($name) {  | 
 | 19 | +	switch (strtolower($name)) {  | 
 | 20 | +		case 'zlib':  | 
 | 21 | +			return Memcached::COMPRESSION_ZLIB;  | 
 | 22 | +		case 'fastlz':  | 
 | 23 | +			return Memcached::COMPRESSION_FASTLZ;  | 
 | 24 | +		default:  | 
 | 25 | +			echo "Strange compression type: $name\n";  | 
 | 26 | +			return 0;  | 
 | 27 | +	}  | 
 | 28 | +}  | 
 | 29 | + | 
 | 30 | +function fetch_with_compression($m, $key, $value, $set_compression = '', $factor = 1.3, $threshold = 2000) {  | 
 | 31 | + 	ini_set("memcached.compression_factor", $factor);  | 
 | 32 | + 	ini_set("memcached.compression_threshold", $threshold);  | 
 | 33 | +	  | 
 | 34 | +	$len=strlen($value);  | 
 | 35 | +	echo "len=[$len] set=[$set_compression] factor=[$factor] threshold=[$threshold]\n";  | 
 | 36 | +	  | 
 | 37 | +	if (!$set_compression) {  | 
 | 38 | +		$m->setOption(Memcached::OPT_COMPRESSION, false);  | 
 | 39 | +	} else {  | 
 | 40 | +		$m->setOption(Memcached::OPT_COMPRESSION, true);  | 
 | 41 | +		$m->setOption(Memcached::OPT_COMPRESSION_TYPE, get_compression($set_compression));  | 
 | 42 | +	}  | 
 | 43 | +	  | 
 | 44 | +	$m->set($key, $value, 1800);  | 
 | 45 | +	  | 
 | 46 | +	$value_back = $m->get($key);  | 
 | 47 | +	var_dump($value === $value_back);  | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +fetch_with_compression($m, 'hello01', $data, 'zlib',         1.3, 4);  | 
 | 51 | +fetch_with_compression($m, 'hello02', $data, 'fastlz',       1.3, 4);  | 
 | 52 | +fetch_with_compression($m, 'hello03', $data, '',             1.3, 4);  | 
 | 53 | +fetch_with_compression($m, 'hello04', $short_data, 'zlib',   1.3, 4);  | 
 | 54 | +fetch_with_compression($m, 'hello05', $short_data, 'fastlz', 1.3, 4);  | 
 | 55 | +fetch_with_compression($m, 'hello06', $short_data, '',       1.3, 4);  | 
 | 56 | +fetch_with_compression($m, 'hello11', $data, 'zlib',         0.3, 4);  | 
 | 57 | +fetch_with_compression($m, 'hello12', $data, 'fastlz',       0.3, 4);  | 
 | 58 | +fetch_with_compression($m, 'hello13', $data, '',             0.3, 4);  | 
 | 59 | +fetch_with_compression($m, 'hello14', $short_data, 'zlib',   0.3, 4);  | 
 | 60 | +fetch_with_compression($m, 'hello15', $short_data, 'fastlz', 0.3, 4);  | 
 | 61 | +fetch_with_compression($m, 'hello16', $short_data, '',       0.3, 4);  | 
 | 62 | +fetch_with_compression($m, 'hello21', $data, 'zlib',         1.3, 2000);  | 
 | 63 | +fetch_with_compression($m, 'hello22', $data, 'fastlz',       1.3, 2000);  | 
 | 64 | +fetch_with_compression($m, 'hello23', $data, '',             1.3, 2000);  | 
 | 65 | +fetch_with_compression($m, 'hello24', $short_data, 'zlib',   1.3, 2000);  | 
 | 66 | +fetch_with_compression($m, 'hello25', $short_data, 'fastlz', 1.3, 2000);  | 
 | 67 | +fetch_with_compression($m, 'hello26', $short_data, '',       1.3, 2000);  | 
 | 68 | +fetch_with_compression($m, 'hello31', $data, 'zlib',         0.3, 2000);  | 
 | 69 | +fetch_with_compression($m, 'hello32', $data, 'fastlz',       0.3, 2000);  | 
 | 70 | +fetch_with_compression($m, 'hello33', $data, '',             0.3, 2000);  | 
 | 71 | +fetch_with_compression($m, 'hello34', $short_data, 'zlib',   0.3, 2000);  | 
 | 72 | +fetch_with_compression($m, 'hello35', $short_data, 'fastlz', 0.3, 2000);  | 
 | 73 | +fetch_with_compression($m, 'hello36', $short_data, '',       0.3, 2000);  | 
 | 74 | +?>  | 
 | 75 | +--EXPECT--  | 
 | 76 | +len=[4877] set=[zlib] factor=[1.3] threshold=[4]  | 
 | 77 | +bool(true)  | 
 | 78 | +len=[4877] set=[fastlz] factor=[1.3] threshold=[4]  | 
 | 79 | +bool(true)  | 
 | 80 | +len=[4877] set=[] factor=[1.3] threshold=[4]  | 
 | 81 | +bool(true)  | 
 | 82 | +len=[7] set=[zlib] factor=[1.3] threshold=[4]  | 
 | 83 | +Memcached::set(): could not compress value  | 
 | 84 | +bool(true)  | 
 | 85 | +len=[7] set=[fastlz] factor=[1.3] threshold=[4]  | 
 | 86 | +bool(true)  | 
 | 87 | +len=[7] set=[] factor=[1.3] threshold=[4]  | 
 | 88 | +bool(true)  | 
 | 89 | +len=[4877] set=[zlib] factor=[0.3] threshold=[4]  | 
 | 90 | +bool(true)  | 
 | 91 | +len=[4877] set=[fastlz] factor=[0.3] threshold=[4]  | 
 | 92 | +bool(true)  | 
 | 93 | +len=[4877] set=[] factor=[0.3] threshold=[4]  | 
 | 94 | +bool(true)  | 
 | 95 | +len=[7] set=[zlib] factor=[0.3] threshold=[4]  | 
 | 96 | +Memcached::set(): could not compress value  | 
 | 97 | +bool(true)  | 
 | 98 | +len=[7] set=[fastlz] factor=[0.3] threshold=[4]  | 
 | 99 | +bool(true)  | 
 | 100 | +len=[7] set=[] factor=[0.3] threshold=[4]  | 
 | 101 | +bool(true)  | 
 | 102 | +len=[4877] set=[zlib] factor=[1.3] threshold=[2000]  | 
 | 103 | +bool(true)  | 
 | 104 | +len=[4877] set=[fastlz] factor=[1.3] threshold=[2000]  | 
 | 105 | +bool(true)  | 
 | 106 | +len=[4877] set=[] factor=[1.3] threshold=[2000]  | 
 | 107 | +bool(true)  | 
 | 108 | +len=[7] set=[zlib] factor=[1.3] threshold=[2000]  | 
 | 109 | +bool(true)  | 
 | 110 | +len=[7] set=[fastlz] factor=[1.3] threshold=[2000]  | 
 | 111 | +bool(true)  | 
 | 112 | +len=[7] set=[] factor=[1.3] threshold=[2000]  | 
 | 113 | +bool(true)  | 
 | 114 | +len=[4877] set=[zlib] factor=[0.3] threshold=[2000]  | 
 | 115 | +bool(true)  | 
 | 116 | +len=[4877] set=[fastlz] factor=[0.3] threshold=[2000]  | 
 | 117 | +bool(true)  | 
 | 118 | +len=[4877] set=[] factor=[0.3] threshold=[2000]  | 
 | 119 | +bool(true)  | 
 | 120 | +len=[7] set=[zlib] factor=[0.3] threshold=[2000]  | 
 | 121 | +bool(true)  | 
 | 122 | +len=[7] set=[fastlz] factor=[0.3] threshold=[2000]  | 
 | 123 | +bool(true)  | 
 | 124 | +len=[7] set=[] factor=[0.3] threshold=[2000]  | 
 | 125 | +bool(true)  | 
0 commit comments