Project

Profile

Help

How to connect?
Download (8.3 KB) Statistics
| Branch: | Revision:

he / src / test / nunit / SaxonNUnit / SaxonNUnit / TestTimingPerformance.cs @ 3df04590

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.Linq;
5
using System.IO;
6
using System.Text;
7
using Saxon.Api;
8
using NUnit.Framework;
9
using System.Xml;
10
using System.Diagnostics;
11
using System.Collections.Concurrent;
12

    
13
namespace SaxonNUnit
14
{
15
    class TestTimingPerformance
16
    {
17

    
18
        public static TimeSpan Time(Action action)
19
        {
20
            Stopwatch stopwatch = Stopwatch.StartNew();
21
            action();
22
            stopwatch.Stop();
23
            return stopwatch.Elapsed;
24
        }
25

    
26
        
27
        
28

    
29
        [Test]
30
        public void TestPerformance()
31
        {
32
           // _processor.SetProperty("http://saxon.sf.net/feature/debugByteCode", "true");
33
            _processor.SetProperty("http://saxon.sf.net/feature/generateByteCode", "true");
34
            string sourcePath = Path.GetFullPath("C:\\work\\misc\\xmark\\xmark10.xml");
35
            string stylesheetPath = Path.GetFullPath("C:\\work\\misc\\xmark\\q12.xsl");
36
            string outputPath = Path.ChangeExtension(sourcePath, ".out.xml");
37
            Stopwatch stopwatch = Stopwatch.StartNew();
38
            RunSaxonApi(sourcePath, stylesheetPath, outputPath);
39
            stopwatch.Stop();
40
            TimeSpan apiTime  = stopwatch.Elapsed;
41

    
42
            stopwatch = Stopwatch.StartNew();
43
            RunTransformCommand(sourcePath, stylesheetPath, outputPath);
44
            stopwatch.Stop();
45
            TimeSpan transTime =  stopwatch.Elapsed;
46
            RunSaxonApi2(sourcePath, stylesheetPath /*"C:\\work\\tests\\XSL\\Master\\Master.xsl"*/, outputPath);
47
            Console.WriteLine("Time to execute Saxon Api: " + apiTime.Milliseconds + "ms (" + apiTime.Seconds + " sec) and Elapsed:" + apiTime);
48

    
49
            Console.WriteLine("Time to execute Saxon command: " + transTime.Milliseconds + "ms (" + transTime.Seconds + " sec) and Elapsed:" + transTime);
50
            Assert.True(true, "******************XXXXXXXXXXXXXXX Time to execute Saxon Api: " + apiTime.Milliseconds + "ms (" + apiTime.Seconds + " sec) and Elapsed:" + apiTime + "\n" + "Time to execute Saxon command: " + transTime.Milliseconds + "ms (" + transTime.Seconds + " sec) and Elapsed:" + transTime);
51

    
52
        }
53

    
54
        private readonly Processor _processor = new Processor(true);
55

    
56
        public void RunSaxonApi(string sourcePath, string stylesheetPath, string outputPath)
57
        {
58

    
59
            using (var source = File.OpenRead(sourcePath))
60
            using (var stylesheet = File.OpenRead(stylesheetPath))
61
            {
62
                Uri stylesheetUri, sourceUri;
63
                if (!Uri.TryCreate(sourcePath, UriKind.RelativeOrAbsolute, out sourceUri))
64
                    throw new ArgumentException(string.Format("Could not create an URI from source path '{0}'.", sourcePath), "sourcePath");
65
                if (!Uri.TryCreate(stylesheetPath, UriKind.RelativeOrAbsolute, out stylesheetUri))
66
                    throw new ArgumentException(string.Format("Could not create an URI from stylesheet path '{0}'.", stylesheetPath), "stylesheetPath");
67

    
68
                var compiler = _processor.NewXsltCompiler();
69

    
70
                compiler.BaseUri = stylesheetUri; //required to resolve stylesheet includes
71
                compiler.ErrorList = new List<StaticError>();
72
                var executable = compiler.Compile(stylesheet);
73
                var transformer = executable.Load();
74
                var builder = _processor.NewDocumentBuilder();
75
                //	builder.XmlResolver = new DtdIgnoringResolver(builder.XmlResolver);
76
                //required to ignore DTDs in System IDs
77
                builder.BaseUri = sourceUri;
78
                transformer.InitialContextNode = builder.Build(source);
79
                var destination = _processor.NewSerializer();
80
                using (var output = File.Open(outputPath, FileMode.Create))
81
                {
82
                    destination.SetOutputStream(output);
83
                    transformer.Run(destination);
84
                }
85
            }
86
        }
87

    
88

    
89
        public void RunSaxonApi2(string sourcePath, string stylesheetPath, string outputPath)
90
        {
91

    
92
            using (var source = File.OpenRead(sourcePath))
93
            using (var stylesheet = File.OpenRead(stylesheetPath))
94
            {
95
                Uri stylesheetUri, sourceUri;
96
                if (!Uri.TryCreate(sourcePath, UriKind.RelativeOrAbsolute, out sourceUri))
97
                    throw new ArgumentException(string.Format("Could not create an URI from source path '{0}'.", sourcePath), "sourcePath");
98
                if (!Uri.TryCreate(stylesheetPath, UriKind.RelativeOrAbsolute, out stylesheetUri))
99
                    throw new ArgumentException(string.Format("Could not create an URI from stylesheet path '{0}'.", stylesheetPath), "stylesheetPath");
100

    
101
                var compiler = _processor.NewXsltCompiler();
102

    
103
                compiler.BaseUri = stylesheetUri; //required to resolve stylesheet includes
104
                compiler.ErrorList = new List<StaticError>();
105
                 ConcurrentDictionary<Uri, XsltExecutable> _executables = new ConcurrentDictionary<Uri, XsltExecutable>();
106
                XsltExecutable executable = null;// compiler.Compile(stylesheet);
107
                try {
108
                    executable = _executables.GetOrAdd(new Uri(stylesheetPath), uri => compiler.Compile(uri));
109
                }
110
                catch (java.lang.ClassNotFoundException e) {
111
                    e.printStackTrace();
112
                    Console.WriteLine();
113
                    throw e;
114
                }
115
                catch (java.io.FileNotFoundException e)
116
                {
117
                    e.printStackTrace();
118
                    Console.WriteLine();
119
                    throw e;
120
                }
121
                var transformer = executable.Load();
122
                var builder = _processor.NewDocumentBuilder();
123
                //	builder.XmlResolver = new DtdIgnoringResolver(builder.XmlResolver);
124
                //required to ignore DTDs in System IDs
125
                builder.BaseUri = sourceUri;
126
                transformer.InitialContextNode = builder.Build(source);
127
                var destination = _processor.NewSerializer();
128
                using (var output = File.Open(outputPath, FileMode.Create))
129
                {
130
                    destination.SetOutputStream(output);
131
                    transformer.Run(destination);
132
                }
133
            }
134
        }
135

    
136

    
137

    
138
        public void RunTransformCommand(string sourcePath, string stylesheetPath, string outputPath)
139
        {
140
            net.sf.saxon.Transform transform = new net.sf.saxon.Transform();
141
            transform.doTransform(new[] { "-s:" + stylesheetPath, "-xsl:" + stylesheetPath, "-o:" + outputPath }, "transform");
142
        }
143

    
144
        static void Main1(string[] args)
145
        {
146
            args = new[] { "C:\\work\\misc\\xmark\\xmark10.xml", "C:\\work\\misc\\xmark\\q12.xsl", "C:\\work\\misc\\xmark\\outputvs.xml" };
147
            if (args.Length < 2 || args.Length > 3)
148
            {
149
                Console.WriteLine("Usage: SaxonTransform input stylesheet[output]");
150
                return;
151
            }
152
            try
153
            {
154
                string sourcePath = Path.GetFullPath(args[0]);
155
                string stylesheetPath = Path.GetFullPath(args[1]);
156
                string outputPath = args.Length == 3 ? Path.GetFullPath(args[2]) : Path.ChangeExtension(sourcePath, ".out.xml");
157
                TestTimingPerformance prog = new TestTimingPerformance();
158
                Console.WriteLine("Starting transform at {0}", DateTime.Now);
159
                var sw = Stopwatch.StartNew();
160
                //TransformDirect(sourcePath, stylesheetPath, outputPath);
161
                prog.RunTransformCommand(sourcePath, stylesheetPath, outputPath);
162
                sw.Stop();
163
                Console.WriteLine("Finished transform at {0} (after {1})", DateTime.Now, sw.Elapsed);
164
                Console.WriteLine();
165
                Console.WriteLine("===========================");
166
                Console.WriteLine("Starting Saxon.Api at {0}", DateTime.Now);
167
                sw = Stopwatch.StartNew();
168
                prog.RunSaxonApi(sourcePath, stylesheetPath, outputPath);
169
                sw.Stop();
170
                Console.WriteLine("Finished transform at {0} (after {1})", DateTime.Now, sw.Elapsed);
171
            }
172
            catch (Exception ex)
173
            {
174
                Console.WriteLine("Unhandled exception: {0}", ex);
175
            }
176
        }
177
    }
178
}
(12-12/19)