Last active
April 22, 2022 11:56
-
-
Save SerafimArts/4c8cd05f20384551a65e3a2958557028 to your computer and use it in GitHub Desktop.
Shared Memory Benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"autoload": { | |
"psr-4": { | |
"Bench\\": "bench" | |
} | |
}, | |
"require": { | |
"phpbench/phpbench": "^1.0" | |
}, | |
"scripts": { | |
"run": "phpbench run bench --report=aggregate" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHPBench (1.2.0) running benchmarks... | |
with PHP version 8.1.4, xdebug ❌, opcache ✔ | |
\Bench\WriteBench | |
benchShm................................I4 - Mo0.338μs (±3.09%) | |
benchShmop..............................I4 - Mo0.174μs (±2.25%) | |
benchSync...............................I4 - Mo0.158μs (±1.51%) | |
benchFFI................................I4 - Mo0.154μs (±1.41%) | |
Subjects: 4, Assertions: 0, Failures: 0, Errors: 0 | |
+------------+------------+-----+--------+-----+-----------+---------+--------+ | |
| benchmark | subject | set | revs | its | mem_peak | mode | rstdev | | |
+------------+------------+-----+--------+-----+-----------+---------+--------+ | |
| WriteBench | benchShm | | 100000 | 5 | 472.232kb | 0.338μs | ±3.09% | | |
| WriteBench | benchShmop | | 100000 | 5 | 472.232kb | 0.174μs | ±2.25% | | |
| WriteBench | benchSync | | 100000 | 5 | 472.232kb | 0.158μs | ±1.51% | | |
| WriteBench | benchFFI | | 100000 | 5 | 472.232kb | 0.154μs | ±1.41% | | |
+------------+------------+-----+--------+-----+-----------+---------+--------+ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Bench; | |
use PhpBench\Attributes\BeforeMethods; | |
use PhpBench\Attributes\Iterations; | |
use PhpBench\Attributes\Revs; | |
use PhpBench\Attributes\Warmup; | |
#[Revs(100000), Warmup(2), Iterations(5)] | |
#[BeforeMethods('prepare')] | |
class WriteBench | |
{ | |
private const MEMORY_PERMISSIONS = 0o600; | |
private const MEMORY_SIZE = 1024; | |
private \SysvSharedMemory $shm; | |
private \Shmop $shmop; | |
private \SyncSharedMemory $sync; | |
private \FFI $ffi; | |
private \FFI\CData|int $addr; | |
private function randomId(): int | |
{ | |
$file = tempnam(sys_get_temp_dir(), 'shm_'); | |
return ftok($file, chr(random_int(1, 254))); | |
} | |
public function prepare(): void | |
{ | |
// Initialize SHM | |
$this->shm = \shm_attach($this->randomId(), self::MEMORY_SIZE, self::MEMORY_PERMISSIONS); | |
// Initialize Shmop | |
$this->shmop = \shmop_open($this->randomId(), 'c', self::MEMORY_PERMISSIONS, self::MEMORY_SIZE); | |
// Initialize Sync | |
$this->sync = new \SyncSharedMemory('example', self::MEMORY_SIZE); | |
// Initialize FFI | |
$this->ffi = \FFI::cdef(<<<'ANSI_C' | |
typedef struct shmid_ds { | |
struct ipc_perm { | |
int key; | |
unsigned int uid; | |
unsigned int gid; | |
unsigned int cuid; | |
unsigned int cgid; | |
unsigned int mode; | |
unsigned short seq; | |
} shm_perm; | |
int shm_segsz; | |
long shm_atime; | |
long shm_dtime; | |
long shm_ctime; | |
int shm_cpid; | |
int shm_lpid; | |
unsigned short shm_nattch; | |
unsigned short shm_unused; | |
void *shm_unused2; | |
void *shm_unused3; | |
} shmid_ds; | |
int shmget(int key, size_t size, int shmflg); | |
void *shmat(int shmid, const void *shmaddr, int shmflg); | |
int shmctl(int shmid, int cmd, struct shmid_ds *buf); | |
ANSI_C); | |
$shm = $this->ffi->new('shmid_ds'); | |
$id = $this->ffi->shmget($this->randomId(), self::MEMORY_SIZE, 1000); | |
$this->ffi->shmctl($id, 2, \FFI::addr($shm)); | |
$this->addr = $this->ffi->shmat($id, null, 0); | |
} | |
public function benchShm(): void | |
{ | |
\shm_put_var($this->shm, 0, 0xDEAD_BEEF); | |
\shm_put_var($this->shm, 1, 'ABCDEFGH'); | |
\shm_put_var($this->shm, 2, 0.42); | |
} | |
public function benchShmop(): void | |
{ | |
\shmop_write($this->shmop, pack('q', 0xDEAD_BEEF), 0); | |
\shmop_write($this->shmop, 'ABCDEFGH', 8); | |
\shmop_write($this->shmop, pack('d', 0.42), 16); | |
} | |
public function benchSync(): void | |
{ | |
$this->sync->write(pack('q', 0xDEAD_BEEF)); | |
$this->sync->write('ABCDEFGH', 8); | |
$this->sync->write(pack('d', 0.42), 16); | |
} | |
public function benchFFI(): void | |
{ | |
\FFI::memcpy($this->addr, pack('q', 0xDEAD_BEEF), 8); | |
\FFI::memcpy($this->addr + 8, 'ABCDEFGH', 8); | |
\FFI::memcpy($this->addr + 16, pack('d', 0.42), 8); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment