<?php
/**
 * @file
 * Web test cases for Cache Consistent.
 */

// Make sure the ConsistentCacheTestCaseBase class is available.
require_once dirname(__FILE__) . '/cache_consistent.test.inc';
require_once dirname(__FILE__) . '/cache_consistent.tests.inc';

/**
 * Class for speed test cases.
 */
class ConsistentCacheSpeedTestCase extends ConsistentCacheTestCaseBase {
  use CacheConsistentTestHelper;

  /**
   * Info for these test cases.
   */
  public static function getInfo() {
    return array(
      'name' => 'Cache Consistent Speed test',
      'description' => 'Test the speed of the consistent cache functionallity.',
      'group' => 'Cache Consistent',
    );
  }

  /**
   * Handle transaction callbacks to cache.
   */
  public function db_transaction($cache_object) {
    return new ConsistentCacheTestTransaction($cache_object);
  }

  /**
   * Run test cases on a database cache backend.
   */
  public function testDatabaseCache() {
    $this->speedTest1($this->cache_default, $this->max);
    $this->speedTest2($this->cache_default, $this->max);
    $this->speedTest3($this->cache_default, $this->max);
    $this->speedTest4($this->cache_default, $this->max);
  }

  /**
   * Run test cases on a database cache backend.
   */
  public function testMemoryCache() {
    $this->speedTest1($this->cache_memory, $this->max);
    $this->speedTest2($this->cache_memory, $this->max);
    $this->speedTest3($this->cache_memory, $this->max);
    $this->speedTest4($this->cache_memory, $this->max);
  }

  /**
   * Run test cases on a non-database cache backend.
   */
  public function testNondatabaseCache() {
    $this->speedTest1($this->cache_test, $this->max);
    $this->speedTest2($this->cache_test, $this->max);
    $this->speedTest3($this->cache_test, $this->max);
    $this->speedTest4($this->cache_test, $this->max);
  }

  /**
   * Run test cases on a non-database cache backend through Cache Consistent.
   */
  public function testConsistentCache() {
    $this->speedTest1($this->cache_consistent, $this->max);
    $this->speedTest2($this->cache_consistent, $this->max);
    $this->speedTest3($this->cache_consistent, $this->max);
    $this->speedTest4($this->cache_consistent, $this->max);
  }

  /**
   * Run test cases on a non-database cache backend through Cache Consistent.
   */
  public function testConsistentCacheMemory() {
    $this->speedTest1($this->cache_consistent_memory, $this->max);
    $this->speedTest2($this->cache_consistent_memory, $this->max);
    $this->speedTest3($this->cache_consistent_memory, $this->max);
    $this->speedTest4($this->cache_consistent_memory, $this->max);
  }

  /**
   * Test non-transactional set.
   */
  public function speedTest1($cache_object, $max) {
    $start = microtime(TRUE);
    for ($i = 1; $i <= $max; $i++) {
      $cache_object->set('test' . $i, 'value0');
    }
    $end = microtime(TRUE);

    $time = $end - $start;

    $this->assertTrue(TRUE, format_string('Speed test completed. Took @time seconds', array(
      '@time' => sprintf("%.08f", $time),
    )), $cache_object->testGroup);
  }

  /**
   * Test transactional set.
   */
  public function speedTest2($cache_object, $max) {
    $start = microtime(TRUE);
    $tx = $this->db_transaction($cache_object);
    for ($i = 1; $i <= $max; $i++) {
      $cache_object->set('test' . $i, 'value1');
    }
    unset($tx);
    $end = microtime(TRUE);

    $time = $end - $start;

    $this->assertTrue(TRUE, format_string('Speed test completed. Took @time seconds', array(
      '@time' => sprintf("%.08f", $time),
    )), $cache_object->testGroup);
  }

  /**
   * Test transactional clear/set/get.
   */
  public function speedTest3($cache_object, $max) {
    $start = microtime(TRUE);
    $tx = $this->db_transaction($cache_object);
    $cache_object->clear('test0', TRUE);
    for ($i = 1; $i <= $max; $i++) {
      $cache_object->set('test' . $i, 'value1');
    }
    for ($i = 1; $i <= $max; $i++) {
      $cache_object->get('test0');
    }
    unset($tx);
    $end = microtime(TRUE);

    $time = $end - $start;

    $this->assertTrue(TRUE, format_string('Speed test completed. Took @time seconds', array(
      '@time' => sprintf("%.08f", $time),
    )), $cache_object->testGroup);
  }

  /**
   * Test pruning efficiency.
   */
  public function speedTest4($cache_object, $max) {
    $start = microtime(TRUE);
    $cache_object->clear('test1', TRUE);
    $tx = $this->db_transaction($cache_object);
    for ($i = 1; $i <= $max; $i++) {
      $cache_object->set('test1', 'value' . $i);
    }
    unset($tx);
    $end = microtime(TRUE);

    $time = $end - $start;

    $cache = $cache_object->get('test1');
    $expect = 'value' . $max;

    $this->assertEqual($expect, $cache->data, format_string('Expect "@expect" in cache, found "@data", took @time seconds', array(
      '@expect' => $expect,
      '@data' => $cache->data,
      '@time' => sprintf("%.08f", $time),
    )), $cache_object->testGroup);
  }

}
