Inside MSXML Performance(MSXML性能分析) (5)

原创 2002年07月15日 14:53:00

Save<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

保存

Saving a document is generally slower than loading one. The following table summarizes the differences:

保存一个文档总的来说要比载入慢。下面的表格显示了其中的区别:

Sample
样本

Load (milliseconds)
载入(毫秒)

Save (milliseconds)
保存(毫秒)

Difference (percentage)
差别(百分比)

Ado.xml

677

1,441

113

Hamlet.xml

104

184

77

Ot.xml

1,063

1,971

85

Northwind.xml

62

103

66

When saving each sample, the worst case is the attribute-heavy ADO Recordset, which is more than twice as slow as loading it into memory. Given that it seems perfectly reasonable to expect a write operation to be slower than a read operation, these numbers are rather good. But be careful. You can easily strain the file system if you are loading and saving lots of small XML documents, as shown in the section "Free-Threaded Documents."

当保存每一个样本文件时,最糟糕的情况发生在属性比重大的ADO Recordset文件上,它写入时间比载入内存要慢两倍。由于一般认为写操作比读操作慢一些是很合理的,所以以上数字大家都可以接受。但是请注意,如果你载入并保存很多小的XML文档,文件系统很可能会受到影响,正如在“自由线程文档”将要讨论的那样。

Namespaces

名字空间

XML Namespaces also add some overhead in XML parsing time, but not as much as you might think. I have two versions of the Ado.xml example. One uses a namespace prefix "recordset:" on all 2,347 rows, and one does not. The following table shows the difference in load time:

XML名字空间也增加了一些XML解析时的开销,但是并不如你想象得那么多。这里有两个版本的Ado.xml示例。其中一个在2,347行中都使用了“recordset:”前缀,而另一个没有。下面的表格显示了它们在载入时间上的不同:

Measurement
度量

Ado.xml

Ado-ns.xml

Difference (percentage)
差别(百分比)

File size
文件大小

1,007,214

1,030,651

2.3

Load time (milliseconds)
载入时间(毫秒)

662

680

2.7

Most of the difference in load time can be explained by the increase in file size.

Free-Threaded Documents

自由线程文档

A "free-threaded" DOM document (CLSID_DOMFreeThreadedDocument, "Microsoft.FreeThreadedXMLDOM") exposes the same interface as the "rental" threaded document (IID_IXMLDOMDocument). This object can be safely shared across any thread in the same process.

自由线程DOM文档(CLSID_DOMFreeThreadedDocument, "Microsoft.FreeThreadedXMLDOM")提供了租用线程文档(IID_IXMLDOMDocument)相同的接口。这个对象可以安全的在同一进程中的任意线程间共享。

Free-threaded documents are generally slower than rental documents because of the extra thread safety work they do. You use them when you want to share a document among multiple threads at the same time, avoiding the need for each of those threads to load up their own copy. In some scenarios, this can result in a big win that outweighs the cost of the extra thread safety work.

自由线程文档一般比租用线程文档要慢,因为它要为线程安全性增加开销。如果你想要同时在多个线程中共享文档,可以使用它,以避免在每个线程都要载入自己的拷贝。在某些情况下,这会大大提高性能,即使在线程安全性上有额外的开销。

For example, suppose you have a 2-KB XML file on your Web server, and you have a simple ASP page that loads that file, increments an attribute inside the file, and saves the file again.

例如,假设在你的Web服务器上有一个2KBXML文件,还有一个简单的ASP页面要载入这个文件,在文件中加入一个属性,然后将它保存。

<%@ LANGUAGE=JSCRIPT %>

<%

Response.Expires = -1;

var filename = Server.MapPath("simple.xml");

Application.Lock();

var doc = Server.CreateObject("Microsoft.XMLDOM");

doc.async = false;

doc.load(filename);

var c = parseInt(doc.documentElement.getAttribute("count"))+1;

doc.documentElement.setAttribute("count",c);

doc.save(filename);

Application.UnLock();

%>

<%=c%>

This ASP code will be completely disk I/O bound. On my Pentium II 450-MHz dual-processor computer, I was not able to get any more than 50 percent CPU utilization. The disk was making a lot of noise.

这段ASP代码会极大的占用磁盘读写通道。在我的Pentium II 450-MHz双处理器的机器上,我不能得到50%以上的CPU使用率。

However, we could bring the file into shared-application state using a free-threaded DOM document, as follows:

但是,我们可以运用自由线程DOM文档将文件可以进行共享,代码如下:

<%@ LANGUAGE=JSCRIPT %>

<%

Response.Expires = -1;

var doc = Application("shared");

if (doc == null)

{

    doc = Server.CreateObject("Microsoft.FreeThreadedXMLDOM");

    doc.async = false;

    doc.load(Server.MapPath("simple.xml"));

    Application("shared") = doc;

}

Application.Lock();

var c = parseInt(doc.documentElement.getAttribute("count"))+1;

doc.documentElement.setAttribute("count",c);

Application.UnLock();

 

%>

<%=c%>

Then we would see the throughput jump dramatically:

这样,我们可以看到吞吐量剧烈的飞跃:

Method
方法

Requests/second
请求/

Load/Save
载入/保存

34

Shared
共享

250

In other words, this second approach using the free-threaded DOM document is seven times faster than the other!

可以说,使用了自由线程DOM文档的第二种方法比第一种方法快7倍。

Delayed Memory Cleanup

延迟的内存释放

The one downside to the free-threaded model is that it exhibits more latency in how it cleans up unused memory, affecting the performance of subsequent operations. (Some people report this as a memory leak when in fact it is just delayed cleanup.) The larger the XML document, the more pronounced the latency. The following table shows the increase in load time and working set when using the free-threaded document as opposed to the rental document:

自由线程模式的一个不足是它在释放不使用的内存空间时会有延迟,这影响了后续操作的性能。(有些人将这个现象报告为内存泄漏,事实上,它只是延迟释放内存而已。)XML文档越大,延迟就越显著。下面的表格中显示了使用自由线程同使用租用模式在载入时间和工作空间上的增量:

Sample
样本

Load time (percentage increase)
载入时间(增量百分比)

Working set (percentage increase)
工作空间(增量百分比)

Ado.xml

4

137

Hamlet.xml

23

83

Ot.xml

1

53

Northwind.xml

2

42

These results show that the worst case is when you have many nodes (as in Ado.xml), which makes sense because this generates more memory for the memory manager to clean up. Note that the benefits of being able to share the same document object across threads, as shown above, still outweigh the downside of slower load time and a larger working set.

上述结果表明最坏情况发生在有许多节点的文档上(就像在Ado.xml中的情况)。请注意,能够在线程间共享文档的益处,如上文所示,仍然超过它在载入时间和工作空间上的不利影响。

Virtual Memory

虚拟内存

In the free-threaded model, the working set can spike if you generate enormous amounts of memory for the memory manager to clean up—which can happen when performing XSL transformations on large documents. In this case, you will use up all available memory and strain the virtual memory manager, causing the performance to dramatically decrease. Watch the peak working set of your application under a heavy load to make sure this doesn't happen. If it does, redesign your application by breaking the XML down into smaller chunks. This situation has been improved somewhat in the MSXML January 2000 Web Release. In fact, we got the following reader comment on the January 2000 Web Release page:

在自由线程模式中,工作空间会大大增加,如果你产生了很多需要内存管理去释放的内存,这在进行大文档的XSL转换时会发生。在这种情况下,你会将可用的内存资源用尽,并频繁调用虚存管理器,性能也就急剧下降。你可以通过观察高负荷情况下应用程序的峰值工作空间来确定这种现象不会发生。如果发生了,可将XML拆开成小块,重新设计你的应用程序。这种情况在MSXML January 2000 Web Release中有了些改进。事实上,我们从中得到以下读者评论:

Faster!

更快!

I've done a couple of informal speed trials on server-side transforms (XML to HTML via XSL), and noticed that there's a big improvement.

我做了一些非正式的服务器端转换(通过XSLXML转换到HTML)速度测试,发现有了巨大的改进。

-         Anonymous 9-Feb-2000

-         匿名 2-92000

IDispatch

IDispatch

Late-bound scripting languages, such as JScript and VBScript, add a lot of overhead to each method call and property access in the DOM interface. The script engines actually invoke the methods and properties indirectly through the IDispatch interface. First, both script engines call GetIDsOfNames or GetDispID, which will pass in a string name for the method or property and return a DISPID. Then the engines package all the arguments into an array and call Invoke with the DISPID.

晚绑定(late-bound)脚本语言,如JScirptVBScirpt,在每次DOM接口的方法调用和属性访问时增加了许多开销。脚本引擎实际上是通过IDispatch接口间接调用方法和属性的。首先,脚本引擎调用GetIDsOfNames或者GetDispID,传入方法和属性名字符串,返回一个DISPID。然后引擎将所有的参数打包到一个数组并以DISPID调用Invoke

Need I say more? Clearly, this is going to be slower than calling a virtual function in C++ or compiled Visual Basic. Visual Basic is a bit tricky, though, because you can actually do both styles of programming in one application. For example:

需要更多信息?显然,这会比调用C++虚函数或编译的Visual Basic要慢。但是Visual Basic复杂一些,因为你可以在一个应用程序中使用两种编程方式。例如:

    Dim doc as Object

    set doc = CreateObject("Microsoft.XMLDOM")

    ...

This is late-bound and will be as slow as VBScript or JScript. To speed this up, from the Project menu, select References and add a reference to the latest version of the "Microsoft XML" library. Then you can write the following early-bound code:

这是晚绑定的,会和VBScriptJscript一样慢。为了提高速度,按Project菜单,选择References并加入最新版的“Microsoft XML”库。然后你可以写以下早绑定代码:

    Dim doc As New MSXML.DOMDocument

    ...

The other advantage of programming this way is that you'll get all sorts of helpful drop-down lists of available methods and their arguments from Visual Basic.

这种编程方式的另一个优点是你可以在Visual Basic中在下拉列表中得到各种方法名等帮助信息。

MSXML的不同版本使用

我参与了IE7的开发过程,看到了在IE浏览器中形形色色使用MSXML的方法。显然有一些东西困扰着开发者:MSXML“混乱”的版本以及如何创建“正确”的实例。下面是一段非常常见的代码: if (Web...
  • u011519892
  • u011519892
  • 2014年03月11日 19:41
  • 1968

XML在数据库应用中的性能问题初探

 1.        引 言XML技术自出现以来,发展非常迅速,在许多领域内得到广泛的支持而有着广阔的应用前景。例如电子数据交换、电子商务等等更是把XML作为一种基础性、支柱性的技术来看待。然而,应该...
  • annicybc
  • annicybc
  • 2006年06月27日 15:05
  • 789

XslCompiledTransform 性能

XslCompiledTransform Performance: Beating MSXML 4.0  From http://blogs.msdn.com/antosha/default.aspx...
  • ChengZi
  • ChengZi
  • 2006年10月24日 17:02
  • 1705

Inside MSXML Performance(MSXML性能分析)

Inside MSXML PerformanceMSXML性能分析Chris Lovett Microsoft CorporationFebruary 21, 2000Download the sou...
  • ayusay
  • ayusay
  • 2002年07月15日 09:47
  • 938

Inside MSXML Performance(MSXML性能分析) (2)

Metrics度量指标There are four key performance metrics that most of you are interested in as you develop ...
  • ayusay
  • ayusay
  • 2002年07月15日 09:48
  • 679

前端性能之非阻塞加载js脚本

SCRIPT标签的阻塞行为会对页面的性能产生影响,这是因为浏览器在下载脚本、解析、执行的过程中不会同时做其他事情,比如渲染页面、响应用户事件等。之所以这样做是因为正在执行的JavaScript代码可能...
  • u010487568
  • u010487568
  • 2015年07月31日 19:11
  • 859

js 高级函数特性

惰性函数 指的是函数只在第一次执行的时候进行检查运行环境的功能特性,并动态修改函数.以后再次执行的时候,就可以避免重复性检查环境 比如,以前我们创建一个XHRHttpRequest请求的时候会这么...
  • qq_17347575
  • qq_17347575
  • 2017年09月20日 15:56
  • 91

Inside MSXML Performance(MSXML性能分析) (3)

MSXML FeaturesMSXML特点Next, lets examine some important scenarios associated with the Document Object...
  • ayusay
  • ayusay
  • 2002年07月15日 09:48
  • 823

Inside MSXML Performance(MSXML性能分析) (6)

Validation验证Validation compares the types of elements in an XML document against a Document Type Def...
  • ayusay
  • ayusay
  • 2002年07月15日 14:53
  • 711

ASP程序的性能优化

好久没有写东西,今天晚上在图书馆花了2个小时写成的,希望对大家有用。ASP程序的性能优化第一章         使用服务器缓存1.         使用application和session保存数据的...
  • AloneSword
  • AloneSword
  • 2005年09月25日 21:41
  • 1633
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Inside MSXML Performance(MSXML性能分析) (5)
举报原因:
原因补充:

(最多只允许输入30个字)