Skip to content

Instantly share code, notes, and snippets.

@kmizumar
Created January 9, 2015 08:05
Show Gist options
  • Select an option

  • Save kmizumar/57648450a38fd09e694e to your computer and use it in GitHub Desktop.

Select an option

Save kmizumar/57648450a38fd09e694e to your computer and use it in GitHub Desktop.
create a report from csv
#!/usr/bin/env gosh
;;
;; tokyobench csv output report
;;
(use gauche.parseopt)
(use gauche.record)
(use slib)
(use text.csv)
(use util.match)
(require 'format)
(define (p . args) (for-each print args))
(define (usage)
(p (format #f "Usage: ~a [options] file" *program-name*)
"Options:"
" -w, --wiki : print output as trac/wiki table")
(exit 0))
(define-class <test> ()
((clv :init-keyword :command)
(ksiz :init-keyword :keysize)
(vsiz :init-keyword :valsize)))
(define-method object-equal? ((a <test>) (b <test>))
(and (eq? (ref a 'clv) (ref b 'clv))
(eq? (ref a 'ksiz) (ref b 'ksiz))
(eq? (ref a 'vsiz) (ref b 'vsiz))))
(define-method object-compare ((a <test>) (b <test>))
(cond [(string<? (symbol->string (ref a 'clv))
(symbol->string (ref b 'clv))) -1]
[(and (eq? (ref a 'clv) (ref b 'clv))
(< (ref a 'ksiz) (ref b 'ksiz))) -1]
[(and (eq? (ref a 'clv) (ref b 'clv))
(= (ref a 'ksiz) (ref b 'ksiz))
(< (ref a 'vsiz) (ref b 'vsiz))) -1]
[(object-equal? a b) 0]
[else 1]))
(define-method object-hash ((a <test>))
(hash `(,(ref a 'clv) ,(ref a 'ksiz) ,(ref a 'vsiz))))
(define-record-type result #t #t
(put) (get) (out))
(define (get-trial dict clv kz vz)
(let* ((id (make <test> :command clv :keysize kz :valsize vz))
(entry (hash-table-get dict id #f)))
(if (boolean entry)
entry
(let1 newentry (make-result '() '() '())
(hash-table-put! dict id newentry)
newentry))
))
(define (read-record dict line)
(match line
[(op _ clv kz vz _ _ _ et sp)
(let1 rslt (get-trial dict
(string->symbol clv) (string->number kz) (string->number vz))
(slot-set! rslt (string->symbol op)
(append (ref rslt (string->symbol op))
`((,(string->number et) ,(string->number sp))))))]
))
(define (report dict)
(letrec
((average (^[sum n lis]
(if (null? lis)
(map (^s (/. s n)) sum)
(average (map + (car lis) sum) (+ n 1) (cdr lis)))))
(println (^[op lis]
(for-each (^p (format #t "~a,~a,~a\n" op (car p) (cadr p)))
lis)))
(printer (^[op rs]
(println op (ref rs op))
(let1 avg (average '(0 0) 0 (ref rs op))
(format #t "avg.~a,~a,~a\n" op (car avg) (cadr avg)))))
)
(for-each
(^t
(format #t "~a,~a,~a\n" (ref t 'clv) (ref t 'ksiz) (ref t 'vsiz))
(for-each (^o (printer o (hash-table-get dict t)))
'(put get out)))
(sort (hash-table-keys dict)))
))
(define (wiki-report dict)
(letrec
((average (^[sum n lis]
(if (null? lis)
(map (^s (/. s n)) sum)
(average (map + (car lis) sum) (+ n 1) (cdr lis)))))
(printer (^[op t rs]
(let1 avg (average '(0 0) 0 (ref rs op))
(format #t "||~a||~a||~a||~a||~d||~6,3f||\n"
op (if (eq? (ref t 'clv) 'vanilla) "v" "e")
(ref t 'ksiz) (ref t 'vsiz)
(inexact->exact (truncate (car avg)))
(/ (cadr avg) 1024 1024)
)
)))
)
(for-each
(^t
(for-each (^o (printer o t (hash-table-get dict t)))
'(put get out)))
(sort (hash-table-keys dict)))
))
;; Entry point
(define (main args)
(let-args (cdr args)
([wiki "w|wiki"]
[#f "h|help" => usage]
[else (opt . _) (print "Unknown option : " opt) (usage)]
. args)
(match args
[() (usage)]
[(csv-file)
(let ([reader (make-csv-reader #\,)]
[dict (make-hash-table 'equal?)])
(call-with-input-file csv-file
(^[in]
(port-for-each
(^[line] (read-record dict line))
(^[] (reader in)))))
((if wiki wiki-report report) dict))]
[else (usage)])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment