• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Using the Counters Object (2)

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 13次 | 进入软件测试论坛讨论

领测软件测试网 In Part 1 we looked at the Counters object and what purpose it serves; we also examined its four methods. In this part we‘’re going to look at the Counters object in a bit more depth and look at some code to track the popularity of various search terms and code to display the values of all of the counters!

Persisting Counter Data:
The Counters object persists its various counters‘’ information. That is, even if the Web server is rebooted the value in these counters will not be reset back to zero. This is possible because the data for each counter is stored in a text file, counters.txt. This file will most likely be located in \WINNT\system32\ or \WINNT\system32\.netsrv\Data\. The file‘’s structure is pretty straight foward. Each counter that you create has its own line in the text file. On each line the name of the counter comes first, followed by a colon, followed by the value of the counter. For example:


HomePage:1935
SomeOtherPage:234
Product Queries:45
Advertisement Displays:35221

Therefore, if we wanted to display the values of all of our counters in an ASP page we could use some very simple FileSystemObject code to open this file and read the contents of each line, displaying the name of the counter and its value. The function presented below can be cut and pasted into your application to list the contents of each counter.

Function DisplayCounters()
  ‘’The full physical filename of the counters text file
  Const strFileName = "C:\WINNT\system32\inetsrv\Data\counters.txt"

  Dim objFSO, objTS
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  Set objTS = objFSO.OpenTextFile(strFileName)

  ‘’Read in the lines one at a time
  Dim strLine
  Do While Not objTS.AtEndOfStream

    ‘’Read in the line from the file
    strLine = objTS.ReadLine()
    
    ‘’Display the counter name and value
    Response.Write split(strLine,":")(0) & " - " & _
                   FormatNumber(split(strLine, ":")(1), 0) & "<br>"

  Loop

  ‘’Clean up...
  objTS.Close
  Set objTS = Nothing
  Set objFSO = Nothing
End Function




With a little more work you could read these values into a two-dimensional array and then sort the array in descending order by the counter values. Then you could selectively display, say, the top ten counters or whatnot. If this interests you be sure to check out: Sorting a Two-Dimensional Array with BubbleSort.

Tracking the Popularity of Various Search Terms:
One very useful application for the Counters object would be to track the popularity of various search terms entered by your users. For example, if you have a search engine on your site (like the search engine here at 4Guys) you may be curious what search keywords are the most popular. To track this metric, all you would need to do is add the following code to the search page:

  ...

  ‘’This assumes that the user‘’s search term was passed through the
  ‘’querystring as "SearchTerm"
  Dim strSearchTerm
  strSearchTerm = Request.QueryString("SearchTerm")

  ‘’If the user entered a search term than increment the counter  
  If Len(strSearchTerm) > 0 then
    objCounter.Increment(strSearchTerm)
  End If
  
  ...




Then, in some reporting screen, you can use the DisplayCounters() function to list the various search terms and their associated popularity.

Conclusion:
This wraps up our examination of the Counters object. The Counters object is a more powerful version of the PageCounter object, capable of providing multiple counters across your entire Web site. 

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网