using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
namespace TestPerf
{
internal class Program
{
static int TOTALITEMS = 400000;
static int SELECTITEMS = 100;
static Stopwatch _swInt= new Stopwatch();
static Stopwatch _swGuid = new Stopwatch();
static void Main(string[] args)
{
try
{
ConfigureDb();
TestDb();
}
catch(DbUpdateException)
{
TestDb();
}
finally {
DropTables();
}
}
private static void DropTables()
{
using var db = new ApplicationContext();
db.IntTest.FromSql($"DROP TABLE IF EXISTS public.inttest;");
db.IntTest.FromSql($"DROP TABLE IF EXISTS public.guidtest;");
db.SaveChanges();
}
private static void TestDb()
{
var intId = new List<long>();
var rand = new Random();
for (int i = 0; i < SELECTITEMS; i++)
{
intId.Add(rand.Next(1,TOTALITEMS));
}
var guidId = new List<Guid>();
using var db = new ApplicationContext();
foreach (var id in intId)
{
guidId.Add(db.GuidTest.Where(x=>x.Idn == id).First().Id);
}
_swInt.Start();
var intRes = db
.IntTest
.Where(x => intId.Contains(x.Id)).ToList();
_swInt.Stop();
_swGuid.Start();
var guidRes = db
.GuidTest
.Where(x => guidId.Contains(x.Id)).ToList();
_swGuid.Stop();
Console.WriteLine("Int key time: " + _swInt.ElapsedMilliseconds);
Console.WriteLine("Guid key time: " + _swGuid.ElapsedMilliseconds);
}
private static void ConfigureDb()
{
using var db = new ApplicationContext();
for (int i = 1; i < TOTALITEMS; i++)
{
var it = new IntTest { Id = i };
var gt = new GuidTest { Id = Guid.NewGuid(), Idn = i };
db.Add(it);
db.Add(gt);
}
db.SaveChanges();
}
}
public class IntTest
{
public Int64 Id { get; set; }
}
public class GuidTest
{
public Guid Id { get; set; }
public Int64 Idn { get; set; }
}
public class ApplicationContext : DbContext
{
public DbSet<IntTest> IntTest => Set<IntTest>();
public DbSet<GuidTest> GuidTest => Set<GuidTest>();
public ApplicationContext() => Database.EnsureCreated();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql("Server=localhost;Port=5432;Database=test;Username=test;Password=test");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var builder = modelBuilder.Entity<GuidTest>();
builder.ToTable("guidtest");
builder.HasKey(x => x.Id);
builder.HasIndex(x => x.Id).IsUnique();
builder.Property(x => x.Id)
.HasColumnName("Id")
.HasColumnType("uuid")
.HasDefaultValueSql("uuid_generate_v4()")
.IsRequired();
builder.HasIndex(x => x.Idn).IsUnique();
var builderi = modelBuilder.Entity<IntTest>();
builderi.ToTable("inttest");
builderi.HasKey(x => x.Id);
builderi.HasIndex(x => x.Id).IsUnique();
}
}
}
const fetch = require('node-fetch');
let url = "https://www.reddit.com/r/popular.json";
let settings = { method: "Get" };
fetch(url, settings)
.then(res => res.json())
.then((json) => {
// do something with JSON
});
const https = require('https');
let url = "https://www.reddit.com/r/popular.json";
https.get(url,(res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
let json = JSON.parse(body);
// do something with JSON
} catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.error(error.message);
});
const request = require('request');
let url = "https://www.reddit.com/r/popular.json";
let options = {json: true};
request(url, options, (error, res, body) => {
if (error) {
return console.log(error)
};
if (!error && res.statusCode == 200) {
// do something with JSON, using the 'body' variable
};
});