PHP Classes

File: test/messageLoader.php

Recommend this page to a friend!
  Classes of Kjell-Inge Gustafsson   MesQ PHP Queue Management System   test/messageLoader.php   Download  
File: test/messageLoader.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MesQ PHP Queue Management System
Queue and pull messages store in directory files
Author: By
Last change: 1.05
Date: 3 years ago
Size: 2,043 bytes
 

Contents

Class file image Download
<?php
/**
 * MesQ, PHP disk based message lite queue manager
 *
 * Copyright 2021 Kjell-Inge Gustafsson, kigkonsult, All rights reserved
 * Link https://kigkonsult.se
 * Package MesQ
 * Version 1.05
 * License LGPL
 *
 * This file is a part of MesQ.
 *
 * This php script generate test messages
 * messageLoader.php
 *
 * Produce 4 kB+ messages, TestMessage class instances
 *
 * Usage
 * php -f /path/to/MesQ/test/messageLoader.php arg1 arg2 arg3
 *
 * arguments :
 * 0 : '/path/to/MesQ/test/messageLoader.php'
 * 1 : queueName
 * 2 : directory
 * 3 : startIndex
 * 4 : number of messages to generate
 * 5 : queue type, FIFO default
 */
declare( strict_types = 1 );
namespace
Kigkonsult\MesQ;

use function
in_array;
use function
getmypid;
use function
intval;
use function
microtime;
use function
rand;
use function
realpath;
use function
sprintf;

include
realpath( '../autoload.php' );
include
realpath( './test.inc.php' );

static
$FMT1 = 'pid %d %s : message %s%s';
static
$FMT2 = 'pid %d : created %d messages in %s sec%s';

// load args
list( $queueName, $directory ) = getArgv1and2( $argv );
$start = isArgSet( $argv, 3 ) ? intval( $argv[2] ) : 3;
$count = isArgSet( $argv, 4 ) ? intval( $argv[4] ) : 1000;
$queueType = ( isArgSet( $argv, 5 ) &&
   
in_array( $argv[5], [ MesQ::FIFO, MesQ::LIFO, MesQ::PRIO ] ))
    ?
$argv[5]
    :
MesQ::FIFO;
// set up
$time = microtime( true );
$pid = getmypid();
$payload = generateRandomString( 4096 ); // 2048 );
$mesq = MesQ::factory( $queueName, $directory )
    ->
setQueueType( $queueType );
echo
$mesq->configToString() . PHP_EOL;
$prio = null;
// load !!
for( $x1 = 1; $x1 <= $count; $x1++ ) {
   
$testMsg = TestMessage::factory( $start++, $payload );
    if(
MesQ::PRIO == $queueType ) {
       
$prio = rand( 0, 9 );
       
$testMsg->setPriority( $prio );
    }
   
$mesq->push( $testMsg, $prio );
    echo
sprintf( $FMT1, $pid, getTime( $time ), $testMsg->ToString(), PHP_EOL );
}
// end for
echo sprintf( $FMT2, $pid, $count, getTime( $time ), PHP_EOL );