运行基准测试时XML解析速度慢

时间:2019-02-07 08:08:15

标签: xml go benchmarking

我想测量GoLang解析XML文件所花费的时间。 因此,我决定编写基准测试。

我确实有一个函数可以生成包含XML文档的io.Reader

// PRIVATE: createSampleXMLReader creates an io.Reader instance that contains 10.000 '<Node />' elements which are
//          suitable for running a benchmark test.
func createSampleXMLReader(
    nodeElementCount int) io.Reader {
    xmlContents := new(strings.Builder)

    xmlContents.WriteString("<ROOT>\n")

    for i := 0; i < nodeElementCount; i++ {
        appendNodeXMLElement(xmlContents)
    }

    xmlContents.WriteString("</ROOT>")

    return strings.NewReader(xmlContents.String())
}

// PRIVATE: appendNodeXMLElement appends a '<Node />' elements to an existing io.Reader instance.
func appendNodeXMLElement(
    xmlDocument *strings.Builder) {

    xmlDocument.WriteString("<Node id=\"0\" position=\"0\" depth=\"0\" parent=\"0\">\n")
    xmlDocument.WriteString("    <Name>Name</Name>\n")
    xmlDocument.WriteString("    <Description>Description</Description>\n")
    xmlDocument.WriteString("    <OwnInformation>\n")
    xmlDocument.WriteString("        <Title>Title</Title>\n")
    xmlDocument.WriteString("        <Description>Description</Description>\n")
    xmlDocument.WriteString("    </OwnInformation>\n")
    xmlDocument.WriteString("    <Assets>\n")
    xmlDocument.WriteString("        <Asset id=\"0\" position=\"0\" type=\"0\" category=\"0\">\n")
    xmlDocument.WriteString("            <OriginalFile>OriginalFile</OriginalFile>\n")
    xmlDocument.WriteString("            <Description>Description</Description>\n")
    xmlDocument.WriteString("            <Uri>Uri</Uri>\n")
    xmlDocument.WriteString("        </Asset>\n")
    xmlDocument.WriteString("        <Asset id=\"1\" position=\"1\" type=\"1\" category=\"1\">\n")
    xmlDocument.WriteString("            <OriginalFile>OriginalFile</OriginalFile>\n")
    xmlDocument.WriteString("            <Description>Description</Description>\n")
    xmlDocument.WriteString("            <Uri>Uri</Uri>\n")
    xmlDocument.WriteString("        </Asset>\n")
    xmlDocument.WriteString("        <Asset id=\"2\" position=\"2\" type=\"2\" category=\"2\">\n")
    xmlDocument.WriteString("            <OriginalFile>OriginalFile</OriginalFile>\n")
    xmlDocument.WriteString("            <Description>Description</Description>\n")
    xmlDocument.WriteString("            <Uri>Uri</Uri>\n")
    xmlDocument.WriteString("        </Asset>\n")
    xmlDocument.WriteString("        <Asset id=\"3\" position=\"3\" type=\"3\" category=\"3\">\n")
    xmlDocument.WriteString("            <OriginalFile>OriginalFile</OriginalFile>\n")
    xmlDocument.WriteString("            <Description>Description</Description>\n")
    xmlDocument.WriteString("            <Uri>Uri</Uri>\n")
    xmlDocument.WriteString("        </Asset>\n")
    xmlDocument.WriteString("        <Asset id=\"4\" position=\"4\" type=\"4\" category=\"4\">\n")
    xmlDocument.WriteString("            <OriginalFile>OriginalFile</OriginalFile>\n")
    xmlDocument.WriteString("            <Description>Description</Description>\n")
    xmlDocument.WriteString("            <Uri>Uri</Uri>\n")
    xmlDocument.WriteString("        </Asset>\n")
    xmlDocument.WriteString("    </Assets>\n")
    xmlDocument.WriteString("    <Synonyms>\n")
    xmlDocument.WriteString("        <Synonym>Synonym 0</Synonym>\n")
    xmlDocument.WriteString("        <Synonym>Synonym 1</Synonym>\n")
    xmlDocument.WriteString("        <Synonym>Synonym 2</Synonym>\n")
    xmlDocument.WriteString("        <Synonym>Synonym 3</Synonym>\n")
    xmlDocument.WriteString("        <Synonym>Synonym 4</Synonym>\n")
    xmlDocument.WriteString("    </Synonyms>\n")
    xmlDocument.WriteString("</Node>\n")
}

然后,我具有实际解析此XML文档的功能。

// PRIVATE: parseXML parses an io.Reader instance into a 'Node' struct.
func parseXML(
    xmlReader io.Reader) {

    xmlDecoder := xml.NewDecoder(xmlReader)

    for {
        token, _ := xmlDecoder.Token()
        if token == nil {
            break
        }

        switch element := token.(type) {
        case xml.StartElement:
            if element.Name.Local == "Node" {
                decodeNodeElement(xmlDecoder, &element)
            }
        }
    }
}

// PRIVATE: decodeNodeElement decodes a '<Node />' element into a 'Node' struct.
func decodeNodeElement(
    xmlDecoder *xml.Decoder,
    element *xml.StartElement) {

    node := new(model.Node)

    xmlDecoder.DecodeElement(node, element)
}

然后,我具有执行基准测试的功能:

// PRIVATE: runBenchmarkParseXML performs a benchmark that parses an XML document that contains the given number of
//          '<Node />' element.
func runBenchmarkParseXML(
    nodeCount int,
    benchmark *testing.B) {

    // Arrange.
    xmlReader := createSampleXMLReader(nodeCount)

    // Act.
    for i := 0; i < benchmark.N; i++ {
        parseXML(xmlReader)
    }
}

然后我有5个函数执行基准测试。 这些用于XML文档中的1、10、100、1000和10000个元素。

func BenchmarkParseXML1(benchmark *testing.B)     { runBenchmarkParseXML(1, benchmark) }
func BenchmarkParseXML10(benchmark *testing.B)    { runBenchmarkParseXML(10, benchmark) }
func BenchmarkParseXML100(benchmark *testing.B)   { runBenchmarkParseXML(100, benchmark) }
func BenchmarkParseXML1000(benchmark *testing.B)  { runBenchmarkParseXML(1000, benchmark) }
func BenchmarkParseXML10000(benchmark *testing.B) { runBenchmarkParseXML(10000, benchmark) }

运行此基准测试时,会看到以下输出:

BenchmarkParseXML1-4             5000000               226 ns/op
BenchmarkParseXML10-4           10000000               230 ns/op
BenchmarkParseXML100-4           5000000               226 ns/op
BenchmarkParseXML1000-4          5000000               254 ns/op
BenchmarkParseXML10000-4               1        1690998100 ns/op

在基准测试中,解析包含10.000个元素的XML文件的基准测试中是否有这样的差异,其余基准测试时间是稳定的?

我的基准测试是新的还是ParseXML方法的实现不正确。

编辑:节点结构

// Node represents a '<Node />' element in the XML document.
type Node struct {
    ID             int    `xml:"id,attr"`
    Position       int    `xml:"position,attr"`
    Depth          int    `xml:"depth,attr"`
    Parent         string `xml:"parent,attr"`
    Name           string `xml:"Name"`
    Description    string `xml:"Description"`
    OwnInformation struct {
        Title       string `xml:"Title"`
        Description string `xml:"Description"`
    } `xml:"OwnInformation"`
    Assets []struct {
        ID           string `xml:"id,attr"`
        Position     int    `xml:"position,attr"`
        Type         string `xml:"type,attr"`
        Category     int    `xml:"category,attr"`
        OriginalFile string `xml:"OriginalFile"`
        Description  string `xml:"Description"`
        URI          string `xml:"Uri"`
    } `xml:"Assets>Asset"`
    Synonyms []string `xml:"Synonyms>Synonym"`
}

谢谢您的指导。

1 个答案:

答案 0 :(得分:2)

您的基准测试存在缺陷。您将输入增加了10倍,但持续时间在整个过程中大致保持不变,这一事实使您对这个基准非常怀疑。

您每次都在重复使用相同的阅读器。实际上,每个基准测试的第一次迭代实际上都不会做任何事情。将会从已经在EOF的阅读器读取进一步的呼叫。

更改设置,使其返回一个字节片,并为每次迭代构造一个新的读取器。这将产生预期的结果:

func createSampleXMLDoc(nodeElementCount int) []byte {
    xmlContents := &bytes.Buffer{}

    xmlContents.WriteString("<ROOT>\n")

    for i := 0; i < nodeElementCount; i++ {
        appendNodeXMLElement(xmlContents)
    }

    xmlContents.WriteString("</ROOT>")
    return xmlContents.Bytes()
}

func runBenchmarkParseXML(nodeCount int, b *testing.B) {
    doc := createSampleXMLDoc(nodeCount)

    for i := 0; i < b.N; i++ {
        xmlReader := bytes.NewReader(doc)
        parseXML(xmlReader)
    }
}

计算机上的结果(按预期,输入增加10倍,时间增加10倍):

$ go test -benchtime=5s -bench .
goos: linux
goarch: amd64
BenchmarkParseXML1-8              100000            115978 ns/op
BenchmarkParseXML10-8              10000           1147605 ns/op
BenchmarkParseXML100-8              1000          11586980 ns/op
BenchmarkParseXML1000-8               50         124199120 ns/op
BenchmarkParseXML10000-8               5        1003668966 ns/op

-benchtime=5s将每个基准的默认时间从一秒增加到五秒。在最后一种情况下,一秒钟的迭代次数是不够的,从而导致结果不可靠。这也是为什么在原始基准中看到大量数字的原因。第一次迭代很慢,但是之后的每个迭代都会立即返回,因此平均时间急剧下降。顺便说一句,在基准测试中只看到一个迭代是另一个危险信号。实际上,即使进行可靠的测量,即使进行五次迭代也仍然很低。

故事的寓意:检查您的错误!