<redis in action>5.2.3 simplifying our statistics recording and discovery

Source: Internet
Author: User

Original: https://redislabs.com/ebook/redis-in-action/part-2-core-concepts-2/ chapter-5-using-redis-for-application-support/5-2-counters-and-statistics/5-2-3- Simplifying-our-statistics-recording-and-discovery

Now that we have the statistics stored in Redis, what do we do next? Specifically, we now have access time (for example) information about not a single page, so how do we find out which pages take a long time to generate? Or how do we know that it took longer than the last time it was generated? The simple answer is that when the above scenario occurs, we need to use some way to store more information to let us discover the problem, which is what we sectionto explain.

If we want to record the access time, we need to calculate the access time. We'll take the time to add the calculation of the access time to each place, then add code to save the access time, or implement something to help us calculate and store the access time. The same assistants can also make the information available, (for example) the slowest-accessed page information stored in the Zset, or even a report that takes more time to generate than the previous page.

To help us calculate and store access time, we need to write a Python context manager to wrap the code for our compute and storage access time, which will get the current time, execute the wrapper code, then calculate the total execution time, log to Redis , and then update the Zset with a maximum access time. The following code listing shows how our context manager performs this set of operations.

#代码来源https://github.com/huangz1990/riacn-code/blob/master/ch05_listing_source.py#l283
@contextlib. ContextManagerdefAccess_time (conn, context):#records the time before code blocks are executed. Start =time.time ()#run the block of code that is wrapped. yield #calculates the length of time the code block executes. Delta = time.time ()-Start#Update the statistics for this context. Stats = update_stats (conn, context,'Accesstime', Delta)#calculates the average length of time the page is accessed. Average = stats[1]/Stats[0] Pipe=conn.pipeline (True)#adds a page's average access time to an ordered collection of the slowest access times recorded. Pipe.zadd ('Slowest:accesstime', context, average)#Accesstime ordered collections will only retain the slowest 100 records. Pipe.zremrangebyrank ('Slowest:accesstime', 0,-101) Pipe.execute ()

There are some magical things in access_time () context Manager that help us understand what's going on in the process of use. The following code shows that Access_time () context manager is used to record Web access times using a middleware or plug-in similar to the one used in Chapter two:

def Process_view (Conn, callback):                  # This is how the context manager, which computes and records the length of the access, surrounds the block of code. with     access_time (conn, request.path):             #  This statement is executed when the yield statement in the context manager is executed.         return callback ()     

After reading this example, you know at least how to use it, even if you don't know how to create a context manager. In this example, we used a Access_time context manager to calculate the total time to generate a page. This context manager can also be used to record database query times or render a template time. As an exercise, can you think of another useful context manager to record statistics? Or can you add access time reports that exceed two standard deviations to Recent_log ()?

Context Manager

<redis in action>5.2.3 simplifying our statistics recording and discovery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.