1111 *
1212 * @param options {Object} reporter options
1313 * id: {String} report id
14- * env: {String } environment description
14+ * env: {Object } environment description
1515 */
1616function EarlReport ( options ) {
1717 let today = new Date ( ) ;
@@ -24,6 +24,8 @@ function EarlReport(options) {
2424 this . now . setMilliseconds ( 0 ) ;
2525 this . id = options . id ;
2626 this . env = options . env ;
27+ // test environment
28+ this . _environment = null ;
2729 /* eslint-disable quote-props */
2830 this . _report = {
2931 '@context' : {
@@ -69,35 +71,40 @@ function EarlReport(options) {
6971 'foaf:homepage' : 'https://digitalbazaar.com/'
7072 } ,
7173 'doap:release' : {
72- 'doap:name' : '' ,
7374 'doap:revision' : '' ,
7475 'doap:created' : today
7576 } ,
7677 'subjectOf' : [ ]
7778 } ;
7879 /* eslint-enable quote-props */
79- this . _report [ '@id' ] +=
80- '#' + this . id ;
81- this . _report [ 'doap:name' ] +=
82- ' ' + this . id + ( this . env ? ' ' + this . env : '' ) ;
83- this . _report [ 'dc:title' ] +=
84- ' ' + this . id + ( this . env ? ' ' + this . env : '' ) ;
80+ if ( this . env && this . env . version ) {
81+ this . _report [ 'doap:release' ] [ 'doap:name' ] = this . env . version ;
82+ }
8583}
8684
8785EarlReport . prototype . addAssertion = function ( test , pass , options ) {
8886 options = options || { } ;
89- this . _report . subjectOf . push ( {
87+ const assertion = {
9088 '@type' : 'earl:Assertion' ,
9189 'earl:assertedBy' : this . _report [ 'doap:developer' ] [ '@id' ] ,
9290 'earl:mode' : 'earl:automatic' ,
9391 'earl:test' : test [ '@id' ] ,
9492 'earl:result' : {
9593 '@type' : 'earl:TestResult' ,
9694 'dc:date' : this . now . toISOString ( ) ,
97- 'earl:outcome' : pass ? 'earl:passed' : 'earl:failed' ,
98- ...options . extra
95+ 'earl:outcome' : pass ? 'earl:passed' : 'earl:failed'
9996 }
100- } ) ;
97+ } ;
98+ if ( options . benchmarkResult ) {
99+ const result = {
100+ ...options . benchmarkResult
101+ } ;
102+ if ( this . _environment ) {
103+ result [ 'jldb:environment' ] = this . _environment [ '@id' ] ;
104+ }
105+ assertion [ 'jldb:result' ] = result ;
106+ }
107+ this . _report . subjectOf . push ( assertion ) ;
101108 return this ;
102109} ;
103110
@@ -109,4 +116,81 @@ EarlReport.prototype.reportJson = function() {
109116 return JSON . stringify ( this . _report , null , 2 ) ;
110117} ;
111118
119+ /* eslint-disable quote-props */
120+ const _benchmarkContext = {
121+ 'jldb' : 'http://json-ld.org/benchmarks/vocab#' ,
122+ 'rdfs' : 'http://www.w3.org/2000/01/rdf-schema#' ,
123+ 'xsd' : 'http://www.w3.org/2001/XMLSchema#' ,
124+
125+ // environment description
126+ 'jldb:Environment' : { '@type' : '@id' } ,
127+
128+ // per environment
129+ // architecture type
130+ // ex: x86
131+ 'jldb:arch' : { '@type' : 'xsd:string' } ,
132+ // cpu model description (may show multiple cpus)
133+ // ex: 'Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz'
134+ 'jldb:cpu' : { '@type' : 'xsd:string' } ,
135+ // count of cpus, may not be uniform, just informative
136+ 'jldb:cpuCount' : { '@type' : 'xsd:integer' } ,
137+ // platform name
138+ // ex: linux
139+ 'jldb:platform' : { '@type' : 'xsd:string' } ,
140+ // runtime name
141+ // ex: Node.js, Chromium, Ruby
142+ 'jldb:runtime' : { '@type' : 'xsd:string' } ,
143+ // runtime version
144+ // ex: v14.19.0
145+ 'jldb:runtimeVersion' : { '@type' : 'xsd:string' } ,
146+ // arbitrary comment
147+ 'jldb:comment' : 'rdfs:comment' ,
148+
149+ // benchmark result
150+ 'jldb:BenchmarkResult' : { '@type' : '@id' } ,
151+
152+ // use in earl:Assertion, type jldb:BenchmarkResult
153+ 'jldb:result' : { '@type' : '@id' } ,
154+
155+ // per BenchmarkResult
156+ 'jldb:environment' : { '@type' : '@id' } ,
157+ 'jldb:hz' : { '@type' : 'xsd:float' } ,
158+ 'jldb:rme' : { '@type' : 'xsd:float' }
159+ } ;
160+ /* eslint-enable quote-props */
161+
162+ // setup @context and environment to handle benchmark data
163+ EarlReport . prototype . setupForBenchmarks = function ( options ) {
164+ // add context if needed
165+ if ( ! Array . isArray ( this . _report [ '@context' ] ) ) {
166+ this . _report [ '@context' ] = [ this . _report [ '@context' ] ] ;
167+ }
168+ if ( ! this . _report [ '@context' ] . some ( c => c === _benchmarkContext ) ) {
169+ this . _report [ '@context' ] . push ( _benchmarkContext ) ;
170+ }
171+ if ( options . testEnv ) {
172+ // add report environment
173+ const fields = [
174+ [ 'arch' , 'jldb:arch' ] ,
175+ [ 'cpu' , 'jldb:cpu' ] ,
176+ [ 'cpuCount' , 'jldb:cpuCount' ] ,
177+ [ 'platform' , 'jldb:platform' ] ,
178+ [ 'runtime' , 'jldb:runtime' ] ,
179+ [ 'runtimeVersion' , 'jldb:runtimeVersion' ] ,
180+ [ 'comment' , 'jldb:comment' ]
181+ ] ;
182+ const _env = {
183+ '@id' : '_:environment:0'
184+ } ;
185+ for ( const [ field , property ] of fields ) {
186+ if ( options . testEnv [ field ] ) {
187+ _env [ property ] = options . testEnv [ field ] ;
188+ }
189+ }
190+ this . _environment = _env ;
191+ this . _report [ '@included' ] = this . _report [ '@included' ] || [ ] ;
192+ this . _report [ '@included' ] . push ( _env ) ;
193+ }
194+ } ;
195+
112196module . exports = EarlReport ;
0 commit comments