summaryrefslogtreecommitdiffstats
path: root/zstd.php
diff options
context:
space:
mode:
Diffstat (limited to 'zstd.php')
-rw-r--r--zstd.php66
1 files changed, 66 insertions, 0 deletions
diff --git a/zstd.php b/zstd.php
new file mode 100644
index 0000000..35c8876
--- /dev/null
+++ b/zstd.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * ZSTD compressor using FFI and libzstd
+ * PoC, only for documentation purpose
+ *
+ * Copyright (c) 2019 Remi Collet
+ * License: CC-BY-SA
+ * http://creativecommons.org/licenses/by-sa/4.0/
+ */
+
+if (PHP_VERSION_ID < 70400 || !extension_loaded("ffi")) {
+ die("PHP 7.4 with FFI required\n");
+}
+printf("PHP version %s\n", PHP_VERSION);
+
+if (PHP_SAPI == "cli" && !class_exists("\\Remi\\Zstd")) {
+ printf("Fallback on manual load\n\n");
+ require_once __DIR__ . '/preload-zstd.inc';
+} else {
+ printf("Use preloaded class\n\n");
+}
+if (class_exists("\\Remi\\Zstd")) {
+ $t = microtime(true);
+
+ $ret = \Remi\Zstd::compress(PHP_BINARY, "testffi.zstd");
+ printf("Src length = %d\n", $ret['in_len']);
+ printf("ZSTD_compressBound = %d\n", $ret['max_len']);
+ printf("ZSTD_compress = %d\n", $ret['out_len']);
+
+ $ret = \Remi\Zstd::decompress("testffi.zstd", "testffi.orig");
+ printf("Src length = %d\n", $ret['in_len']);
+ printf("ZSTD_decompressBound = %d\n", $ret['max_len']);
+ printf("ZSTD_decompress = %d\n", $ret['out_len']);
+
+ $t = microtime(true) - $t;
+ printf("Using FFI extension = %.3f\"\n\n", $t);
+} else {
+ printf("FFI missing\n\n");
+}
+
+if (extension_loaded("zstd")) {
+ $t = microtime(true);
+ $src = file_get_contents(PHP_BINARY);
+ $len = strlen($src);
+ printf("Src length = %d\n", $len);
+
+ $comp = zstd_compress($src, 6);
+ $clen = strlen($comp);
+ printf("ZSTD_compress = %d\n", $clen);
+ $comp = substr($comp, 0, $clen);
+ file_put_contents("testzstd.zstd", $comp);
+
+ $comp = file_get_contents("testzstd.zstd");
+ $unco = zstd_uncompress($comp);
+ $ulen = strlen($unco);
+ printf("ZSTD_decompress = %d\n", $ulen);
+ file_put_contents("testzstd.orig", $unco);
+
+ var_dump($src === $unco);
+ $t = microtime(true) - $t;
+ printf("Using ZSTD extension = %.3f\"\n\n", $t);
+} else {
+ printf("ZSTD missing\n\n");
+}
+
+