Exemple #1
0
def bench_cache_lifetime(minutes):
    "Attempts to find how long a given memcached key can be expected to live"

    from pylons import g
    from r2.lib.cache import PyMemcache as Memcache

    # we'll create an independent connection to memcached for this
    # test
    mc = Memcache(g.memcaches)

    # set N keys, and tell them not to live for longer than this test
    mc.set_multi(dict(('bench_cache_%d' % x, x) for x in xrange(minutes)),
                 time=minutes * 60)

    # and every minute, check to see that the keys are still present,
    # until we find one missing
    for x in xrange(minutes):
        if mc.get('bench_cache_%d' % x, None) is not None:
            sleep(60)
        else:
            # we found one missing
            return x - 1
    else:
        # we're out of numbers, and we didn't find any missing
        # keys. Since we only set N keys, we can't check for anything
        # else
        print(("Cache lifetime is greater than %d minutes. Try again with a" +
               " higher 'minutes' value") % minutes)
        return None
Exemple #2
0
def bench_cache_lifetime(minutes):
    "Attempts to find how long a given memcached key can be expected to live"

    from pylons import g
    from r2.lib.cache import PyMemcache as Memcache

    # we'll create an independent connection to memcached for this
    # test
    mc = Memcache(g.memcaches)

    # set N keys, and tell them not to live for longer than this test
    mc.set_multi(dict( ('bench_cache_%d' % x, x)
                       for x in xrange(minutes) ),
                 time=minutes*60)

    # and every minute, check to see that the keys are still present,
    # until we find one missing
    for x in xrange(minutes):
        if mc.get('bench_cache_%d' % x, None) is not None:
            sleep(60)
        else:
            # we found one missing
            return x-1
    else:
        # we're out of numbers, and we didn't find any missing
        # keys. Since we only set N keys, we can't check for anything
        # else
        print (("Cache lifetime is greater than %d minutes. Try again with a"+
                " higher 'minutes' value") % minutes)
        return None
Exemple #3
0
def monitor_cache_lifetime(minutes,
                           retest=10,
                           ntest=-1,
                           cache_name="memcaches",
                           verbose=False):

    # list of list of active memcache test keys
    keys = []
    period = 60  # 1 minute cycle time
    data = DataLogger()

    # we'll create an independent connection to memcached for this test
    mc = Memcache(getattr(g, cache_name))

    counter = 0
    while ntest:

        if counter == 0 or (retest and counter % retest == 0):
            randstr = random.random()
            newkeys = [("%s_lifetime_%s_%d" % (cache_name, randstr, x), x + 1)
                       for x in xrange(minutes)]

            # set N keys, and tell them not to live for longer than this test
            mc.set_multi(
                dict(newkeys),
                #time = minutes * period)
                time=(minutes + 1) * period)

            # add the list in reverse order since we'll be poping.
            newkeys.reverse()
            keys.append(newkeys)

        # wait for the next key to (potentially) expire
        counter += 1
        time.sleep(period)

        for k in keys:
            key, age = k.pop()
            if mc.get(key) is None or k == []:
                if verbose:
                    print "cache expiration: %d seconds" % (period * age)
                data.add(period * age)
                AppServiceMonitor.set_cache_lifetime(data, key=cache_name)
                # wipe out the list for removal by the subsequent filter
                while k:
                    k.pop()

        # clear out any empty key lists
        if [] in keys:
            keys = filter(None, keys)
            ntest -= 1
Exemple #4
0
def monitor_cache_lifetime(minutes, retest = 10, ntest = -1,
                           cache_name = "memcaches", verbose = False):

    # list of list of active memcache test keys
    keys = []
    period = 60  # 1 minute cycle time
    data = DataLogger()
    
    # we'll create an independent connection to memcached for this test
    mc = Memcache(getattr(g, cache_name))

    counter = 0
    while ntest:

        if counter == 0 or (retest and counter % retest == 0):
            randstr = random.random()
            newkeys = [("%s_lifetime_%s_%d" % (cache_name, randstr, x), x+1)
                       for x in xrange(minutes)]

            # set N keys, and tell them not to live for longer than this test
            mc.set_multi(dict(newkeys),
                         #time = minutes * period)
                         time = (minutes+1) * period)

            # add the list in reverse order since we'll be poping.
            newkeys.reverse()
            keys.append(newkeys)

        # wait for the next key to (potentially) expire
        counter += 1
        time.sleep(period)

        for k in keys:
            key, age = k.pop()
            if mc.get(key) is None or k == []:
                if verbose:
                    print "cache expiration: %d seconds" % (period * age)
                data.add(period * age)
                AppServiceMonitor.set_cache_lifetime(data, key = cache_name)
                # wipe out the list for removal by the subsequent filter
                while k: k.pop()

        # clear out any empty key lists
        if [] in keys:
            keys = filter(None, keys)
            ntest -= 1