Go experiments

This commit is contained in:
Eduard Urbach 2016-10-29 01:11:25 +09:00
parent 6805afaa61
commit 4ed5d14bb5
11 changed files with 100 additions and 52 deletions

5
.gitignore vendored
View File

@ -31,4 +31,7 @@ vendor/
node_modules/ node_modules/
# debugger # debugger
debug debug
# pixy
❖.go

1
layout.css Normal file

File diff suppressed because one or more lines are too long

7
layout/layout.pixy Normal file
View File

@ -0,0 +1,7 @@
component Layout(content string, css string)
html
head
title Test
style!= css
body
main#content

View File

@ -1,6 +0,0 @@
doctype html
html
head
title Test
body
{{ yield }}

View File

@ -1 +0,0 @@
{{ yield }}

51
main.go
View File

@ -6,36 +6,65 @@ import (
"github.com/aerojs/aero" "github.com/aerojs/aero"
"github.com/blitzprog/arn" "github.com/blitzprog/arn"
"github.com/valyala/fasthttp"
) )
func main() { func main() {
app := aero.New() app := aero.New()
example, _ := ioutil.ReadFile("security/frontpage.html") cssBytes, _ := ioutil.ReadFile("layout.css")
css := string(cssBytes)
css = css
app.Get("/", func(ctx *aero.Context) { app.Get("/", func(ctx *aero.Context) {
ctx.RespondBytes(example) ctx.Respond(Stream.Dashboard())
}) })
app.Get("/hello", func(ctx *aero.Context) {
ctx.Respond("Hello World")
})
template := aero.NewTemplate("pages/anime/anime.pug")
app.Get("/anime/:id", func(ctx *aero.Context) { app.Get("/anime/:id", func(ctx *aero.Context) {
id, _ := strconv.Atoi(ctx.Params.ByName("id")) id, _ := strconv.Atoi(ctx.Params.ByName("id"))
anime, err := arn.GetAnime(id) anime, err := arn.GetAnime(id)
anime = anime
if err != nil { if err != nil {
ctx.Respond("Anime not found") ctx.Respond("Anime not found")
return return
} }
templateParams := make(map[string]interface{}) stream := fasthttp.AcquireByteBuffer()
templateParams["anime"] = anime Stream.Layout(stream, anime, css)
ctx.Respond(template.Render(templateParams)) ctx.RespondBytes(stream.Bytes())
Stream.Release(stream)
// ctx.Respond(Render.Layout(Render.Anime(anime), css))
}) })
// layout := aero.NewTemplate("layout.pug")
// template := aero.NewTemplate("anime.pug")
// app.Get("/anime/:id", func(ctx *aero.Context) {
// id, _ := strconv.Atoi(ctx.Params.ByName("id"))
// anime, err := arn.GetAnime(id)
// if err != nil {
// ctx.Respond("Anime not found")
// return
// }
// content := template.Render(map[string]interface{}{
// "anime": anime,
// })
// final := layout.Render(map[string]interface{}{
// "content": content,
// })
// final = strings.Replace(final, cssSearch, cssReplace, 1)
// ctx.Respond(final)
// })
// app.Get("/t", func(ctx *aero.Context) {
// ctx.Respond(templates.Hello("abc"))
// })
app.Run() app.Run()
} }

View File

@ -1,33 +1,58 @@
package main package main
import ( import (
"bytes"
"fmt"
"strings"
"testing" "testing"
"unsafe"
) )
var code = "Hello World" // func BenchmarkRender(b *testing.B) {
var slice = []byte("Hello World 2") // b.ReportAllocs()
// layout := aero.NewTemplate("layout/layout.pug")
// template := aero.NewTemplate("pages/anime/anime.pug")
// cssBytes, _ := ioutil.ReadFile("layout.css")
// css := string(cssBytes)
// cssSearch := "</head><body"
// cssReplace := "<style>" + css + "</style></head><body"
func BenchmarkStringToBytesSafe(b *testing.B) { // for i := 0; i < b.N; i++ {
// anime, _ := arn.GetAnime(1000001)
// content := template.Render(map[string]interface{}{
// "anime": anime,
// })
// final := layout.Render(map[string]interface{}{
// "content": content,
// })
// final = strings.Replace(final, cssSearch, cssReplace, 1)
// }
// }
func Benchmark1(b *testing.B) {
code := []byte(strings.Repeat("a", 10000))
b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
slice = []byte(code) var buf bytes.Buffer
buf.Write(code)
buf.Write(code)
buf.Write(code)
c := buf.String()
fmt.Println(len(c))
} }
} }
func BenchmarkStringToBytesUnsafe(b *testing.B) { func Benchmark2(b *testing.B) {
for i := 0; i < b.N; i++ { code := strings.Repeat("a", 10000)
slice = *(*[]byte)(unsafe.Pointer(&code))
}
}
func BenchmarkBytesToStringSafe(b *testing.B) { b.ReportAllocs()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
code = string(slice) c := code
} c += code
} c += code
fmt.Println(len(c))
func BenchmarkBytesToStringUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = *(*string)(unsafe.Pointer(&slice))
} }
} }

3
pages/anime/anime.pixy Normal file
View File

@ -0,0 +1,3 @@
component Anime(anime *arn.Anime)
h2= anime.Title.Romaji
p= anime.Description

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,2 @@
component Dashboard
h1 notify.moe

View File

@ -1 +0,0 @@
h1 notify.moe